git 重写提交记录的手段

引自

https://www.atlassian.com/git/tutorials/rewriting-history

git commit --amend

这个命令可以快速弥补第一次提交后发现有遗漏,然后补充的情况,这个命令不会创建新的commit,而是将之前的commit替换为新的

git rebase -i

Commands:
p, pick = use commit
r, reword = use commit, but edit the commit message
e, edit = use commit, but stop for amending
s, squash = use commit, but meld into previous commit

用上面几个就够了,还有下面的参数,没试过
f, fixup = like “squash”, but discard this commit’s log message
x, exec = run command (the rest of the line) using shell
d, drop = remove commit
l, label = label current HEAD with a name
t, reset = reset HEAD to a label
m, merge [-C | -c ] [# ]

git revert HEAD

撤销更改

一种特殊情况是,当前提交是merge几个分支出来的,这种情况回退不知道回退到哪一个分支上,这种情况,需要通过 git log 查看当前提交合并自那几个提交,从左自右依次为1,2,3…

commit 47d4XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX50
Merge: f8acXX5 243eXX3
Author: someone
Date:   Fri Apr 19 13:00:46 2019 +0800

确认后,运行git revert HEAD -m 1就可以回滚到指定分支的提交 f8acXX5。

checkout VS reset VS revert

引自:https://www.atlassian.com/git/tutorials/resetting-checking-out-and-reverting

Command Scope Common use cases
git reset Commit-level Discard commits in a private branch or throw away uncommited changes
git reset File-level Unstage a file
git checkout Commit-level Switch between branches or inspect old snapshots
git checkout File-level Discard changes in the working directory
git revert Commit-level Undo commits in a public branch
git revert File-level (N/A)

你可能感兴趣的:(git)