Skip to content

Commit eb8fac5

Browse files
authored
Add featured images for blog articles (#7)
1 parent fc17962 commit eb8fac5

5 files changed

Lines changed: 146 additions & 151 deletions

File tree

1.85 MB
Loading
1.69 MB
Loading

content/blog/git-revert-local.en.md

Lines changed: 0 additions & 147 deletions
This file was deleted.
2.09 MB
Loading
Lines changed: 146 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,162 @@
11
---
2-
title: "Revert a pushed change in Git"
2+
title: "Reverting changes in Git"
33
authors:
44
- jnonino
55
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.
77
date: 2023-01-18
88
tags: ["Version Control", "VCS", "Git", "Revert"]
99
---
1010

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.
18+
>
19+
> [Git reset documentation](https://git-scm.com/docs/git-reset)
20+
> {: style="text-align: right;"}
21+
22+
Here there is an example about using `git reset`. We start by changing a file and creating a commit with the change.
23+
24+
```bash
25+
bash-3.2$ cat README.md
26+
# Index
27+
28+
1
29+
bash-3.2$
30+
bash-3.2$ cat README.md
31+
# Index
32+
33+
1
34+
2
35+
bash-3.2$
36+
bash-3.2$ git add README.md
37+
bash-3.2$
38+
bash-3.2$ git commit -m "Add number 2 in README.md"
39+
[main 3734fd5] Add number 2 in README.md
40+
1 file changed, 1 insertion(+)
41+
```
42+
43+
`git status` shows there is one commit pending to be published.
44+
45+
```bash
46+
bash-3.2$ git status
47+
On branch main
48+
Your branch is ahead of 'origin/main' by 1 commit.
49+
(use "git push" to publish your local commits)
50+
51+
nothing to commit, working tree clean
52+
```
53+
54+
Using `git reflog` to see the history.
55+
56+
```bash
57+
3734fd5 (HEAD -> main) HEAD@{0}: commit: Add number 2 in README.md
58+
866bfa8 (origin/main) HEAD@{1}: revert: Revert "Merge branch 'feature-1'"
59+
e2f6d08 HEAD@{2}: merge feature-1: Merge made by the 'ort' strategy.
60+
23644da HEAD@{3}: checkout: moving from feature-1 to main
61+
```
62+
63+
Now we can use `git reset --soft <COMMIT_ID>` to undo the commit but keep the changes.
64+
65+
```bash
66+
bash-3.2$ git reset --soft 866bfa8
67+
bash-3.2$
68+
bash-3.2$ git status
69+
On branch main
70+
Your branch is up to date with 'origin/main'.
71+
72+
Changes to be committed:
73+
(use "git restore --staged <file>..." to unstage)
74+
modified: README.md
75+
```
76+
77+
`git log` after using `git reset`
78+
79+
```bash
80+
commit 866bfa8a952d11240707ebfc87f3266034d42443 (HEAD -> main, origin/main)
81+
Author: Julian Nonino <learn.software.eng+jnonino@gmail.com>
82+
Date: Wed Jan 18 20:06:40 2023 -0300
83+
84+
Revert "Merge branch 'feature-1'"
85+
86+
This reverts commit e2f6d08d3b38a02a1c026cfb879f3131536757ac, reversing
87+
changes made to 23644dab9fc5828ecdd358c6d3acb4196ed23546.
88+
```
89+
90+
We create a new commit so we can test the `git reset --hard` command.
91+
92+
```bash
93+
bash-3.2$ git status
94+
On branch main
95+
Your branch is up to date with 'origin/main'.
96+
97+
Changes to be committed:
98+
(use "git restore --staged <file>..." to unstage)
99+
modified: README.md
100+
101+
bash-3.2$ git commit -m "Add number 2 in README.md - NEW COMMIT"
102+
[main 2e7193d] Add number 2 in README.md - NEW COMMIT
103+
1 file changed, 1 insertion(+)
104+
```
105+
106+
`git log` now shows the new commit.
107+
108+
```bash
109+
commit 2e7193db650b9ba0762fe73525df599a08f8577d (HEAD -> main)
110+
Author: Julian Nonino <learn.software.eng+jnonino@gmail.com>
111+
Date: Thu Jan 19 08:32:57 2023 -0300
112+
113+
Add number 2 in README.md - NEW COMMIT
114+
115+
commit 866bfa8a952d11240707ebfc87f3266034d42443 (origin/main)
116+
Author: Julian Nonino <learn.software.eng+jnonino@gmail.com>
117+
Date: Wed Jan 18 20:06:40 2023 -0300
118+
119+
Revert "Merge branch 'feature-1'"
120+
121+
This reverts commit e2f6d08d3b38a02a1c026cfb879f3131536757ac, reversing
122+
changes made to 23644dab9fc5828ecdd358c6d3acb4196ed23546.
123+
```
124+
125+
Now we can use `git reset --hard <COMMIT_ID>` to undo the commit and discard all the changes.
126+
127+
```bash
128+
bash-3.2$ git reset --hard 866bfa8
129+
HEAD is now at 866bfa8 Revert "Merge branch 'feature-1'"
130+
bash-3.2$
131+
bash-3.2$ git status
132+
On branch main
133+
Your branch is up to date with 'origin/main'.
134+
135+
nothing to commit, working tree clean
136+
```
137+
138+
`git log` remains as it nothing had happened.
139+
140+
```bash
141+
commit 866bfa8a952d11240707ebfc87f3266034d42443 (HEAD -> main, origin/main)
142+
Author: Julian Nonino <learn.software.eng+jnonino@gmail.com>
143+
Date: Wed Jan 18 20:06:40 2023 -0300
144+
145+
Revert "Merge branch 'feature-1'"
146+
147+
This reverts commit e2f6d08d3b38a02a1c026cfb879f3131536757ac, reversing
148+
changes made to 23644dab9fc5828ecdd358c6d3acb4196ed23546.
149+
```
150+
151+
## Reverting a commit after it was pushed
152+
11153
When we realized that the last commit was a mistake but we already published it, the command to use is `git revert <COMMIT_HASH>`.
12154

13155
- 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.
14156
- 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.
15157
- Push our changes so the revert is available for everyone in our group.
16158

17-
## Reverting multiple commits
159+
### Reverting multiple commits
18160

19161
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.
20162

@@ -34,7 +176,7 @@ bash-3.2$ git commit -m "Revert to version in COMMIT-3"
34176
bash-3.2$ git push
35177
```
36178

37-
## Reverting a merge commit
179+
### Reverting a merge commit
38180

39181
> -m parent-number, --mainline parent-number
40182
>

0 commit comments

Comments
 (0)