git reflog 查看当前仓库的所有操作日志
$ git reflog
d16f2f4 HEAD@{7}: commit (amend): Merge branch "fix-B"
b110cfe HEAD@{8}: commit (merge): Fix conflict
d8b19ab HEAD@{9}: checkout: moving from fix-B to master
8340da0 HEAD@{10}: checkout: moving from master to fix-B
d8b19ab HEAD@{11}: checkout: moving from feature-A to master
04f1afd HEAD@{12}: checkout: moving from fix-B to feature-A
8340da0 HEAD@{13}: reset: moving to 8340da0
d8b19ab HEAD@{14}: checkout: moving from master to fix-B
d8b19ab HEAD@{15}: reset: moving to d8b19ab
9ba9239 HEAD@{16}: checkout: moving from fix-B to master
d8b19ab HEAD@{17}: reset: moving to d8b19ab
8340da0 HEAD@{18}: commit: Fix B
9ba9239 HEAD@{19}: checkout: moving from fix_b to fix-B
9ba9239 HEAD@{20}: checkout: moving from master to fix_b
9ba9239 HEAD@{21}: reset: moving to 9ba9239561b2ba36c7b2be2c2378aecc09ca8db8
d8b19ab HEAD@{22}: merge feature-A: Merge made by the 'recursive' strategy.
9ba9239 HEAD@{23}: checkout: moving from feature-A to master
04f1afd HEAD@{24}: checkout: moving from master to feature-A
9ba9239 HEAD@{25}: checkout: moving from feature-A to master
04f1afd HEAD@{26}: commit: Add feature-A
9ba9239 HEAD@{27}: checkout: moving from master to feature-A
9ba9239 HEAD@{28}: commit: Add index
b1c8e4f HEAD@{29}: commit: commit README file
6e5f9c7 HEAD@{30}: commit (initial): First commit
- reflog git log 命令只能查看以当前状态为终点的历史日志,使用 git reflog 可以查看所有的历史操作日志。
git reset 回溯历史版本
$ git reset --hard 8340da0
HEAD is now at 8340da0 Fix B
- --hard 要让仓库的HEAD、暂存区、当前工作树回溯到指定状态,需要通过 git reset --hard 命令进行操作。
消除冲突
- 在分支合并的过程中,会出现文件冲突的情况,这个时候需要我们手动消除冲突:
$ git merge --no-ff fix-B
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
- 打开冲突的文件进行查看:
$ vim README.md
# Git 教程
<<<<<<< HEAD
-feature-A
=======
-fix-B
>>>>>>> fix-B
- ======= 以上的部分是当前HEAD的内容,以下的部分是需要合并的内容,我们手工将其修复:
# Git 教程
-feature-A
-fix-B
- 修复了冲突后,提交文件即可:
$ git add README.md
$ git commit -m "fix conflict"
[master c03e2d7] fix conflict
git commit --amend 修改上一条提交的信息
- 当我们提交的信息有误,需要修改的时候,可以使用 git commit --amend 命令:
$ git commit --amend
编辑器会打开并显示上一条修改信息:
fix conflict
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Tue May 16 16:26:47 2017 +0800
#
# On branch master
# Changes to be committed:
# modified: README.md
- 修改了并保存编译器中的内容,即可完成修改:
[master dfa52fb] Merge branch 'fix-B'
Date: Tue May 16 16:26:47 2017 +0800
- 执行 git log --graph,可以看到历史中的对应记录也被修改为最新的内容:
$ git log --graph
\* commit dfa52fbc655efe39c94fe8a9cda814ee8cc4d054
|\ Merge: 96d8c7b 046d2e2
| | Author: JannyHo <fung.w.chan@hotmail.com>
| | Date: Tue May 16 16:26:47 2017 +0800
| |
| | Merge branch 'fix-B'
| |
| * commit 046d2e2f63c86b3431dc3cc28775d567f583c447
| | Author: JannyHo <fung.w.chan@hotmail.com>
| | Date: Tue May 16 16:16:00 2017 +0800
| |
| | fix-B
| |
\* | commit 96d8c7bca7dbbdfa012f4977696f76994d1c3d59
|\ \ Merge: 2561af2 186162c
| |/ Author: JannyHo <fung.w.chan@hotmail.com>
|/| Date: Tue May 16 16:16:27 2017 +0800
| |
| | Merge branch 'feature-A'
| |
| * commit 186162c5fef8eff5a2c79d3f7d5bb4155a4d72e1
|/ Author: JannyHo <fung.w.chan@hotmail.com>
| Date: Tue May 16 16:11:14 2017 +0800
|
| feature-A
|
\* commit 2561af291bb9b1065c5bb15122cc0739fbb4d8e8
Author: JannyHo <fung.w.chan@hotmail.com>
Date: Tue May 16 16:09:58 2017 +0800
First commit
git rebase -i 压缩历史
- 在合并特性分支前,如果发现已提交的内容在有错误,可以提交一个修改,然后将这个修改合并到前一个提交中压缩成一个历史记录:
我们先创建一个新的特性分支 feature-C:
$ git checkout -b feature-C
Switched to a new branch 'feature-C'
我们在 feature-C 中修改了 README.md,在此处有一个拼写错误:
> # Git 教程
>
> - feature-A
> - fix-B
> - faeture-C
我们先提交这些修改(git add 和 git commit -m 可以简写为 git commit -am):
$ git commit -am "Add feature-C"
[feature-C e015d1c] Add feature-C
1 file changed, 1 insertion(+)
我们把 README.md 中的 faeture-C 改为拼写正确的 feature-C,并查看一下修改的内容:
> $ git diff
> diff --git a/README.md b/README.md
> index 4d32145..1c9d4ee 100644
> --- a/README.md
> +++ b/README.md
> @@ -2,4 +2,4 @@
>
> - feature-A
> - fix-B
> -- faeture-C
> +- feature-C
我们提交这些修改:
$ git commit -am "Fix typo"
[feature-C dd18150] Fix typo
1 file changed, 1 insertion(+), 1 deletion(-)
这种小修改在历史记录中没有意义,所以我们要想办法把它去掉:
未完待续
网友评论