文章目录
- `GIT`基本操作
- `GIT`配置个人信息配置:
- `GIT`查看个人信息配置:
- `GIT`的三大区域
- `GIT`回滚:`git reset`
- `GIT`恢复日志:`git reflog`
- `GIT`三大区域转换
- `GIT`新建分支
- `GIT`合并分支
- `GIT`删除分支
- 码云上创建项目
- `GIT`变基:`git rebase`合并提交记录
- `GIT`变基:`git rebase`简化工作流
- `GIT`变基:`git rebase`合并代码
GIT
基本操作
GIT
是一个分布式版本控制软件,官网地址https://git-scm.com/
版本控制操作步骤:1,进入要管理的文件夹;2,初始化;3,管理;4,生成版本;下面是本次操作的流程演示过程
#第一步:进入要管理的文件夹
dream21th@dream21th MINGW64 /d/home
$ cd git-study/#第二步: 执行git初始化
dream21th@dream21th MINGW64 /d/home/git-study
$ git init
Initialized empty Git repository in D:/home/git-study/.git/#第三步: 查询需要管理的文件状态,abc.txt,index.html两个文件都未被管理
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git status
On branch masterNo commits yetUntracked files:(use "git add <file>..." to include in what will be committed)abc.txtindex.htmlnothing added to commit but untracked files present (use "git add" to track)#第四步: 通过add添加要管理的文件
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git add index.html
warning: in the working copy of 'index.html', LF will be replaced by CRLF the next time Git touches it#第五步: 查询需要管理的文件状态,发现只有abc.txt没有被管理,而index.html被git管理了
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git status
On branch masterNo commits yetChanges to be committed:(use "git rm --cached <file>..." to unstage)new file: index.htmlUntracked files:(use "git add <file>..." to include in what will be committed)abc.txt
#第六步: 通过git add .让所有没有被管理的文件被管理起来
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git add .dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git status
On branch masterNo commits yetChanges to be committed:(use "git rm --cached <file>..." to unstage)new file: abc.txtnew file: index.html
#第七步:通过git commit添加版本控制
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git commit -m "v1"
[master (root-commit) 986868c] v12 files changed, 2 insertions(+)create mode 100644 abc.txtcreate mode 100644 index.htmldream21th@dream21th MINGW64 /d/home/git-study (master)
$ git status
On branch master
nothing to commit, working tree clean# 第八步:修改index.html文件
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ vim index.html# 第九步: 修改index.html
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ vim index.html# 第十步: 重新添加文件管理控制
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git status
On branch master
Changes not staged for commit:(use "git add <file>..." to update what will be committed)(use "git restore <file>..." to discard changes in working directory)modified: index.htmlno changes added to commit (use "git add" and/or "git commit -a")dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git add .
warning: in the working copy of 'index.html', LF will be replaced by CRLF the next time Git touches itdream21th@dream21th MINGW64 /d/home/git-study (master)
$ git status
On branch master
Changes to be committed:(use "git restore --staged <file>..." to unstage)modified: index.htmldream21th@dream21th MINGW64 /d/home/git-study (master)
$ git commit -m "v2"
[master 1c7b174] v21 file changed, 2 insertions(+)# 第十一步: 查询版本控制日志dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git log
commit 1c7b174b6574698d880bc18efe38a7457cb2bd77 (HEAD -> master)
Author: dream21th <dream21th@126.com>
Date: Wed Jul 5 12:01:51 2023 +0800v2commit 986868cdd81c46867a700d63b79f494c3a97065e
Author: dream21th <dream21th@126.com>
Date: Wed Jul 5 11:59:30 2023 +0800v1
文件在管控台的三种颜色:红色(新增或者修改的文件),绿色(git已经管理的版本)。
GIT
配置个人信息配置:
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git config --global user.name "dream21th"dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git config --global user.email "dream21th@126.com"
GIT
查看个人信息配置:
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git config --global user.namedream21th@dream21th MINGW64 /d/home/git-study (master)
$ git config --global user.email
GIT
的三大区域
GIT
的三大区域是工作区,暂存区,版本库,他们之间的关系如下:
GIT
回滚:git reset
# 第一步:查看提交的版本号,确认要回退的版本号
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git log
commit df3b201b621dfc7b3b256a8d5a51cd229bb4f1fe (HEAD -> master)
Author: dream21th <dream21th@126.com>
Date: Wed Jul 5 13:57:13 2023 +0800v3commit 1c7b174b6574698d880bc18efe38a7457cb2bd77
Author: dream21th <dream21th@126.com>
Date: Wed Jul 5 12:01:51 2023 +0800v2commit 986868cdd81c46867a700d63b79f494c3a97065e
Author: dream21th <dream21th@126.com>
Date: Wed Jul 5 11:59:30 2023 +0800v1# 第二步: 假设这次要回退到v2版本,在git reset --hard后面跟上v2的版本号
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git reset --hard 1c7b174b6574698d880bc18efe38a7457cb2bd77
HEAD is now at 1c7b174 v2# 第三步: 回退后查看当前版本
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git log
commit 1c7b174b6574698d880bc18efe38a7457cb2bd77 (HEAD -> master)
Author: dream21th <dream21th@126.com>
Date: Wed Jul 5 12:01:51 2023 +0800v2commit 986868cdd81c46867a700d63b79f494c3a97065e
Author: dream21th <dream21th@126.com>
Date: Wed Jul 5 11:59:30 2023 +0800v1
GIT
恢复日志:git reflog
通过上面的例子版本已经回退到v2版本,假如这个时候,又想退回到原来的v3版本,可以通过下面的指令操作
# 第一步: 通过git reflog查询到v3的版本号
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git reflog
1c7b174 (HEAD -> master) HEAD@{1}: reset: moving to 1c7b174b6574698d880bc18efe38a7457cb2bd77
df3b201 HEAD@{2}: commit: v3
1c7b174 (HEAD -> master) HEAD@{3}: commit: v2
986868c HEAD@{4}: commit (initial): v1# 第二步: 通过git reset --hard加上版本号回退
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git reset --hard df3b201
HEAD is now at df3b201 v3# 第三步: 查看版本日志信息
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git log
commit df3b201b621dfc7b3b256a8d5a51cd229bb4f1fe (HEAD -> master)
Author: dream21th <dream21th@126.com>
Date: Wed Jul 5 13:57:13 2023 +0800v3commit 1c7b174b6574698d880bc18efe38a7457cb2bd77
Author: dream21th <dream21th@126.com>
Date: Wed Jul 5 12:01:51 2023 +0800v2commit 986868cdd81c46867a700d63b79f494c3a97065e
Author: dream21th <dream21th@126.com>
Date: Wed Jul 5 11:59:30 2023 +0800v1
GIT
三大区域转换
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wdjPaLKS-1688630161959)
# 第一步: 编写一个abc.txt在里面写v1
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ vim abc.txt# 第二步: 查询状态
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git status
On branch master
Changes not staged for commit:(use "git add <file>..." to update what will be committed)(use "git restore <file>..." to discard changes in working directory)modified: abc.txtno changes added to commit (use "git add" and/or "git commit -a")#第三步: 加入暂存区
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git add -A
warning: in the working copy of 'abc.txt', LF will be replaced by CRLF the next time Git touches it# 第四步:加入版本控制
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git commit -m "111"
[master ead1292] 1111 file changed, 1 insertion(+)# 第五步:编写文件,加入新内容v2
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ vim abc.txt# 第六步: 查看状态
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git status
On branch master
Changes not staged for commit:(use "git add <file>..." to update what will be committed)(use "git restore <file>..." to discard changes in working directory)modified: abc.txtno changes added to commit (use "git add" and/or "git commit -a")# 第七步: 加入暂存区
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git add .
warning: in the working copy of 'abc.txt', LF will be replaced by CRLF the next time Git touches it# 第八步:加入版本库
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git commit -m 'v2'
[master 5bf7911] v21 file changed, 1 insertion(+)# 第九步:查看文件内容
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ cat abc.txt
v1
v2# 第十步:查看版本日志
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git log
commit 5bf7911b941700fa5e1f2f60f87e6a4dbfa5f892 (HEAD -> master)
Author: dream21th <dream21th@126.com>
Date: Wed Jul 5 15:12:39 2023 +0800v2commit ead12928cf17629e451f4697505c681ad8d2cd76
Author: dream21th <dream21th@126.com>
Date: Wed Jul 5 15:12:08 2023 +0800111# 第十一步:回到第一次提交的暂存区
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git reset --soft ead12928cf17629e451f4697505c681ad8d2cd76# 第十二步:查看版本状态
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git status
On branch master
Changes to be committed:(use "git restore --staged <file>..." to unstage)modified: abc.txtdream21th@dream21th MINGW64 /d/home/git-study (master)
$ cat abc.txt
v1
v2# 第十三步:回退到工作区修改内容(未暂存)
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git reset HEAD abc.txt
Unstaged changes after reset:
M abc.txtdream21th@dream21th MINGW64 /d/home/git-study (master)
$ git status
On branch master
Changes not staged for commit:(use "git add <file>..." to update what will be committed)(use "git restore <file>..." to discard changes in working directory)modified: abc.txtno changes added to commit (use "git add" and/or "git commit -a")# 第十四步:回退到工作区修改前内容
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git checkout -- abc.txt# 第十五步:查看wen'jian
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ cat abc.txt
v1
GIT
新建分支
# 第一步: 查看当前有的分支
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git branch -a
* master# 第二步: 基于master新建dev分支
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git branch dev# 第三步: 切换到dev分支
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git checkout dev
Switched to branch 'dev'dream21th@dream21th MINGW64 /d/home/git-study (dev)
$ git status
On branch dev
nothing to commit, working tree clean# 第四步: 修改abc.txt文件
dream21th@dream21th MINGW64 /d/home/git-study (dev)
$ vim abc.txtdream21th@dream21th MINGW64 /d/home/git-study (dev)
$ git status
On branch dev
Changes not staged for commit:(use "git add <file>..." to update what will be committed)(use "git restore <file>..." to discard changes in working directory)modified: abc.txtno changes added to commit (use "git add" and/or "git commit -a")# 第五步: 加入暂存区
dream21th@dream21th MINGW64 /d/home/git-study (dev)
$ git add .# 第六步: 加入版本库
dream21th@dream21th MINGW64 /d/home/git-study (dev)
$ git commit -m "v3"
[dev 4e55aa6] v31 file changed, 1 insertion(+)dream21th@dream21th MINGW64 /d/home/git-study (dev)
$ git status
On branch dev
nothing to commit, working tree cleandream21th@dream21th MINGW64 /d/home/git-study (dev)
$ git log
commit 4e55aa657fd871b787cb384bc7086813dc87ac55 (HEAD -> dev)
Author: dream21th <dream21th@126.com>
Date: Wed Jul 5 17:10:03 2023 +0800v3commit ead12928cf17629e451f4697505c681ad8d2cd76 (master)
Author: dream21th <dream21th@126.com>
Date: Wed Jul 5 15:12:08 2023 +0800111commit df3b201b621dfc7b3b256a8d5a51cd229bb4f1fe
Author: dream21th <dream21th@126.com>
Date: Wed Jul 5 13:57:13 2023 +0800v3commit 1c7b174b6574698d880bc18efe38a7457cb2bd77
Author: dream21th <dream21th@126.com>
Date: Wed Jul 5 12:01:51 2023 +0800v2commit 986868cdd81c46867a700d63b79f494c3a97065e
Author: dream21th <dream21th@126.com>
Date: Wed Jul 5 11:59:30 2023 +0800v1
除了上面采用git branch
的方式创建分支,还可以采用git checkout -b 分支名
创建一个分支并切换到新建的分支。
dream21th@dream21th MINGW64 /d/home/git-study (dev)
$ git checkout -b bug_fix
Switched to a new branch 'bug_fix'dream21th@dream21th MINGW64 /d/home/git-study (bug_fix)
GIT
合并分支
# 第一步: 切换到要合并的分支
dream21th@dream21th MINGW64 /d/home/git-study (bug_fix)
$ git checkout master
Switched to branch 'master'# 第二步: 将dev分支合并到master
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git merge dev
Updating ead1292..4e55aa6
Fast-forwardabc.txt | 1 +1 file changed, 1 insertion(+)# 第三步:查看合并后的内容
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ cat abc.txt
v1
v3dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git log
commit 4e55aa657fd871b787cb384bc7086813dc87ac55 (HEAD -> master, dev, bug_fix)
Author: dream21th <dream21th@126.com>
Date: Wed Jul 5 17:10:03 2023 +0800v3commit ead12928cf17629e451f4697505c681ad8d2cd76
Author: dream21th <dream21th@126.com>
Date: Wed Jul 5 15:12:08 2023 +0800111commit df3b201b621dfc7b3b256a8d5a51cd229bb4f1fe
Author: dream21th <dream21th@126.com>
Date: Wed Jul 5 13:57:13 2023 +0800v3commit 1c7b174b6574698d880bc18efe38a7457cb2bd77
Author: dream21th <dream21th@126.com>
Date: Wed Jul 5 12:01:51 2023 +0800v2commit 986868cdd81c46867a700d63b79f494c3a97065e
Author: dream21th <dream21th@126.com>
Date: Wed Jul 5 11:59:30 2023 +0800v1
GIT
删除分支
# 删除分支bug_fix
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git branch -d bug_fix
Deleted branch bug_fix (was 4e55aa6).dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git branch -adev
* master
码云上创建项目
码云的官网地址:https://gitee.com/
,登录成功后进行下面操作:
点击创建之后,会出现下面界面,可以采用下面的方法将本地代码上传到远程。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KKPsYCfP-1688630161959)(D:\developsoftware\mayun\note\study-note\image\image-20230706095205520.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TprLMrwG-1688630161960)
GIT
变基:git rebase
合并提交记录
# 第一步: 新建一个目录
dream21th@dream21th MINGW64 /d/home
$ mkdir git-study-onedream21th@dream21th MINGW64 /d/home
$ cd git-study-one/# 第二步:添加版本控制
dream21th@dream21th MINGW64 /d/home/git-study-one
$ git init
Initialized empty Git repository in D:/home/git-study-one/.git/# 第三步:创建文件并加入版本库,标记为v1.1
dream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ touch a.txtdream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ git add .dream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ git commit -m "v1.1"
[master (root-commit) 09c30f6] v1.11 file changed, 0 insertions(+), 0 deletions(-)create mode 100644 a.txt# 第四步:创建文件并加入版本库,标记为v1.2
dream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ touch b.txtdream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ git add .dream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ git commit -m "v1.2"
[master efcf537] v1.21 file changed, 0 insertions(+), 0 deletions(-)create mode 100644 b.txt# 第五步:创建文件并加入版本库,标记为v1.3
dream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ touch c.txtdream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ git add .dream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ git commit -m "v1.3"
[master 159bd65] v1.31 file changed, 0 insertions(+), 0 deletions(-)create mode 100644 c.txt# 第六步:创建文件并加入版本库,标记为v1.4
dream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ touch d.txtdream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ git add .dream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ git commit -m "v1.4"
[master 822f1d7] v1.41 file changed, 0 insertions(+), 0 deletions(-)create mode 100644 d.txt# 第七步:查看版本日志,发现有四条提交记录
dream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ git log
commit 822f1d743052055521f45e0712996c35a3b0232d (HEAD -> master)
Author: dream21th <dream21th@126.com>
Date: Thu Jul 6 11:01:29 2023 +0800v1.4commit 159bd65f730db0ab317fe784a554a1660f6e391f
Author: dream21th <dream21th@126.com>
Date: Thu Jul 6 11:01:09 2023 +0800v1.3commit efcf53797b6a41ed20f59fa7e1bf2d1ff28e0d84
Author: dream21th <dream21th@126.com>
Date: Thu Jul 6 11:00:54 2023 +0800v1.2commit 09c30f6113714c59fbf4665de5bd9317aa1d8215
Author: dream21th <dream21th@126.com>
Date: Thu Jul 6 11:00:30 2023 +0800v1.1# 第八步: 合并后三次提交记录(通过git rebase -i HEAD~3 数字代表合并的后面条数,也可以通过版本号合并)
dream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ git rebase -i HEAD~3
[detached HEAD 85ea9ba] 合并v1.2 到 v1.4Date: Thu Jul 6 11:00:54 2023 +08003 files changed, 0 insertions(+), 0 deletions(-)create mode 100644 b.txtcreate mode 100644 c.txtcreate mode 100644 d.txt
Successfully rebased and updated refs/heads/master.# 第九步: 查看合并后的提交记录,发现只剩两条
dream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ git log
commit 85ea9ba0e5d4389f007c6be99a339cd6c124c8d5 (HEAD -> master)
Author: dream21th <dream21th@126.com>
Date: Thu Jul 6 11:00:54 2023 +0800合并v1.2 到 v1.4commit 09c30f6113714c59fbf4665de5bd9317aa1d8215
Author: dream21th <dream21th@126.com>
Date: Thu Jul 6 11:00:30 2023 +0800v1.1dream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ ls
a.txt b.txt c.txt d.txt
注意:尽量不要把已经推到远程的版本合并。
GIT
变基:git rebase
简化工作流
dream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ git branch
* master# 第一步: 新建一个分支dev,并切换到dev分支
dream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ git checkout -b dev
Switched to a new branch 'dev'# 第二步: 创建一个文件并加入版本控制
dream21th@dream21th MINGW64 /d/home/git-study-one (dev)
$ touch f.txtdream21th@dream21th MINGW64 /d/home/git-study-one (dev)
$ git add .dream21th@dream21th MINGW64 /d/home/git-study-one (dev)
$ git commit -m "f.txt"
[dev bea1b84] f.txt1 file changed, 0 insertions(+), 0 deletions(-)create mode 100644 f.txt# 第三步: 切换到master分支
dream21th@dream21th MINGW64 /d/home/git-study-one (dev)
$ git checkout master
Switched to branch 'master'# 第四步: 创建一个文件并加入版本控制
dream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ touch g.txtdream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ git add .dream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ git commit -m 'g.txt'
[master d600c5c] g.txt1 file changed, 0 insertions(+), 0 deletions(-)create mode 100644 g.txt# 查看版本控制记录
dream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ git log
commit d600c5cfb4d34f8b1dc44a9555d02369df6122b2 (HEAD -> master)
Author: dream21th <dream21th@126.com>
Date: Thu Jul 6 13:31:04 2023 +0800g.txtcommit 85ea9ba0e5d4389f007c6be99a339cd6c124c8d5
Author: dream21th <dream21th@126.com>
Date: Thu Jul 6 11:00:54 2023 +0800合并v1.2 到 v1.4commit 09c30f6113714c59fbf4665de5bd9317aa1d8215
Author: dream21th <dream21th@126.com>
Date: Thu Jul 6 11:00:30 2023 +0800v1.1# 图形化的方式查看版本日志
dream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ git log --graph
* commit d600c5cfb4d34f8b1dc44a9555d02369df6122b2 (HEAD -> master)
| Author: dream21th <dream21th@126.com>
| Date: Thu Jul 6 13:31:04 2023 +0800
|
| g.txt
|
* commit 85ea9ba0e5d4389f007c6be99a339cd6c124c8d5
| Author: dream21th <dream21th@126.com>
| Date: Thu Jul 6 11:00:54 2023 +0800
|
| 合并v1.2 到 v1.4
|
* commit 09c30f6113714c59fbf4665de5bd9317aa1d8215Author: dream21th <dream21th@126.com>Date: Thu Jul 6 11:00:30 2023 +0800v1.1# 第五步: 在master分支合并dev分支
dream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ git merge dev
Merge made by the 'ort' strategy.f.txt | 01 file changed, 0 insertions(+), 0 deletions(-)create mode 100644 f.txt# 图形化的方式查看版本日志,看到后面有两条分叉线
dream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ git log --graph
* commit e53ba9a679b8c292f7ee0d54dd9d56d02eb630e9 (HEAD -> master)
|\ Merge: d600c5c bea1b84
| | Author: dream21th <dream21th@126.com>
| | Date: Thu Jul 6 13:31:29 2023 +0800
| |
| | Merge branch 'dev'
| |
| * commit bea1b84e2ea85bb4d2fdb32ae9764104510a9e38 (dev)
| | Author: dream21th <dream21th@126.com>
| | Date: Thu Jul 6 13:30:36 2023 +0800
| |
| | f.txt
| |
* | commit d600c5cfb4d34f8b1dc44a9555d02369df6122b2
|/ Author: dream21th <dream21th@126.com>
| Date: Thu Jul 6 13:31:04 2023 +0800
|
| g.txt
|
* commit 85ea9ba0e5d4389f007c6be99a339cd6c124c8d5
| Author: dream21th <dream21th@126.com>
| Date: Thu Jul 6 11:00:54 2023 +0800
|
| 合并v1.2 到 v1.4
|
* commit 09c30f6113714c59fbf4665de5bd9317aa1d8215Author: dream21th <dream21th@126.com>Date: Thu Jul 6 11:00:30 2023 +0800v1.1
# 回到dev分支合并master的代码,后面展示通过git rebase的方式减少交叉线
dream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ git checkout dev
Switched to branch 'dev'dream21th@dream21th MINGW64 /d/home/git-study-one (dev)
$ git merge mster
merge: mster - not something we can mergedream21th@dream21th MINGW64 /d/home/git-study-one (dev)
$ git merge master
Updating bea1b84..e53ba9a
Fast-forwardg.txt | 01 file changed, 0 insertions(+), 0 deletions(-)create mode 100644 g.txtdream21th@dream21th MINGW64 /d/home/git-study-one (dev)
$ git log --graph
* commit e53ba9a679b8c292f7ee0d54dd9d56d02eb630e9 (HEAD -> dev, master)
|\ Merge: d600c5c bea1b84
| | Author: dream21th <dream21th@126.com>
| | Date: Thu Jul 6 13:31:29 2023 +0800
| |
| | Merge branch 'dev'
| |
| * commit bea1b84e2ea85bb4d2fdb32ae9764104510a9e38
| | Author: dream21th <dream21th@126.com>
| | Date: Thu Jul 6 13:30:36 2023 +0800
| |
| | f.txt
| |
* | commit d600c5cfb4d34f8b1dc44a9555d02369df6122b2
|/ Author: dream21th <dream21th@126.com>
| Date: Thu Jul 6 13:31:04 2023 +0800
|
| g.txt
|
* commit 85ea9ba0e5d4389f007c6be99a339cd6c124c8d5
| Author: dream21th <dream21th@126.com>
| Date: Thu Jul 6 11:00:54 2023 +0800
|
| 合并v1.2 到 v1.4
|
* commit 09c30f6113714c59fbf4665de5bd9317aa1d8215Author: dream21th <dream21th@126.com>Date: Thu Jul 6 11:00:30 2023 +0800v1.1
===============================================================
#下面演示通过git rebase的方式合并分叉
# 第一步: 创建h.txt文件并加入版本控制
dream21th@dream21th MINGW64 /d/home/git-study-one (dev)
$ touch h.txtdream21th@dream21th MINGW64 /d/home/git-study-one (dev)
$ git add .dream21th@dream21th MINGW64 /d/home/git-study-one (dev)
$ git commit -m "h.txt"
[dev 6167254] h.txt1 file changed, 0 insertions(+), 0 deletions(-)create mode 100644 h.txt# 第二步:切换到master分支创建i.txt文件并加入版本控制
dream21th@dream21th MINGW64 /d/home/git-study-one (dev)
$ git checkout master
Switched to branch 'master'dream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ touch i.txtdream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ git add .dream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ git commit -m "i.txt"
[master 92d1b47] i.txt1 file changed, 0 insertions(+), 0 deletions(-)create mode 100644 i.txtdream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ git checkout dev
Switched to branch 'dev'# 第三步:切换到dev分支,rebase分支master
dream21th@dream21th MINGW64 /d/home/git-study-one (dev)
$ git rebase master
Successfully rebased and updated refs/heads/dev.dream21th@dream21th MINGW64 /d/home/git-study-one (dev)
$ git status
On branch dev
nothing to commit, working tree clean# 第四步: 切换回master分支,merge分支dev
dream21th@dream21th MINGW64 /d/home/git-study-one (dev)
$ git checkout master
Switched to branch 'master'dream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ git merge dev
Updating 92d1b47..398a705
Fast-forwardh.txt | 01 file changed, 0 insertions(+), 0 deletions(-)create mode 100644 h.txt# 第五步:通过git rebase查看发现后面只有一条线
dream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ git log --graph
* commit 398a705fdfe4d7e23934505268a736f3247ced87 (HEAD -> master, dev)
| Author: dream21th <dream21th@126.com>
| Date: Thu Jul 6 13:32:46 2023 +0800
|
| h.txt
|
* commit 92d1b474c1f6df90c8495a9430895beefb2f1bcd
| Author: dream21th <dream21th@126.com>
| Date: Thu Jul 6 13:33:17 2023 +0800
|
| i.txt
|
* commit e53ba9a679b8c292f7ee0d54dd9d56d02eb630e9
|\ Merge: d600c5c bea1b84
| | Author: dream21th <dream21th@126.com>
| | Date: Thu Jul 6 13:31:29 2023 +0800
| |
| | Merge branch 'dev'
| |
| * commit bea1b84e2ea85bb4d2fdb32ae9764104510a9e38
| | Author: dream21th <dream21th@126.com>
| | Date: Thu Jul 6 13:30:36 2023 +0800
| |
| | f.txt
| |
* | commit d600c5cfb4d34f8b1dc44a9555d02369df6122b2
|/ Author: dream21th <dream21th@126.com>
| Date: Thu Jul 6 13:31:04 2023 +0800
|
| g.txt
|
* commit 85ea9ba0e5d4389f007c6be99a339cd6c124c8d5
| Author: dream21th <dream21th@126.com>
| Date: Thu Jul 6 11:00:54 2023 +0800
|
| 合并v1.2 到 v1.4
|
* commit 09c30f6113714c59fbf4665de5bd9317aa1d8215Author: dream21th <dream21th@126.com>Date: Thu Jul 6 11:00:30 2023 +0800v1.1
# 简化日志图形化输出
dream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ git log --graph --pretty=format:"%h %s"
* 398a705 h.txt
* 92d1b47 i.txt
* e53ba9a Merge branch 'dev'
|\
| * bea1b84 f.txt
* | d600c5c g.txt
|/
* 85ea9ba 合并v1.2 到 v1.4
* 09c30f6 v1.1
GIT
变基:git rebase
合并代码
# 模拟在本地环境一提交代码到远程
dream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ touch y.txtdream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ git add .dream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ git commit -m "y.txt"
[master e9d6ee5] y.txt1 file changed, 0 insertions(+), 0 deletions(-)create mode 100644 y.txtdream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ git push
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 16 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 222 bytes | 222.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To https://gitee.com/dream21th/git_study.git398a705..e9d6ee5 master -> master
在本地环境二先做修改代码,然后提交版本库,在更新远程代码,更新完成后看到本地代码有分叉。
dream21th@dream21th MINGW64 /d/home/git-study-two/git_study (master)
$ touch x.txtdream21th@dream21th MINGW64 /d/home/git-study-two/git_study (master)
$ git add .dream21th@dream21th MINGW64 /d/home/git-study-two/git_study (master)
$ git commit -m 'x.txt'
[master e1b0e87] x.txt1 file changed, 0 insertions(+), 0 deletions(-)create mode 100644 x.txtdream21th@dream21th MINGW64 /d/home/git-study-two/git_study (master)
$ git pull
Already up to date.dream21th@dream21th MINGW64 /d/home/git-study-two/git_study (master)
$ git pull
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 2 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (2/2), 202 bytes | 25.00 KiB/s, done.
From https://gitee.com/dream21th/git_study398a705..e9d6ee5 master -> origin/master
Merge made by the 'ort' strategy.y.txt | 01 file changed, 0 insertions(+), 0 deletions(-)create mode 100644 y.txtdream21th@dream21th MINGW64 /d/home/git-study-two/git_study (master)
$ git log --graph
* commit d9aba5c612a8a7dd5e23c61f7785f29588bcdb3d (HEAD -> master)
|\ Merge: e1b0e87 e9d6ee5
| | Author: dream21th <dream21th@126.com>
| | Date: Thu Jul 6 13:58:34 2023 +0800
| |
| | Merge branch 'master' of https://gitee.com/dream21th/git_study
| |
| * commit e9d6ee5159adc051450d36c20a6e61f0be3cb950 (origin/master, origin/HEAD)
| | Author: dream21th <dream21th@126.com>
| | Date: Thu Jul 6 13:58:10 2023 +0800
| |
| | y.txt
| |
* | commit e1b0e871154e50f874d249213446288c292bc86e
|/ Author: dream21th <dream21th@126.com>
| Date: Thu Jul 6 13:57:38 2023 +0800
|
| x.txt
通过git fetch
和git rebase
的操作方式减少分叉:
dream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ touch xx.txtdream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ git add .dream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ git commit -m "xx.txt"
[master 676791f] xx.txt1 file changed, 0 insertions(+), 0 deletions(-)create mode 100644 xx.txtdream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ git push
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 16 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 224 bytes | 224.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To https://gitee.com/dream21th/git_study.gitd9aba5c..676791f master -> master
dream21th@dream21th MINGW64 /d/home/git-study-two/git_study (master)
$ touch xy.txtdream21th@dream21th MINGW64 /d/home/git-study-two/git_study (master)
$ git add .dream21th@dream21th MINGW64 /d/home/git-study-two/git_study (master)
$ gt commit -m "xy.txt"
bash: gt: command not founddream21th@dream21th MINGW64 /d/home/git-study-two/git_study (master)
$ git commit -m "xy.txt"
[master a6bbb8a] xy.txt1 file changed, 0 insertions(+), 0 deletions(-)create mode 100644 xy.txtdream21th@dream21th MINGW64 /d/home/git-study-two/git_study (master)
$ git fetch
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 2 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (2/2), 204 bytes | 68.00 KiB/s, done.
From https://gitee.com/dream21th/git_studyd9aba5c..676791f master -> origin/masterdream21th@dream21th MINGW64 /d/home/git-study-two/git_study (master)
$ git rebase origin/master
Successfully rebased and updated refs/heads/master.dream21th@dream21th MINGW64 /d/home/git-study-two/git_study (master)
$ git log --graph
* commit 3fbfdf8919825508c96fbba54bf8536856f50fc9 (HEAD -> master)
| Author: dream21th <dream21th@126.com>
| Date: Thu Jul 6 14:04:33 2023 +0800
|
| xy.txt
|
* commit 676791fd5eb1639b3d333907110d65b3018cc60b (origin/master, origin/HEAD)
| Author: dream21th <dream21th@126.com>
| Date: Thu Jul 6 14:04:01 2023 +0800
|
| xx.txt
|