Git

Git Remove Commit Example

In this example, we shall show how to remove commits from our git’s log history. As we can see, we can commit every time we want because these commits will persist in our local machine and nobody can access to them if push have not been performed to the git central repository. Commit every change is really good but what if one change does not make sense? That is the perfect moment to apply git reset command and undo changes.

We must take into that we can not  undo changes all the time unless you have been working against your local repository. If you undo changes from git central repository you must perform git push --force or git push -f in order to rewrite the log history.  Take care to do this if the code is shared with other members.

 
In the image below, we can see all the commits we have performed in our local repository. The example is very simple, basically we have upgraded the spring-boot version in each commit.

git-remove-commit-example

1. Undoing changes with reset

Want to undo changes? Then, git reset is the simple way to do that. We just need to use the following command:

git reset <commit>

Once git reset has been performed files involved in the commit are ready to be taken from garbage collector.

The list below show some flags supported by git reset command:

  • --soft, remove commit but keep changes on track to commit again.
    git reset --soft <commit>
    

    Imagine we want to come back to version 1.0.0.RC1 then we have to perform.

    git reset --soft 17222ce
    

    Perform git status and the terminal will tell us that changes are in the stage.

    On branch master
    Changes to be committed:
      (use "git reset HEAD ..." to unstage)
    
    	modified:   pom.xml
    
  • --mixed, remove commit but keep changes to be added on stage.
    git reset --mixed <commit>
    

    For any reason, we want to come back to version 1.0.0.M5 then we have to perform.

    git reset --mixed 22d157d
    

    Perform git status and the terminal will tell us that changes are not in the stage.

    On branch master
    Changes not staged for commit:
      (use "git add ..." to update what will be committed)
      (use "git checkout -- ..." to discard changes in working directory)
    
    	modified:   pom.xml
    
  • --hard, remove commit and undo changes.
    git reset --hard <commit>
    

    Perform git status and the terminal won’t display anything.

2. Undoing changes with Rebase

2.1 Interactive Mode

Another way to undo changes is through git rebase interactive mode.

git rebase -i HEAD~7

Terminal will display all the commits in aster branch.

pick 058fb9c Update to M1
pick a2dd100 Update to M2
pick 351fe18 Update to M3
pick afeee89 Update to M4
pick 22d157d Update to M5
pick 17222ce Update to RC1
pick 16594ba Update to RELEASE

Now, we can delete line by line and then will be deleted from the git’s log history too. We will delete the first 6 commits.

pick 16594ba Update to RELEASE

Resolve the conflict:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>

 <groupId>io.eddumelendez</groupId>
 <artifactId>git-remove-commit-example</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>

 <name>git-remove-commit-example</name>
 <description>Demo project for Spring Boot</description>

 <parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
<<<<<<< HEAD
 <version>1.3.0.BUILD-SNAPSHOT</version>
=======
 <version>1.3.0.RELEASE</version>
>>>>>>> 16594ba... Update to RELEASE
 <relativePath/> <!-- lookup parent from repository -->
 </parent>

 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 <java.version>1.8</java.version>
 </properties>

 <dependencies>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
 </dependency>

 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-test</artifactId>
 <scope>test</scope>
 </dependency>
 </dependencies>

 <build>
 <plugins>
 <plugin>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-maven-plugin</artifactId>
 </plugin>
 </plugins>
 </build>

</project>

After conflicts resolution you can perform the following command.

git add .

Now, proceed with the rebase.

git rebase --continue

Perform the following command git log --oneline --graph --decorate. Then as you can see commits between the first and the last commit have been deleted.

6b91ba6 (HEAD -> master) Update to RELEASE
6a93b1b Initial commit

2.2. Onto

This time we will perform commits deletion in a single command using git rebase --onto. Similar to the example above we will remove commits between the first and the last commit.

git rebase --onto HEAD~7 HEAD~1 master

Perform the following command git log --oneline --graph --decorate. Then as you can see commits between the first and the last commit have been deleted.

bf9340f (HEAD -> master) Update to RELEASE
6a93b1b Initial commit

3. Conclusion

As we can see, in this example we have learnt how to remove commits in different ways using following commands reset, rebase and more options with each one. Take care when you are removing commits in order to avoid conflicts with your teammates.

4. Download the source code

Download
You can download the full source code of this example here: GitRemoveCommitExample.zip

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.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button