How to Make Changes to Past Git Commits

You don’t always have to commit to your commits

Bikash Paneru
Level Up Coding
Published in
10 min readJul 30, 2021

--

Base image by @ilumire on Unsplash

Git is one of the most important tools for Software Developers. It provides a clear and traceable history of how code has evolved over time. Git is also essential for code reviews since it provides a clear view of code changes. Keeping git commits minimal, clear, and concise helps to better track changes and makes code reviews easier.

However, us developers often mess up our commits and it is very tempting to go back and change them. Here, we will look at a bunch of such scenarios and discuss how to deal with them.

A fair warning

A lot of the examples in this article deal with changing commit history. Please note that if you have already pushed to the remote server before making these kinds of changes, then the remote server will reject your next push.

In such a case, you can do a force push using git push -f origin/branch-name. However, you must make sure no one has already pulled your branch before force pushing. This will cause problems for them. Its mostly a better idea to just make another commit if you have already pushed the branch to the remote server. This applies for all of the examples here.

--

--