git - If after committing, you find that something is not perfect and you want to improve it before committing again, how can you cancel the previous commit?
phpcn_u15822017-05-02 09:25:39
0
3
648
Or, you can commit later and merge with the previous one.
If you want to change commit message ,用 git commit -amend.
If you want to add some content, don’t touch the last commit(就叫 commit-1 吧),接着修改,然后提交一个新的 commit (commit-2)。然后 git rebase -i HEAD~~, the following content will appear:
Save the second pick 改成 squash and exit, and the following will appear:
# first commit message:
commit-1-message
# second commit message:
commit-2-message
Delete these things and change them to what you want commit messageSave and exit.
Then you can git log 查看一下,两个 commit 已经合并成为新的 commit.
You can refer to: 5. Use rebase -i to merge and submit
===
BTW, if you can only commit the last time commit 已经推到远程分支了,而且该分支有人跟你合作,那么最好还是不要修改了。因为你修改之后,就跟远程分支不同步了,所以需要 git push -f origin your-local-branch, this may cause other people's branches to be out of sync with the remote.
If you want to change
commit message
,用git commit -amend
.If you want to add some content, don’t touch the last
commit
(就叫commit-1
吧),接着修改,然后提交一个新的commit
(commit-2
)。然后git rebase -i HEAD~~
, the following content will appear:Save the second
pick
改成squash
and exit, and the following will appear:Delete these things and change them to what you want
commit message
Save and exit.Then you can
git log
查看一下,两个commit
已经合并成为新的commit
.You can refer to:
5. Use rebase -i to merge and submit
===
BTW, if you can only commit the last time
commit
已经推到远程分支了,而且该分支有人跟你合作,那么最好还是不要修改了。因为你修改之后,就跟远程分支不同步了,所以需要git push -f origin your-local-branch
, this may cause other people's branches to be out of sync with the remote.git commit —amend
Can be used to undo the last submission