Other Tutorials

Git – Adding changes to your last commit

Introduction:

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.

Use Cases:

  • Modifying Last Commit Message:

    To only modify our last commit message, we can simply use:

    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.

  • Adding More Files or Changes: 

    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:

    Once our changes have been staged, we can amend our commit:

    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.

Verifying Amended Commit:

Now, we have our last commit updated with the desired changes. We can look at the updates in the Git logs:

And yes, we are good to go to push our final changes. Yayy!

Conclusion:

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!

Be the First to comment.

Leave a Comment

Your email address will not be published. Required fields are marked *