How to modify commits that were not pushed

Asked

Viewed 1,920 times

4

I need to edit a commit message and add another file.

I haven’t pushed the commit yet, there’s a way to fix it?

2 answers

3


Yes!

You can undo your last commit local (without having pushed), going back to the previous commit state and missing the changes made to it.

Just do:

git reset --soft HEAD~1

Being 1 the number of commits back you want to undo. Since you only want the last one, you will use 1 (one).

After this, your pre-commit changes will all be shown as unstaged in the git status and will need to add them again with git add ..

Now just add the file you want and commit again by changing its message.

  • There is also the git commit --amend, that attaches things to the last commit (instead of undoing). You speak or I speak?

  • I was going to comment on the amend, but since he doesn’t want to just change the commit message, I ended up finding it best not to quote so as not to confuse. What do you think?

  • @Jeffersonquesado, I looked at the documentation now. I didn’t know that the amend could change the last commit by inserting more information. I think it’s fair that you speak :)

  • 2

    So, after I get to work I write =]

  • Thank you very much Jefferson for supplementing the answer.

2

If you want to make a change to last commit, keeping all others unchanged, you can make one git commit --amend.

The amend (in Portuguese seria traduzido para "melhorar" ou "emendar" according to Wiktionary), Roughly speaking, it works just like the normal commit. You use the casting area to place the additional changes and, at the time of realizing the commitment, you say that is an "emendo" of the previous commitment.

Through the Sourcetree, you can select "fix last commit" option to perform this commit --amend:

selecionando a opção de emendar o commit

I usually use the --amend to send some correction to a commit I already made by mistake. For example, I had to declare a new method in an interface and in the class that implements it. Sometimes I happen to list only the changed class and forget the interface. Another example, also very common, is when I create a new file and use it in a previously existing file, or else join the changelog the change in the code already committed.

Note that this one doesn’t need files in the cast area. It can be done to change the commit message only.

It is also possible to provide other parameters of emendo, such as the commit author.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.