Git 命令的基本使用 clone、push 等
作者:忙着长大#
- 2022-11-13 北京
本文字数:2467 字
阅读完需:约 8 分钟
1、git clone 克隆代码到本地
## 新建一个目录 cd 进去,然后执行clone代码
root@yt:~# mkdir my-gitlab-code
root@yt:~# cd my-gitlab-code/
root@yt:~/my-gitlab-code# pwd
/root/my-gitlab-code
root@yt:~/my-gitlab-code# git clone http://192.168.131.130/my-app1/app1-1.git
Cloning into 'app1-1'...
Username for 'http://192.168.131.130': user2
Password for 'http://user2@192.168.131.130':
remote: Enumerating objects: 9, done.
remote: Counting objects: 100% (9/9), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 9 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (9/9), 3.26 KiB | 3.26 MiB/s, done.
root@yt:~/my-gitlab-code# ls
app1-1
复制代码
2、git push 将本地代码同步到代码仓库
## 首先执行 git add . 将变化的文件放入暂存区
root@yt:~/my-gitlab-code/app1-1# git add .
## 然后执行 git commit -m "xx" 编写提交变更信息注释 , 注意到这里位置都是属于再本地的修改,不会影响到代码仓库的内容
root@yt:~/my-gitlab-code/app1-1# git commit -m "xxx"
[main f61e0a1] xxx
Committer: root <root@1395001789@qq.com>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:
git config --global --edit
After doing this, you may fix the identity used for this commit with:
git commit --amend --reset-author
1 file changed, 1 insertion(+)
## 最后执行git push 将修改的代码push到代码仓库
root@yt:~/my-gitlab-code/app1-1# git push
Username for 'http://192.168.131.130': user2
Password for 'http://user2@192.168.131.130':
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 277 bytes | 277.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To http://192.168.131.130/my-app1/app1-1.git
8aee487..f61e0a1 main -> main
root@yt:~/my-gitlab-code/app1-1#
复制代码
3、git reflog 可以看到在本地进行过哪些操作,开头的数字就是 commitid
root@yt:~/my-gitlab-code/app1-1# git reflog
f61e0a1 (HEAD -> main, origin/main, origin/HEAD) HEAD@{0}: commit: xxx
8aee487 HEAD@{1}: clone: from http://192.168.131.130/my-app1/app1-1.git
复制代码
4、git reset --hard HEAD^ 向前回滚一个版本,HEAD 后跟几个“^”就回退几个版本
也可以使用 git reset --hard <commitid> 通过指定 commitid 去回滚到指定的代码提交版本
root@yt:~/my-gitlab-code/app1-1# git reflog
f61e0a1 (HEAD -> main, origin/main, origin/HEAD) HEAD@{0}: commit: xxx
8aee487 HEAD@{1}: clone: from http://192.168.131.130/my-app1/app1-1.git
root@yt:~/my-gitlab-code/app1-1# git reset --hard HEAD^
HEAD is now at 8aee487 index v2
root@yt:~/my-gitlab-code/app1-1# git reflog
8aee487 (HEAD -> main) HEAD@{0}: reset: moving to HEAD^
f61e0a1 (origin/main, origin/HEAD) HEAD@{1}: commit: xxx
8aee487 (HEAD -> main) HEAD@{2}: clone: from http://192.168.131.130/my-app1/app1-1.git
复制代码
通过对比回滚前后的 git log 可以看出回滚的效果 , git log 主要是看代码的提交记录
## 回滚后的log
root@yt:~/my-gitlab-code/app1-1# git log
commit 8aee4876f5417686ffc42390d29f6c85a6b433ae (HEAD -> main)
Author: root <root@1395001789@qq.com>
Date: Sun Nov 13 12:59:42 2022 +0000
index v2
commit f753dfef97c9508f92f454dff8c87fd9e2cb077d
Author: root <root@1395001789@qq.com>
Date: Sun Nov 13 12:45:52 2022 +0000
add index v1
commit 08896eae40625f644175a469251d6c9891b1f1b9
Author: Administrator <admin@example.com>
Date: Sun Nov 13 12:30:40 2022 +0000
Initial commit
## 重新拉去仓库中最新的代码的git log
root@yt:~/my-gitlab-code/app1-1# git pull
Username for 'http://192.168.131.130': user2
Password for 'http://user2@192.168.131.130':
Updating 8aee487..f61e0a1
Fast-forward
index.html | 1 +
1 file changed, 1 insertion(+)
root@yt:~/my-gitlab-code/app1-1# git log
commit f61e0a1114c86282356d53f6d17997fb876bbefb (HEAD -> main, origin/main, origin/HEAD)
Author: root <root@1395001789@qq.com>
Date: Sun Nov 13 13:29:18 2022 +0000
xxx
commit 8aee4876f5417686ffc42390d29f6c85a6b433ae
Author: root <root@1395001789@qq.com>
Date: Sun Nov 13 12:59:42 2022 +0000
index v2
commit f753dfef97c9508f92f454dff8c87fd9e2cb077d
Author: root <root@1395001789@qq.com>
Date: Sun Nov 13 12:45:52 2022 +0000
add index v1
commit 08896eae40625f644175a469251d6c9891b1f1b9
Author: Administrator <admin@example.com>
Date: Sun Nov 13 12:30:40 2022 +0000
Initial commit
root@yt:~/my-gitlab-code/app1-1#
复制代码
5、git branch 可以看到当前所在的分支
root@yt:~/my-gitlab-code/app1-1# git branch
* main
复制代码
6、git checkout 切换分支
## 如果分支不存在会报错
root@yt:~/my-gitlab-code/app1-1# git checkout develop
error: pathspec 'develop' did not match any file(s) known to git
## 加上 -b 后如果分支不存在会把分支创建出来
root@yt:~/my-gitlab-code/app1-1# git checkout -b develop
Switched to a new branch 'develop'
## 查看当前分支
root@yt:~/my-gitlab-code/app1-1# git branch
* develop
main
复制代码
划线
评论
复制
发布于: 刚刚阅读数: 4
忙着长大#
关注
还未添加个人签名 2022-02-09 加入
还未添加个人简介
评论