DEV Community

Rashwan Lazkani
Rashwan Lazkani

Posted on

Git command tips: git amend

A very powerful yet maybe not so common git command is the amend command.

Consider the following scenario, you have a task to create a label to show current date. You create this and commit and your git tree looks something like this now:

5152417 (HEAD -> feature/add-date-label, origin/master, master) Added date field
936442a Added assets
…
Enter fullscreen mode Exit fullscreen mode

Now you noticed that your date label is showing:

Feb 3, 2022 at 10:05 PM
Enter fullscreen mode Exit fullscreen mode

But the requirement was to show it in the following format:

yyyy-MM-dd HH:mm
Enter fullscreen mode Exit fullscreen mode

You update this and commit again and your git tree looks like this now:

283b5d0 (HEAD -> feature/add-date-label, origin/master, master) Added right formatting to date field
5152417 Added date field
936442a Added assets
…
Enter fullscreen mode Exit fullscreen mode

This does work, but you could do it in a much cleaner way without creating a new commit with the use of the amend command. So try this command instead:

git add . && git commit --amend --no-edit && git push --force
Enter fullscreen mode Exit fullscreen mode

After running above command you will instead of creating a new commit, you will amend it to the current commit. Your git tree looks like this now after the amend:

84hv801 (HEAD -> feature/add-date-label, origin/master, master) Added date field
936442a Added assets
…
Enter fullscreen mode Exit fullscreen mode

The following will happen:

  1. You will get a new commit id for you latest commit
  2. You will not create a new commit, instead you will amend the latest commit

Hope you found this useful!

Top comments (0)