inf3rno
inf3rno

Reputation: 26157

Git override last commit

I have a commit which has some minor bugs. I don't want to send the bugfix in a new commit, because I will send a pull request, so I want the changes to be as straightforward as possible. So I'd like to remove the old commit, but keep it's changes and send a new one after I applied my bug fixes. Is this possible?

Upvotes: 0

Views: 4623

Answers (2)

0xAX
0xAX

Reputation: 21837

If I understood you correctly, there are two methods. First is simple, just use:

git commit --amend

The second is a little complex, but more flexible. You no need to delete you old commit. You can just make new commit with your bug fixes above your old commit. So, the history will be look like this:

Old commit ---> New commit

Now you can execute:

git rebase -i HEAD~2

You will see something like this:

pick hash new commit
pick hash old commit with bugs

# 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

Just write squash instead of pick for the old commit and save changes. After this you will edit commit message for squashed commits and both commits will be squashed into one. More about this, you will find - 7.6 Git Tools - Rewriting History

Upvotes: 3

David Siro
David Siro

Reputation: 1906

Amend is what you're looking for. It simply adds changes to previous commit.

git commit --amend

Upvotes: 1

Related Questions