Like I said already about hard reset, it's a nasty way to alter a commit. Then what is the better way?
That's exactly what I have written in this post about how we could use amend and what does it do.
* Please read this post fully before executing any command. My scenario might not be same as yours.
What is amend in git commit?
In git commit, when you add the attribute --amend like git commit --amend, it lets you edit the most recent commit.
git commit --amend
So, when you give this command, it opens VIM editor with your latest commit message. Here you can edit your commit message and update it & push.
Is that all?
The answer is no. If you have changes in any of the files and you want to add them to your most recent commit, you can stage those files to commit. Then when you execute the above command, the most recent commit message is shown in the VIM editor or whatever default editor you have along with the newly staged files.
Therefore when you save the commit message from VIM editor, it updates the file changes as well to your most recent commit.
Therefore to sum up, with git commit --amend you can,
- Update the latest commit message
- Add / Remove / Modify files from / to the latest commit
I guess that tip helps whenever you have a case where the most recent commit needs to be updated.
Good Job !!ReplyDelete
Keep writing the blogs, this will really help when you want to avoid multiple commits..