In this post, I want to talk about maintaining a single commit in Git using the amend flag.

Whether your company policy restricts multiple commits in a pull request or you’ve mistakenly included incorrect files with an inaccurate log message, amend flag can rewrite Git history!

Why maintain a single commit in Git?

Large companies usually have some kind of standards around contributing to a codebase.

Some allow multiple number of commits in a pull request, which will later get squashed into a single commit as part of the merge. Others require you to maintain a single commit in a Git pull request.

I was accustomed to the first approach for a long time in my career. However, when I joined Modulr, I had to switch my ways of working and adjust to the latter.

Modulr’s contribution guidelines state:

PRs are typically composed of a single commit & capture a single “unit of work”. This means not fixing 3 unrelated bugs together or adding multiple APIs in one go.

Modulr Pull Request Guidance

This means when addressing a code review comments in my branch, I had to amend the existing commit instead of creating new commits.

Using the Git command line

Start making the code changes and stage the files to commit:

git add .Code language: Bash (bash)

Next, commit using the amend flag:

git commit --amendCode language: Bash (bash)

This will open a text editor to overwrite the commit message.

Once you close the editor window, the last commit in the branch will be modified to reflect your changes.

To amend the previous commit without changing the commit message, use --no-edit flag:

git commit --amend --no-editCode language: Bash (bash)

You’ll now notice that your local branch has diverged from the remote:

umut.esen@XYZ /c/Dev/test-app (my-branch)
$ git status
On branch my-branch
Your branch and 'origin/my-branch' have diverged,
and have 1 and 1 different commits each, respectively.
  (use "git pull" if you want to integrate the remote branch with yours)Code language: Bash (bash)

This is totally expected as your local branch does not match what’s in remote Git repository.

Continue with force push to overwrite the commit in remote:

git push --force Code language: Bash (bash)

Using VSCode

If you’re like me and sometimes enjoy using the VSCode UI to work with Git, follow the steps below to amend your commits.

Click on the down arrow on the Commit button and choose Commit (Amend):

VSCode amend commit

This will automatically stage all your changes and open the commit editor for updating the message.

Close the commit message editor and you’ll now see the bottom left the diverged state of the branch:

git diverged branch

When it comes to force push, VSCode does not allow you to do that out of the box.

Thanks to this SO answer, it is possible to enable it in settings.

Go to File > Preferences > Settings and search for git force:

VSCode settings window git force

Once enabled, the force push option is visible in the Git menu (three dots, more) Pull, Push:

VSCode push force

Conclusion

In conclusion, the amend flag is very useful for maintaining a single commit in Git and correcting a mistake in a previous commit. It will help you keep your Git history clean in a pull request.

Umut Esen

Software Engineer specialising in full-stack web application development.

Leave a Reply