Git command tips: git amend
Rashwan Lazkani
Posted on February 3, 2022
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
…
Now you noticed that your date label is showing:
Feb 3, 2022 at 10:05 PM
But the requirement was to show it in the following format:
yyyy-MM-dd HH:mm
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
…
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
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
…
The following will happen:
- You will get a new commit id for you latest commit
- You will not create a new commit, instead you will amend the latest commit
Hope you found this useful!
Posted on February 3, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 29, 2024