Photo by Jim Wilson on Unsplash

How to Undo Last Git Commit

Learn how to avoid “Fix previous commit” commits in Git

Ali Kamalizade
Published in
3 min readMay 6, 2020

--

Most software projects use Git as a version-control system. I have previously written about the topic of writing small Git commits. One of the advantages of this approach is that you can easily fix or revert small commits. If the commit is small then it should be easy to describe and easy to fixup in case we forgot something.

There are two common situations happening that many of us are probably encountering a lot:

  • You made some Git commits and then realized that you forgot to include some local changes. Suppose you’re displaying some images in your application but you forgot to include these new images in your commit. If someone were to checkout the project at this stage then he would not see these images in the application.
  • You are unhappy with the last Git commit message you wrote earlier. As Git commit messages should be easy to understand the purpose of this commit, a bad commit message may cause confusion.

Of course you can make another commit to add missing files or to better explain what you did in the previous commit. However, by doing this you’re creating unnecessary commits and the “bad” commits will still be there which will be harder to review.

Luckily, Git allows us to change previous commits. Some IDEs like IntelliJ IDEA or tools like Sourcetree have a handy UI to perform various Git operations. In this short post, I want to show you how we can do this in the command line.

How to edit the commit changes but keep the commit message

  1. State the files that should be included in the previous commit
  2. Amend your local changes to be included
  3. Push the changes to the repository
# Stage your changes
git add someFileThatWasChanged
# Amend your local changes into the last commit
git commit --amend
# Push the changes to the repository
git push --force-with-lease

How to edit the commit message but keep the changes

  1. Reset the last commit
  2. Create a new commit with a proper message
  3. Push the changes to the repository
# Undo the last commit
git reset --soft HEAD~1
# Create a commit with an updated message
git commit -m="My new commit message"
# Push the changes to the repository
git push --force-with-lease
Settings in Visual Studio Code

Force pushing the safe way with --force-with-lease

git push --force-with-lease is a safer way to force push your changes compared to git push --force. git push --force can overwrite other people’s commits which makes this command quite dangerous. By using git push --force-with-lease, you don’t alter previous commit IDs and thereby reduce potential conflicts if other developers are working on the same branch as you.

If you prefer to use the Git integration in your favorite IDE then you’re in luck as in recent Jetbrains IDEs (like IntelliJ IDEA, WebStorm etc.) as well as in Visual Studio Code this is done automatically when force pushing changes. I recommend to keep this setting as force pushing can cause commits to be lost when you are not careful.

Conclusion

Thanks for reading this short post on how you can fix your last Git commit. Do you know other Git tricks? Let me know in the comments.

--

--

Ali Kamalizade
Webtips

Co-founder of Sunhat. Posts about software engineering, startups and anything else. 有難うございます。🚀