You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/blog/git-revert/index.en.md
+146-4Lines changed: 146 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,20 +1,162 @@
1
1
---
2
-
title: "Revert a pushed change in Git"
2
+
title: "Reverting changes in Git"
3
3
authors:
4
4
- jnonino
5
5
description: >
6
-
Have you ever pushed a change in Git and it was wrong? Here you'll learn how to revert the change, even if the problem was with a merge commit.
6
+
This article will teach you how to discard commits in Git before pushing them and, how to revert a change that you have already pushed.
7
7
date: 2023-01-18
8
8
tags: ["Version Control", "VCS", "Git", "Revert"]
9
9
---
10
10
11
+
## Reverting a commit before pushing
12
+
13
+
When we have created a commit locally but have not published it to the remote yet, we can use `git reset` to undo the commit and, if we wish, discard the changes.
14
+
Although there are several options for `git reset` the most used are:
15
+
16
+
> -`--soft`: Does not touch the index file or the working tree at all (but resets the head to <COMMIT>, just like all modes do). This leaves all your changed files "Changes to be committed", as git status would put it.
17
+
> -`--hard`: Resets the index and working tree. Any changes to tracked files in the working tree since <COMMIT> are discarded. Any untracked files or directories in the way of writing any tracked files are simply deleted.
This reverts commit e2f6d08d3b38a02a1c026cfb879f3131536757ac, reversing
148
+
changes made to 23644dab9fc5828ecdd358c6d3acb4196ed23546.
149
+
```
150
+
151
+
## Reverting a commit after it was pushed
152
+
11
153
When we realized that the last commit was a mistake but we already published it, the command to use is `git revert <COMMIT_HASH>`.
12
154
13
155
- First we need to locate the ID of the commit we want to revert, it can be done with `git log` or `git reflog` commands.
14
156
- 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.
15
157
- Push our changes so the revert is available for everyone in our group.
16
158
17
-
## Reverting multiple commits
159
+
###Reverting multiple commits
18
160
19
161
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.
20
162
@@ -34,7 +176,7 @@ bash-3.2$ git commit -m "Revert to version in COMMIT-3"
0 commit comments