简体   繁体   中英

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'.

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

EDIT: Found out how to do it: 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.

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:

  • adds the changes in index to the previous commit (therefore the need for git add , or -a )
  • allows you to change your commit message

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. Use it to merge a branch containing your two commits, unto the previous commit.

Squashing works with git rebase as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM