分支进阶与版本回退

fast-forward

  • 如果可能,合并分支时Git会使用fast-forward模式
  • 在这种模式下,删除分支时会丢掉分支信息
  • 合并时加上--no-ff参数会禁用fast-forward,这样会多出一个commit id

    • git merge --no-ff
  • 查看log,以图形化的方式

    • git log --graph

不使用ff.png

这样dev虽然在合并后也是在分支上的,而且master并不会往前前进一个位子,而是直接commit一次提交记录,再与dev进行合并

尝试--no-ff参数

首先我们查看一下当前masterdevgit log,对比一下,会发现两个的git log因为上一章的合并导致完全一样,然后我们切换到dev修改一下文件,并且提交:

$ git status
On branch dev
nothing to commit, working tree clean

A@DESKTOP-6DP8MG1 MINGW64 /e/Gitfile (dev)
$ cat test.txt
I am YQHP-YuKi
hello everyone
this is master
this is dev again

这时候我们再切回到master,使用git merge --no-ff dev试试:

出现了这个vim界面,显示出一条提交信息

Merge branch 'dev' into master
# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
$ git merge --no-ff dev
Merge made by the 'recursive' strategy.
 test.txt | 2 ++
 1 file changed, 2 insertions(+)

这时候我们再查看一下mastergit log,与之前devgit log比较一下:

经过git merge --no-ffmaster:

commit 534d7bb70f5e95ba74af17c252e8c14d27ce16d1 (HEAD -> master)
Merge: a41fd9e 74bee32
Author: YQHP-YuKi <**********@qq.com>
Date:   Fri Nov 20 17:37:35 2020 +0800

    Merge branch 'dev' into master

commit 74bee3236352ae346395dca7f3de1675df548a0c (dev)
Author: YQHP-YuKi <**********@qq.com>
Date:   Fri Nov 20 17:22:23 2020 +0800

    this is dev again

commit a41fd9e529c9a9e0b746745beae653b6736dac46
Merge: 6162e11 0a3a478
Author: YQHP-YuKi <**********@qq.com>
Date:   Thu Nov 19 15:42:10 2020 +0800

    Merge branch 'dev' into master
commit 74bee3236352ae346395dca7f3de1675df548a0c (HEAD -> dev)
Author: YQHP-YuKi <1465722762@qq.com>
Date:   Fri Nov 20 17:22:23 2020 +0800

    this is dev again


commit a41fd9e529c9a9e0b746745beae653b6736dac46 (HEAD -> dev, master)
Merge: 6162e11 0a3a478
Author: YQHP-YuKi <**********@qq.com>
Date:   Thu Nov 19 15:42:10 2020 +0800

    Merge branch 'dev' into master

commit 0a3a478dc18d6b39a53e7ad3cbbecdda0531071a
Author: YQHP-YuKi <***********@qq.com>
Date:   Thu Nov 19 15:25:09 2020 +0800

    this is dev in test.txt

commit 6162e11b99c56f3e2e0f8c8bce933bbaa7081f26
Author: YQHP-YuKi <***********@qq.com>
Date:   Thu Nov 19 15:22:15 2020 +0800

    this is master in test.txt

可以发现master的第一条是一条单独多出来的commit历史记录,如果使用fast-forward的话,masterlog一个会与devlog一样,我们还可以使用git log --graph查看:

*   commit 534d7bb70f5e95ba74af17c252e8c14d27ce16d1 (HEAD -> master)
|\  Merge: a41fd9e 74bee32
| | Author: YQHP-YuKi <***********@qq.com>
| | Date:   Fri Nov 20 17:37:35 2020 +0800
| |
| |     Merge branch 'dev' into master
| |
| * commit 74bee3236352ae346395dca7f3de1675df548a0c (dev)
|/  Author: YQHP-YuKi <***********@qq.com>
|   Date:   Fri Nov 20 17:22:23 2020 +0800
|
|       this is dev again
|
*   commit a41fd9e529c9a9e0b746745beae653b6736dac46
|\  Merge: 6162e11 0a3a478
| | Author: YQHP-YuKi <*********@qq.com>
| | Date:   Thu Nov 19 15:42:10 2020 +0800
| |
| |     Merge branch 'dev' into master
| |
| * commit 0a3a478dc18d6b39a53e7ad3cbbecdda0531071a
| | Author: YQHP-YuKi <**********@qq.com>
| | Date:   Thu Nov 19 15:25:09 2020 +0800

可以看到原本使用fast-forward就应该在dev那就停住了,使用--no-ff就多了第一条commit信息

版本回退

我们删除先前的Gitfile,重新创建一个git厂库:

$ rm -rf *
$ cd ..
$ rm -rf Gitfile
$ git init
Initialized empty Git repository in E:/Gitfile/.git/

这时候创建一个test1.txt,向里面分4次添加4条信息,分4次提交:

$ cat test1.txt
hello
world
hello world
hello hello hello
$ git log --pretty=oneline --abbrev-commit
3573a23 (HEAD -> master) 3hello in test1.txt
b38e36d hello world in test1.txt
9ed5c23 world in test1.txt
61adf63 hello test1.txt

这时候我们想回退到第3次提交,也就是hello world那次:

Git回退版本

  • 回退到上一版本

    • git reset --hard HEAD^
    • git reset --hard HEAD~1
  • 返回到某一版本

hard HEAD^

我们想回退到上一版本,使用这条命令:

git reset --hard HEAD^
$ git reset --hard HEAD^
HEAD is now at b38e36d hello world in test1.txt

想再回退2个版本:

$ git reset --hard HEAD^^
HEAD is now at 61adf63 hello test1.txt

这时候用git log查看一下:

$ git log
commit 61adf63007461e6e5d79b19aa0ff09ff61033d76 (HEAD -> master)
Author: YQHP-YuKi <************@qq.com>
Date:   Sun Nov 22 15:53:29 2020 +0800

    hello test1.txt

如果我们这时候还是想回到第4次提交那,可以直接后面跟版本号:

$ git reset --hard 3573a
HEAD is now at 3573a23 3hello in test1.txt
$ cat test1.txt
hello
world
hello world
hello hello hello
hard HEAD~1

如果我们想回退到前3 5次这种很前的提交,则后面可以直接跟数字:

$ git reset --hard HEAD~3
HEAD is now at 61adf63 hello test1.txt

注释:3是从你当前记录开始的前3次提交,相当于是4-3=1,是版本1,如果是~1,则是版本3

hard commit_id

我们如果想直接回到某次提交,则可以直接跟那次的commit_id:

第一次:

$ git reset --hard 61adf
HEAD is now at 61adf63 hello test1.txt

第四次:

$ git reset --hard 3573a
HEAD is now at 3573a23 3hello in test1.txt
git reflog

如果我们在版本1,想回到版本4,但又不记得版本4commit_id了,我们可以用使用这个命令查看:

$ git reflog
3573a23 (HEAD -> master) HEAD@{0}: reset: moving to 3573a23
61adf63 HEAD@{1}: reset: moving to HEAD
61adf63 HEAD@{2}: reset: moving to HEAD
61adf63 HEAD@{3}: reset: moving to HEAD
61adf63 HEAD@{4}: reset: moving to 61adf
3573a23 (HEAD -> master) HEAD@{5}: reset: moving to 3573a
61adf63 HEAD@{6}: reset: moving to 61adf
3573a23 (HEAD -> master) HEAD@{7}: reset: moving to 3573a
61adf63 HEAD@{8}: reset: moving to HEAD~3
3573a23 (HEAD -> master) HEAD@{9}: reset: moving to 3573a
b38e36d HEAD@{10}: reset: moving to HEAD~1
3573a23 (HEAD -> master) HEAD@{11}: reset: moving to 3573a
61adf63 HEAD@{12}: reset: moving to HEAD
61adf63 HEAD@{13}: reset: moving to HEAD^^
b38e36d HEAD@{14}: reset: moving to HEAD^
3573a23 (HEAD -> master) HEAD@{15}: commit: 3hello in test1.txt
b38e36d HEAD@{16}: commit: hello world in test1.txt
9ed5c23 HEAD@{17}: commit: world in test1.txt
61adf63 HEAD@{18}: commit (initial): hello test1.txt

git reflog是操作日志,这样我们就可以看到我们操作的任何版本日志的id了

Last modification:December 5th, 2020 at 01:23 pm