查看提交历史
然后在此项目中运行 git log
,应该会看到下面的输出:

合并commit 信息
我们需要将 2dfbc7e8
和 c4e858b5
合并成一个 commit,那么我们输入如下命令

其中,-i
的参数是不需要合并的 commit 的 hash 值,这里指的是第一条 commit, 接着我们就进入到 vi
的编辑模式

可以看到其中分为两个部分,上方未注释的部分是填写要执行的指令,而下方注释的部分则是指令的提示说明。指令部分中由前方的命令名称、commit hash 和 commit message 组成。
当前我们只要知道 pick
和 squash
这两个命令即可。
-
pick
的意思是要会执行这个 commit -
squash
的意思是这个 commit 会被合并到前一个commit
我们将 c4e858b5
这个 commit 前方的命令改成 squash
或 s
,然后输入:wq
以保存并退出

这是我们会看到 commit message 的编辑界面

其中, 非注释部分就是两次的 commit message, 你要做的就是将这两个修改成新的 commit message。

输入wq
保存并推出, 再次输入git log
查看 commit 历史信息,你会发现这两个 commit 已经合并了。

注意事项:如果这个过程中有操作错误,可以使用 git rebase --abort
来撤销修改,回到没有开始操作合并之前的状态。
看某次commit改了哪些文件
git log 查看commit的历史
git show <commit-hash-id>查看某次commit的修改内容
git log -p <filename>查看某个文件的修改历史
git log -p -2查看最近2次的更新内容
相关log 命令:GIT log命令全解析
对比两个commit 之间的差异
git diff commit-id-1 commit-id-2 > d:/diff.txt
结果文件diff.txt中:
"-"号开头的表示 commit-id-2 相对 commit-id-1 减少了的内容。
"+"号开头的表示 commit-id-2 相对 commit-id-1 增加了的内容。
查看所有操作记录
git reflog 可以查看所有分支的所有操作记录(包括commit和reset的操作),包括已经被删除的commit记录(版本回退记录),git log 则不能察看已经删除了的commit记录
对已经存在的commit 进行 再次提交
git cherry-pick <commit id> #git reset --hard 退掉的提交也可以找回来
查看历史版本记录--指定显示条数
git reflog -n
git reset --hard 63ee781 #想回复到那个位置改写 版本号或日志标识即可
commit 修改描述
提交修改的代码(只是提交到本地的代码库,不会推送到服务器)
git commit -am '修改说明'
如果觉得刚提交的“修改说明”写得不够好,前提是没有push到远程分支,可输入以下命令调整
git commit --amend -m "your new message"
修改历史提交描述
git rebase -i HEAD~3 #表示要修改当前版本的倒数第三次状态:
git rebase -i master~1 #最后一次
git rebase -i master~5 #最后五次
git rebase -i HEAD~3 #当前版本的倒数第三次状态
git rebase -i 32e0a87f #指定的 commit_id 位置
git rebase -i 修改历史提交 这个方法就是有冲突不断,解决起来比较麻烦
$ git status interactive rebase in progress; onto 93f7a28 Last commands done (2 commands done): pick 794a68f js 常用插件 pick a9ce019 修改 README.md Next commands to do (14 remaining commands): pick 7d91899 修改 README.md pick 70bf6af edit css3.js file (use "git rebase --edit-todo" to view and edit) You are currently rebasing branch 'devPhp' on '93f7a28'. (fix conflicts and then run "git rebase --continue") (use "git rebase --skip" to skip this patch) (use "git rebase --abort" to check out the original branch) Unmerged paths: (use "git reset HEAD <file>..." to unstage) (use "git add <file>..." to mark resolution) both modified: README.md no changes added to commit (use "git add" and/or "git commit -a")
简洁步骤(从网上查找的未亲测):
git rebase -i HEAD~3
这个命令出来之后,会出来三行东东:
pick:*******
pick:*******
pick:*******
如果你要修改哪个,就把那行的pick改成edit,然后退出。
这时通过git log你可以发现,git的最后一次提交已经变成你选的那个了,这时再使用:
git commit --amend 来对commit进行修改,
修改完成后使用 git rebase --continue