Git Revert Commit Example
1. Introduction
This is an in-depth article related to the Git Revert Commit. Git was developed by Vincent Driessen in 2010. The git revert command is used to remove all the changes done in a single commit made to your source code repository.
2. Git Revert Commit
You can revert a commit that you made to Git. In some cases, a set of commits can be reverted. This occurs when there is a build problem that came up because of a commit. It is an operation that is equivalent to simple undo in the repository.
2.1 Prerequisites
Java 7 or 8 is required on the Linux, windows, or mac operating system. Maven 3.6.1 is required for building this application. A GitHub account is required.
2.2 Download
You can download Java 8 can be downloaded from the Oracle website. Apache Maven 3.6.1 can be downloaded from the Apache site. GitHub can be accessed at this link. Gitkraken can be downloaded from this site. Git can be downloaded from this link.
2.3 Setup
You can create an account on GitHub account using the link. You can create a GitHub repository with a Calculator application java code. You can add the file manually to get started. The code is shown below :
Calculator
package org.build.maventool; /** * Calculator class */ public class Calculator { /** * getProduct * @param val1 integer * @param val2 integer * @return product of val1 and val2 */ public int getProduct(int val1, int val2) { return val1*val2; } /** * main method * @param args arguments */ public static void main(String[] args) { Calculator calc = new Calculator(); int product = calc.getProduct(43,4); System.out.println("Product of " + " 43 and 4 is "+product); } }
You can use the below maven pom for building this file.
maven pom.xml
<?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>maven-hello</groupId> <artifactId>Maven-project</artifactId> <packaging>jar</packaging> <version>1.0</version> <name>Maven-project</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.11</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.0</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>org.build.maventool.Calculator</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
2.4 Importance of git revert
Git revert is important and valuable when you have multiple developers working together in the same repository. Git revisions are part of a git commit. A commit is identified by sha and it is unique. Similarly, the branch names are identified by characters followed by ~ and the number of commits behind the head commit. Git revert deletes the commit from the local workspace and not from the local repository.
2.5 Reverting a Commit With git revert
Let us take an example where there is an edit to the java class. The code is shown below:
Modified Calculator class
package org.build.maventool; /** * Calculator class */ public class Calculator { /** * getProduct * @param val1 integer * @param val2 integer * @return product of val1 and val2 */ public int getProduct(int val1, int val2) { return val1*val2; } /** * getSum * @param val1 integer * @param val2 integer * @return sum of val1 and val2 */ public int getSum(int val1, int val2) { return val1+val2; } /** * main method * @param args arguments */ public static void main(String[] args) { Calculator calc = new Calculator(); int product = calc.getProduct(43,4); System.out.println("Product of " + " 43 and 4 is "+product); int sum = calc.getSum(43,4); System.out.println("Sum of " + " 43 and 4 is "+sum); } }
This code is committed to a repository created in GitHub. The below commands are used.
Commands for commit
(base) apples-MacBook-Air:opensource bhagvan.kommadi$ git clone https://github.com/bhagvank/gitexamples.git Cloning into 'gitexamples'... remote: Enumerating objects: 3, done. remote: Counting objects: 100% (3/3), done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. (base) apples-MacBook-Air:opensource bhagvan.kommadi$ pwd /Users/bhagvan.kommadi/olddesk/opensource (base) apples-MacBook-Air:opensource bhagvan.kommadi$ ls NoCodeAIML gitexamples tensorflowexamples computer_vision springexamples (base) apples-MacBook-Air:opensource bhagvan.kommadi$ cd gitexamples/ (base) apples-MacBook-Air:gitexamples bhagvan.kommadi$ ls MavenHello README.md (base) apples-MacBook-Air:gitexamples bhagvan.kommadi$ git add . (base) apples-MacBook-Air:gitexamples bhagvan.kommadi$ git commit -m "added modified calculator code" [main 9fe8866] added modified calculator code 5 files changed, 191 insertions(+) create mode 100755 MavenHello/README.md create mode 100644 MavenHello/dependency-reduced-pom.xml create mode 100755 MavenHello/pom.xml create mode 100755 MavenHello/src/main/java/org/build/maventool/Calculator.java create mode 100755 MavenHello/src/test/java/org/build/maventool/CalculatorTest.java (base) apples-MacBook-Air:gitexamples bhagvan.kommadi$
Now add a line in the java code for printing a message. The modified code is shown below:
Calculator class added a new line
package org.build.maventool; /** * Calculator class */ public class Calculator { /** * getProduct * @param val1 integer * @param val2 integer * @return product of val1 and val2 */ public int getProduct(int val1, int val2) { return val1*val2; } /** * getSum * @param val1 integer * @param val2 integer * @return sum of val1 and val2 */ public int getSum(int val1, int val2) { return val1+val2; } /** * main method * @param args arguments */ public static void main(String[] args) { Calculator calc = new Calculator(); int product = calc.getProduct(43,4); System.out.println("Product of " + " 43 and 4 is "+product); int sum = calc.getSum(43,4); System.out.println("Sum of " + " 43 and 4 is "+sum); System.out.println("New message"); } }
Try first commit this change and then revert the change using the commands below:
Commands to commit and revert commit
(base) apples-MacBook-Air:MavenHello bhagvan.kommadi$ git status On branch main Your branch is ahead of 'origin/main' by 1 commit. (use "git push" to publish your local commits) Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: src/main/java/org/build/maventool/Calculator.java no changes added to commit (use "git add" and/or "git commit -a") (base) apples-MacBook-Air:MavenHello bhagvan.kommadi$ git add . (base) apples-MacBook-Air:MavenHello bhagvan.kommadi$ git commit -m "added a new line" [main 61b377c] added a new line 1 file changed, 1 insertion(+) (base) apples-MacBook-Air:MavenHello bhagvan.kommadi$ git revert 61b377c [main 944d7b2] Revert "added a new line" 1 file changed, 1 deletion(-)
The git revert commit command removes the changes, and a new commit is created to identify the new state of your repository.
2.6 Compare git revert vs. reset
When you want to undo a set of changes associated with a commit, you can use the git revert commit command. In the case of undoing every change from a given commit, you need to use a hard git reset. In another case where hard resetting files to HEAD on git needs to be done, the git reset command is used –hard option.
2.7 git revert options
The syntax to revert a commit and undo unwanted changes is simple. All developers need to do is issue the git revert command and provide the ID of the commit to undo:
git revert commit command options
… Commits to revert. For a more complete list of ways to spell commit names, see gitrevisions[7]. Sets of commits can also be given but no traversal is done by default, see git-rev-list[1] and its --no-walk option. -e --edit With this option, git revert will let you edit the commit message prior to committing the revert. This is the default if you run the command from a terminal. -m parent-number --mainline parent-number Usually you cannot revert a merge because you do not know which side of the merge should be considered the mainline. This option specifies the parent number (starting from 1) of the mainline and allows revert to reverse the change relative to the specified parent. Reverting a merge commit declares that you will never want the tree changes brought in by the merge. As a result, later merges will only bring in tree changes introduced by commits that are not ancestors of the previously reverted merge. This may or may not be what you want. See the revert-a-faulty-merge How-To for more details. --no-edit With this option, git revert will not start the commit message editor. --cleanup= This option determines how the commit message will be cleaned up before being passed on to the commit machinery. See git-commit[1] for more details. In particular, if the is given a value of scissors, scissors will be appended to MERGE_MSG before being passed on in the case of a conflict. -n --no-commit Usually the command automatically creates some commits with commit log messages stating which commits were reverted. This flag applies the changes necessary to revert the named commits to your working tree and the index, but does not make the commits. In addition, when this option is used, your index does not have to match the HEAD commit. The revert is done against the beginning state of your index. This is useful when reverting more than one commits' effect to your index in a row. -S[] --gpg-sign[=] --no-gpg-sign GPG-sign commits. The keyid argument is optional and defaults to the committer identity; if specified, it must be stuck to the option without a space. --no-gpg-sign is useful to countermand both commit.gpgSign configuration variable, and earlier --gpg-sign. -s --signoff Add a Signed-off-by trailer at the end of the commit message. See the signoff option in git-commit[1] for more information. --strategy= Use the given merge strategy. Should only be used once. See the MERGE STRATEGIES section in git-merge[1] for details. -X --strategy-option= Pass the merge strategy-specific option through to the merge strategy. See git-merge[1] for details. --rerere-autoupdate --no-rerere-autoupdate Allow the rerere mechanism to update the index with the result of auto-conflict resolution if possible. --reference Instead of starting the body of the log message with "This reverts .", refer to the commit using "--pretty=reference" format (cf. git-log[1]). The revert.reference configuration variable can be used to enable this option by default.
3. Download the Source Code
You can download the full source code of this example here: Git Revert Commit Example