写点什么

Git 命令的基本使用 clone、push 等

作者:忙着长大#
  • 2022-11-13
    北京
  • 本文字数:2467 字

    阅读完需:约 8 分钟

1、git clone 克隆代码到本地

## 新建一个目录 cd 进去,然后执行clone代码root@yt:~# mkdir my-gitlab-coderoot@yt:~# cd my-gitlab-code/root@yt:~/my-gitlab-code# pwd/root/my-gitlab-coderoot@yt:~/my-gitlab-code# git clone http://192.168.131.130/my-app1/app1-1.gitCloning into 'app1-1'...Username for 'http://192.168.131.130': user2Password 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 0Unpacking objects: 100% (9/9), 3.26 KiB | 3.26 MiB/s, done.root@yt:~/my-gitlab-code# lsapp1-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 basedon your username and hostname. Please check that they are accurate.You can suppress this message by setting them explicitly. Run thefollowing command and follow the instructions in your editor to edityour 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 pushUsername for 'http://192.168.131.130': user2Password for 'http://user2@192.168.131.130': Enumerating objects: 5, done.Counting objects: 100% (5/5), done.Delta compression using up to 4 threadsCompressing 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 -> mainroot@yt:~/my-gitlab-code/app1-1#
复制代码

3、git reflog 可以看到在本地进行过哪些操作,开头的数字就是 commitid

root@yt:~/my-gitlab-code/app1-1# git reflogf61e0a1 (HEAD -> main, origin/main, origin/HEAD) HEAD@{0}: commit: xxx8aee487 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 reflogf61e0a1 (HEAD -> main, origin/main, origin/HEAD) HEAD@{0}: commit: xxx8aee487 HEAD@{1}: clone: from http://192.168.131.130/my-app1/app1-1.gitroot@yt:~/my-gitlab-code/app1-1# git reset --hard HEAD^HEAD is now at 8aee487 index v2root@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: xxx8aee487 (HEAD -> main) HEAD@{2}: clone: from http://192.168.131.130/my-app1/app1-1.git
复制代码

通过对比回滚前后的 git log 可以看出回滚的效果 , git log 主要是看代码的提交记录

## 回滚后的logroot@yt:~/my-gitlab-code/app1-1# git logcommit 8aee4876f5417686ffc42390d29f6c85a6b433ae (HEAD -> main)Author: root <root@1395001789@qq.com>Date:   Sun Nov 13 12:59:42 2022 +0000
index v2
commit f753dfef97c9508f92f454dff8c87fd9e2cb077dAuthor: root <root@1395001789@qq.com>Date: Sun Nov 13 12:45:52 2022 +0000
add index v1
commit 08896eae40625f644175a469251d6c9891b1f1b9Author: Administrator <admin@example.com>Date: Sun Nov 13 12:30:40 2022 +0000
Initial commit
## 重新拉去仓库中最新的代码的git logroot@yt:~/my-gitlab-code/app1-1# git pullUsername for 'http://192.168.131.130': user2Password for 'http://user2@192.168.131.130': Updating 8aee487..f61e0a1Fast-forward index.html | 1 + 1 file changed, 1 insertion(+)root@yt:~/my-gitlab-code/app1-1# git logcommit f61e0a1114c86282356d53f6d17997fb876bbefb (HEAD -> main, origin/main, origin/HEAD)Author: root <root@1395001789@qq.com>Date: Sun Nov 13 13:29:18 2022 +0000
xxx
commit 8aee4876f5417686ffc42390d29f6c85a6b433aeAuthor: root <root@1395001789@qq.com>Date: Sun Nov 13 12:59:42 2022 +0000
index v2
commit f753dfef97c9508f92f454dff8c87fd9e2cb077dAuthor: root <root@1395001789@qq.com>Date: Sun Nov 13 12:45:52 2022 +0000
add index v1
commit 08896eae40625f644175a469251d6c9891b1f1b9Author: Administrator <admin@example.com>Date: Sun Nov 13 12:30:40 2022 +0000
Initial commitroot@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 developerror: pathspec 'develop' did not match any file(s) known to git## 加上 -b 后如果分支不存在会把分支创建出来root@yt:~/my-gitlab-code/app1-1# git checkout -b developSwitched to a new branch 'develop'## 查看当前分支root@yt:~/my-gitlab-code/app1-1# git branch* develop  main
复制代码


用户头像

忙着长大#

关注

还未添加个人签名 2022-02-09 加入

还未添加个人简介

评论

发布
暂无评论
Git 命令的基本使用clone、push 等_忙着长大#_InfoQ写作社区