When we realized that the last commit was a mistake but we already published it, the command to use is git revert <COMMIT_HASH>
.
- First we need to locate the ID of the commit we want to revert, it can be done with
git log
orgit reflog
commands. - Then, run the
git revert <COMMIT_HASH>
command using the ID obtained in the previous step. Use the options-e
or--edit
to edit the commit message if we like. - Push our changes so the revert is available for everyone in our group.
Reverting multiple commits
If we need to revert multiple commits we can revert them one by one using the --no-commit
option in order to create a single revert commit at the end.
Imagine the history is like the following and we need to go back to COMMIT-3
.
1COMMIT-1 -> COMMIT-2 -> COMMIT-3 -> COMMIT-4 -> COMMIT-5 -> COMMIT-6 -> HEAD
This sequence of commands will get our files to the version of COMMIT-3
:
1bash-3.2$ git revert --no-commit COMMIT-6
2bash-3.2$ git revert --no-commit COMMIT-5
3bash-3.2$ git revert --no-commit COMMIT-4
4bash-3.2$ git commit -m "Revert to version in COMMIT-3"
5bash-3.2$ git push
Reverting a merge commit
-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.
Git revert documentation {: style=“text-align: right;”}
When we need to revert a merge commit git revert
command needs to be run with the -m
or --mainline
option to indicate the parent number because a merge commit
has more than one parent and Git does not know which parent was target branch and which was the branch with the changes that should be reverted.
Here there is an example showing how to revert a merge commit.
Create the first commit in main
branch.
1bash-3.2$ cat README.md
2# Index
3
41
5bash-3.2$
6bash-3.2$ git commit -m "Add number 1 in README.md - main branch"
7[main (root-commit) 23644da] Add number 1 in README.md - main branch
8 1 file changed, 3 insertions(+)
9 create mode 100644 README.md
10bash-3.2$
11bash-3.2$ git push -u origin main
12Enumerating objects: 3, done.
13Counting objects: 100% (3/3), done.
14Writing objects: 100% (3/3), 254 bytes | 254.00 KiB/s, done.
15Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
16To github.com:jnonino/test-repo.git
17 * [new branch] main -> main
18branch 'main' set up to track 'origin/main'.
19bash-3.2$
The state of the README.md
file in main
branch.
1bash-3.2$git status
2On branch main
3Your branch is up to date with 'origin/main'.
4
5nothing to commit, working tree clean
6bash-3.2$
7bash-3.2$ cat README.md
8# Index
9
101
Branch feature-1
created and added one commit.
1bash-3.2$ git checkout -b feature-1
2Switched to a new branch 'feature-1'
3bash-3.2$
4bash-3.2$ cat README.md
5# Index
6
71
82
9bash-3.2$
10bash-3.2$ git add README.md
11bash-3.2$
12bash-3.2$ git commit -m "Add number 2 in README.md - feature-1 branch"
13[feature-1 83ea1a3] Add number 2 in README.md - feature-1 branch
14 1 file changed, 1 insertion(+)
15bash-3.2$
16bash-3.2$ git push --set-upstream origin feature-1
17Enumerating objects: 5, done.
18Counting objects: 100% (5/5), done.
19Writing objects: 100% (3/3), 292 bytes | 292.00 KiB/s, done.
20Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
21remote:
22remote: Create a pull request for 'feature-1' on GitHub by visiting:
23remote: https://github.com/jnonino/test-repo/pull/new/feature-1
24remote:
25To github.com:jnonino/test-repo.git
26 * [new branch] feature-1 -> feature-1
27branch 'feature-1' set up to track 'origin/feature-1'.
28bash-3.2$
The state of README.md
in the feature-1
branch.
1bash-3.2$ git status
2On branch feature-1
3Your branch is up to date with 'origin/feature-1'.
4
5nothing to commit, working tree clean
6bash-3.2$
7bash-3.2$ cat README.md
8# Index
9
101
112
Merge the feature-1
branch into the main
branch.
1bash-3.2$ git checkout main
2Switched to branch 'main'
3Your branch is up to date with 'origin/main'.
4bash-3.2$
5bash-3.2$ git merge --no-ff feature-1
6Merge made by the 'ort' strategy.
7 README.md | 1 +
8 1 file changed, 1 insertion(+)
9bash-3.2$
10bash-3.2$ git push
11Enumerating objects: 1, done.
12Counting objects: 100% (1/1), done.
13Writing objects: 100% (1/1), 233 bytes | 233.00 KiB/s, done.
14Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
15To github.com:jnonino/test-repo.git
16 23644da..e2f6d08 main -> main
17bash-3.2$
Current state of README.md
in main
branch.
1bash-3.2$ git status
2On branch main
3Your branch is up to date with 'origin/main'.
4
5nothing to commit, working tree clean
6bash-3.2$
7bash-3.2$ cat README.md
8# Index
9
101
112
git log
after merging feature-1
into main
branch.
1commit e2f6d08d3b38a02a1c026cfb879f3131536757ac (HEAD -> main, origin/main)
2Merge: 23644da 83ea1a3
3Author: Julian Nonino <learn.software.eng+jnonino@gmail.com>
4Date: Wed Jan 18 19:58:19 2023 -0300
5
6 Merge branch 'feature-1'
7
8commit 83ea1a347e0e87b19a611997219089b5b9247d1f (origin/feature-1, feature-1)
9Author: Julian Nonino <learn.software.eng+jnonino@gmail.com>
10Date: Wed Jan 18 19:53:38 2023 -0300
11
12 Add number 2 in README.md - feature-1 branch
13
14commit 23644dab9fc5828ecdd358c6d3acb4196ed23546
15Author: Julian Nonino <learn.software.eng+jnonino@gmail.com>
16Date: Wed Jan 18 19:48:37 2023 -0300
17
18 Add number 1 in README.md - main branch
To revert the merge commit, as it was stated above we need to pay attention to the merge
field.
1Merge: 23644da 83ea1a3
Running git revert e2f6d08 -m 1
will reinstate the tree as it was in 23644da
, and git revert e2f6d08 -m 2
will set the tree as it was in 83ea1a3
.
In this example we would like to leave the main
branch as it was before the merge commit. For doing that, we need to run git revert e2f6d08 -m 1
.
1bash-3.2$ git revert e2f6d08 -m 1
2[main 866bfa8] Revert "Merge branch 'feature-1'"
3 1 file changed, 1 deletion(-)
4bash-3.2$
5bash-3.2$ git status
6On branch main
7Your branch is ahead of 'origin/main' by 1 commit.
8 (use "git push" to publish your local commits)
9
10nothing to commit, working tree clean
11bash-3.2$
12bash-3.2$ git push
13Enumerating objects: 5, done.
14Counting objects: 100% (5/5), done.
15Writing objects: 100% (3/3), 344 bytes | 344.00 KiB/s, done.
16Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
17To github.com:jnonino/test-repo.git
18 e2f6d08..866bfa8 main -> main
git log
after reverting the merge commit.
1commit 866bfa8a952d11240707ebfc87f3266034d42443 (HEAD -> main, origin/main)
2Author: Julian Nonino <learn.software.eng+jnonino@gmail.com>
3Date: Wed Jan 18 20:06:40 2023 -0300
4
5 Revert "Merge branch 'feature-1'"
6
7 This reverts commit e2f6d08d3b38a02a1c026cfb879f3131536757ac, reversing
8 changes made to 23644dab9fc5828ecdd358c6d3acb4196ed23546.
9
10commit e2f6d08d3b38a02a1c026cfb879f3131536757ac
11Merge: 23644da 83ea1a3
12Author: Julian Nonino <learn.software.eng+jnonino@gmail.com>
13Date: Wed Jan 18 19:58:19 2023 -0300
14
15 Merge branch 'feature-1'
16
17commit 83ea1a347e0e87b19a611997219089b5b9247d1f (origin/feature-1, feature-1)
18Author: Julian Nonino <learn.software.eng+jnonino@gmail.com>
19Date: Wed Jan 18 19:53:38 2023 -0300
20
21 Add number 2 in README.md - feature-1 branch
22
23commit 23644dab9fc5828ecdd358c6d3acb4196ed23546
24Author: Julian Nonino <learn.software.eng+jnonino@gmail.com>
25Date: Wed Jan 18 19:48:37 2023 -0300
26
27 Add number 1 in README.md - main branch
Current state of README.md
in main
branch.
1bash-3.2$ git status
2On branch main
3Your branch is up to date with 'origin/main'.
4
5nothing to commit, working tree clean
6bash-3.2$
7bash-3.2$ cat README.md
8# Index
9
101