GIT版本控制常规性操作演示汇总

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 master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        abc.txt
        index.html

nothing 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 master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   index.html

Untracked 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 master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   abc.txt
        new file:   index.html
#第七步:通过git commit添加版本控制
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git commit -m "v1"
[master (root-commit) 986868c] v1
 2 files changed, 2 insertions(+)
 create mode 100644 abc.txt
 create mode 100644 index.html

dream21th@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.html

no 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 it

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:   index.html

dream21th@dream21th MINGW64 /d/home/git-study (master)
$ git commit -m "v2"
[master 1c7b174] v2
 1 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 +0800

    v2

commit 986868cdd81c46867a700d63b79f494c3a97065e
Author: dream21th <dream21th@126.com>
Date:   Wed Jul 5 11:59:30 2023 +0800

    v1

文件在管控台的三种颜色:红色(新增或者修改的文件),绿色(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.name

dream21th@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 +0800

    v3

commit 1c7b174b6574698d880bc18efe38a7457cb2bd77
Author: dream21th <dream21th@126.com>
Date:   Wed Jul 5 12:01:51 2023 +0800

    v2

commit 986868cdd81c46867a700d63b79f494c3a97065e
Author: dream21th <dream21th@126.com>
Date:   Wed Jul 5 11:59:30 2023 +0800

    v1

# 第二步: 假设这次要回退到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 +0800

    v2

commit 986868cdd81c46867a700d63b79f494c3a97065e
Author: dream21th <dream21th@126.com>
Date:   Wed Jul 5 11:59:30 2023 +0800

    v1

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 +0800

    v3

commit 1c7b174b6574698d880bc18efe38a7457cb2bd77
Author: dream21th <dream21th@126.com>
Date:   Wed Jul 5 12:01:51 2023 +0800

    v2

commit 986868cdd81c46867a700d63b79f494c3a97065e
Author: dream21th <dream21th@126.com>
Date:   Wed Jul 5 11:59:30 2023 +0800

    v1

GIT三大区域转换

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wdjPaLKS-1688630161959)(D:\developsoftware\mayun\note\study-note\image\image-20230705150825706.png)]

# 第一步: 编写一个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.txt

no 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] 111
 1 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.txt

no 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] v2
 1 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 +0800

    v2

commit ead12928cf17629e451f4697505c681ad8d2cd76
Author: dream21th <dream21th@126.com>
Date:   Wed Jul 5 15:12:08 2023 +0800

    111

# 第十一步:回到第一次提交的暂存区
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.txt

dream21th@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.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.txt

no 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.txt

dream21th@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.txt

no 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] v3
 1 file changed, 1 insertion(+)

dream21th@dream21th MINGW64 /d/home/git-study (dev)
$ git status
On branch dev
nothing to commit, working tree clean

dream21th@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 +0800

    v3

commit ead12928cf17629e451f4697505c681ad8d2cd76 (master)
Author: dream21th <dream21th@126.com>
Date:   Wed Jul 5 15:12:08 2023 +0800

    111

commit df3b201b621dfc7b3b256a8d5a51cd229bb4f1fe
Author: dream21th <dream21th@126.com>
Date:   Wed Jul 5 13:57:13 2023 +0800

    v3

commit 1c7b174b6574698d880bc18efe38a7457cb2bd77
Author: dream21th <dream21th@126.com>
Date:   Wed Jul 5 12:01:51 2023 +0800

    v2

commit 986868cdd81c46867a700d63b79f494c3a97065e
Author: dream21th <dream21th@126.com>
Date:   Wed Jul 5 11:59:30 2023 +0800

    v1

​ 除了上面采用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-forward
 abc.txt | 1 +
 1 file changed, 1 insertion(+)

# 第三步:查看合并后的内容
dream21th@dream21th MINGW64 /d/home/git-study (master)
$ cat abc.txt
v1
v3

dream21th@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 +0800

    v3

commit ead12928cf17629e451f4697505c681ad8d2cd76
Author: dream21th <dream21th@126.com>
Date:   Wed Jul 5 15:12:08 2023 +0800

    111

commit df3b201b621dfc7b3b256a8d5a51cd229bb4f1fe
Author: dream21th <dream21th@126.com>
Date:   Wed Jul 5 13:57:13 2023 +0800

    v3

commit 1c7b174b6574698d880bc18efe38a7457cb2bd77
Author: dream21th <dream21th@126.com>
Date:   Wed Jul 5 12:01:51 2023 +0800

    v2

commit 986868cdd81c46867a700d63b79f494c3a97065e
Author: dream21th <dream21th@126.com>
Date:   Wed Jul 5 11:59:30 2023 +0800

    v1


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 -a
  dev
* master

码云上创建项目

​ 码云的官网地址:https://gitee.com/,登录成功后进行下面操作:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-d3aQnCXr-1688630161959)(D:\developsoftware\mayun\note\study-note\image\image-20230706095029880.png)]

​ 点击创建之后,会出现下面界面,可以采用下面的方法将本地代码上传到远程。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KKPsYCfP-1688630161959)(D:\developsoftware\mayun\note\study-note\image\image-20230706095205520.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TprLMrwG-1688630161960)(D:\developsoftware\mayun\note\study-note\image\image-20230706102259473.png)]

GIT变基:git rebase合并提交记录

# 第一步: 新建一个目录
dream21th@dream21th MINGW64 /d/home
$ mkdir git-study-one

dream21th@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.txt

dream21th@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.1
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 a.txt
 
# 第四步:创建文件并加入版本库,标记为v1.2
dream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ touch b.txt

dream21th@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.2
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 b.txt

# 第五步:创建文件并加入版本库,标记为v1.3
dream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ touch c.txt

dream21th@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.3
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 c.txt

# 第六步:创建文件并加入版本库,标记为v1.4
dream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ touch d.txt

dream21th@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.4
 1 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 +0800

    v1.4

commit 159bd65f730db0ab317fe784a554a1660f6e391f
Author: dream21th <dream21th@126.com>
Date:   Thu Jul 6 11:01:09 2023 +0800

    v1.3

commit efcf53797b6a41ed20f59fa7e1bf2d1ff28e0d84
Author: dream21th <dream21th@126.com>
Date:   Thu Jul 6 11:00:54 2023 +0800

    v1.2

commit 09c30f6113714c59fbf4665de5bd9317aa1d8215
Author: dream21th <dream21th@126.com>
Date:   Thu Jul 6 11:00:30 2023 +0800

    v1.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.4
 Date: Thu Jul 6 11:00:54 2023 +0800
 3 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 b.txt
 create mode 100644 c.txt
 create 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.4

commit 09c30f6113714c59fbf4665de5bd9317aa1d8215
Author: dream21th <dream21th@126.com>
Date:   Thu Jul 6 11:00:30 2023 +0800

    v1.1

dream21th@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.txt

dream21th@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.txt
 1 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.txt

dream21th@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.txt
 1 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 +0800

    g.txt

commit 85ea9ba0e5d4389f007c6be99a339cd6c124c8d5
Author: dream21th <dream21th@126.com>
Date:   Thu Jul 6 11:00:54 2023 +0800

    合并v1.2 到 v1.4

commit 09c30f6113714c59fbf4665de5bd9317aa1d8215
Author: dream21th <dream21th@126.com>
Date:   Thu Jul 6 11:00:30 2023 +0800

    v1.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 09c30f6113714c59fbf4665de5bd9317aa1d8215
  Author: dream21th <dream21th@126.com>
  Date:   Thu Jul 6 11:00:30 2023 +0800

      v1.1

# 第五步: 在master分支合并dev分支
dream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ git merge dev
Merge made by the 'ort' strategy.
 f.txt | 0
 1 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 09c30f6113714c59fbf4665de5bd9317aa1d8215
  Author: dream21th <dream21th@126.com>
  Date:   Thu Jul 6 11:00:30 2023 +0800

      v1.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 merge

dream21th@dream21th MINGW64 /d/home/git-study-one (dev)
$ git merge master
Updating bea1b84..e53ba9a
Fast-forward
 g.txt | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 g.txt

dream21th@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 09c30f6113714c59fbf4665de5bd9317aa1d8215
  Author: dream21th <dream21th@126.com>
  Date:   Thu Jul 6 11:00:30 2023 +0800

      v1.1
===============================================================
#下面演示通过git rebase的方式合并分叉
# 第一步: 创建h.txt文件并加入版本控制
dream21th@dream21th MINGW64 /d/home/git-study-one (dev)
$ touch h.txt

dream21th@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.txt
 1 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.txt


dream21th@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.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 i.txt

dream21th@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-forward
 h.txt | 0
 1 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 09c30f6113714c59fbf4665de5bd9317aa1d8215
  Author: dream21th <dream21th@126.com>
  Date:   Thu Jul 6 11:00:30 2023 +0800

      v1.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.txt

dream21th@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.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 y.txt

dream21th@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.git
   398a705..e9d6ee5  master -> master

​ 在本地环境二先做修改代码,然后提交版本库,在更新远程代码,更新完成后看到本地代码有分叉。

dream21th@dream21th MINGW64 /d/home/git-study-two/git_study (master)
$ touch x.txt

dream21th@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.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 x.txt

dream21th@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_study
   398a705..e9d6ee5  master     -> origin/master
Merge made by the 'ort' strategy.
 y.txt | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 y.txt

dream21th@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 fetchgit rebase的操作方式减少分叉:

dream21th@dream21th MINGW64 /d/home/git-study-one (master)
$ touch xx.txt

dream21th@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.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 xx.txt

dream21th@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.git
   d9aba5c..676791f  master -> master

dream21th@dream21th MINGW64 /d/home/git-study-two/git_study (master)
$ touch xy.txt

dream21th@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 found

dream21th@dream21th MINGW64 /d/home/git-study-two/git_study (master)
$ git commit -m "xy.txt"
[master a6bbb8a] xy.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 xy.txt

dream21th@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_study
   d9aba5c..676791f  master     -> origin/master

dream21th@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
|

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dream21st

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值