While using Git, we all have come across situations when we feel the need of modifying our last commit before we can finally push our code changes.
In this quick tutorial, we’ll learn how can we amend our last commit which has not yet been pushed to the remote repository.
To only modify our last commit message, we can simply use:
git commit --amend -m "New Commit Message"
We might usually feel such a need when say we have made a typo in our last commit message. Or we simply want to make some more sense.
If we want to add new files or more file changes to the recent commit, we can pretty easily amend our commit. It will save us from the distorted commit history.
We might feel such a need when we are just about to push our changes and realize having missed some minor code changes or say simple formatting.
To achieve it, we need to first stage all our recent changes:
git add .
Once our changes have been staged, we can amend our commit:
git commit --amend
Here, we’ll also be prompted with an option to edit our commit message in a text editor. We can choose to amend our commit message or leave it as-is.
Now, we have our last commit updated with the desired changes. We can look at the updates in the Git logs:
git log
And yes, we are good to go to push our final changes. Yayy!
In this brief tutorial, we have learned to amend our last commit. We might either need to do so to change our commit message or to accommodate some more file changes.
Hope that was helpful!