[英]How can I undo my last git add/commit?
I edited a file and did: 我编辑了一个文件并做了:
git add file.py
git commit -m 'fixed bug'
I then edited another file and performed a minor bug fix. 然后我编辑了另一个文件并执行了一个小错误修复。 I don't want two commits, one after the other, showing 'bug fix'. 我不希望两个提交,一个接一个,显示'错误修复'。 I want one commit with 'bug fixes'. 我想要一个提交'bug fixes'。
How can I undo the last add/commit and change the first commit message? 如何撤消上次添加/提交并更改第一个提交消息?
I was looking at the git reset
, git revert
, git undo
commands but I don't want to screw up my repo with a guess 我正在看git reset
, git revert
, git undo
命令,但我不想用猜测搞砸我的回购
EDIT: Found out how to do it: http://www.gitready.com/advanced/2009/02/10/squashing-commits-with-rebase.html 编辑:了解如何做到: http : //www.gitready.com/advanced/2009/02/10/squashing-commits-with-rebase.html
If you already commited your second change, reset it first: 如果您已提交第二次更改,请先重置:
git reset HEAD^
Now your HEAD
is at the first commit, and the content of your local files is unchanged. 现在您的HEAD
处于第一次提交,并且本地文件的内容保持不变。
git add <the file(s) for the second bug fix>
git commit --amend -m'bug fixes'
If all tracked and changed file are to be included for the second bug fix, you can run this instead, as usual: 如果要为第二个错误修复包含所有跟踪和更改的文件,您可以像往常一样运行它:
git commit -a --amend
Amending the commit is exactly this: 修改提交正是这样的:
git add
, or -a
) 将索引中的更改添加到上一次提交中(因此需要git add
或-a
) Be careful, though: if you have distributed the first commit, other people's repo will get strange. 但要小心:如果你分发了第一个提交,其他人的回购会变得奇怪。 You should not change a commit that someone else has fetched. 您不应该更改其他人提取的提交。
You could also probably use git merge --squash
, which feels more logical but not necessarily easier. 你也可以使用git merge --squash
,这感觉更合乎逻辑,但不一定更容易。 Use it to merge a branch containing your two commits, unto the previous commit. 使用它来合并包含两个提交的分支,直到前一个提交。
Squashing works with git rebase
as well. Squashing也适用于git rebase
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.