Home » Software Development » Git » Git Commit Amend Example

About Eddu Melendez

Eddu Melendez
Eddu is a Peruvian software engineer. He is interested in Java, Spring Framework and Mule ESB. He is also very enthusiastic to learn new stuff.

Git Commit Amend Example

In the previous post Git “Hello World” Example we learnt some basic steps about Git. In this example, we shall show how to edit commit messages.

1. Context

As a developers, we perform commits all the time while we are in the development process, adding new features, tests, documentation and so on. But, there are some cases when we forgot to add specific files, properties or miss to include some changes. We must take into account that every commit must be atomic which mean all changes must be in one commit. Then, our question now would be How can we handle these new changes? The simple question would be make another commit to fix them but our functionality will be involved two or more commits in the repository. For that reason, we could edit commits. Edit commit messages means rewrite the repository history and generate new commit ids.

Warning: Be careful to amend public commits shared by other developers.

2. Use Cases

There are two ways to edit messages, using commit and rebase commands.

  1. Edit single commit message:
     
    Performing git commit --amend command will display an editor in order to edit the previous commit message. The --amend option works fine with other options like --reset-author, --no-edit and --only

    1. --no-edit, this option will merge your recent changes with the previous commit, new commit message will not be requested.
      git commit --amend --no-edit
    2. --reset-author, this option will overwrite the author information in the most recent commit.
      git commit --amend --reset-author
    3. --only, this option allow to edit the most recent commit with the exclusion of the new files staged in the repository.
      git commit --amend --only
    4. --reuse-message, this option will use the commit message used in previous commits.
      git commit --amend --reuse-message=HEAD

    If we are working with public repositories and we performed amends we will have to force push otherwise we will get an error like that:

    To git@github.com:eddumelendez/hello-world.git
     ! [rejected]        master -> master (non-fast-forward)
    error: failed to push some refs to 'git@github.com:eddumelendez/hello-world.git'
    hint: Updates were rejected because the tip of your current branch is behind
    hint: its remote counterpart. Integrate the remote changes (e.g.
    hint: 'git pull ...') before pushing again.
    hint: See the 'Note about fast-forwards' in 'git push --help' for details.
    

    In order to force update in the public repository you should use git push --force or git push -f.

    git push -f origin master
  2. Edit multiple commit messages:
     
    To do this, we need to work with git rebase --interactive or git rebase -i command.

    git rebase -i HEAD~2

    Terminal will display the last two commits.

    pick c50f274 Add CONTRIBUTION.md file
    pick 0554b38 Fix utroque
    
    # Rebase 4be061b..0554b38 onto 4be061b (2 command(s))
    #
    # Commands:
    # p, pick = use commit
    # r, reword = use commit, but edit the commit message
    # e, edit = use commit, but stop for amending
    # s, squash = use commit, but meld into previous commit
    # f, fixup = like "squash", but discard this commit's log message
    # x, exec = run command (the rest of the line) using shell
    #
    # These lines can be re-ordered; they are executed from top to bottom.
    #
    # If you remove a line here THAT COMMIT WILL BE LOST.
    #
    # However, if you remove everything, the rebase will be aborted.
    #
    # Note that empty commits are commented out
    

    If you want to rebase all history then we should use git rebase -i --root.

    Also, if we want to review pending commits to be submitted to the public repository we can use:

    git rebase -i origin/master

    Terminal will display the difference between your local repository and remote repository.

    Now, replace pick by reword, this option allow us to edit commit messages. Terminal will display each commit message to be edited.

Below, we can see the original history and the history modified after the amend. Message has been edited from “Fix utroque” to “Replace utroque by branch name”.

original history

original history

rewrited history

rewrited history

3. Conclusion

As we can see, amend commit messages enable us to fix our commits for different reasons. But, also there are some considerations to take into account in order to avoid conflicts with our team. We must keep in mind when is valid to amend commits in our git workflow.

4. Download the Source Code

This was an example of Git Commit Amend. We will use the source code completed of Git “Hello World” Example to start with this one.

Download
You can download the full source code of this example here: git-hello-world-example
(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of