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.
Although there are several options for git reset
the most used are:
--soft
: Does not touch the index file or the working tree at all (but resets the head to, just like all modes do). This leaves all your changed files “Changes to be committed”, as git status would put it. --hard
: Resets the index and working tree. Any changes to tracked files in the working tree sinceare discarded. Any untracked files or directories in the way of writing any tracked files are simply deleted. Git reset documentation {: style=“text-align: right;”}
Here there is an example about using git reset
. We start by changing a file and creating a commit with the change.
1bash-3.2$ cat README.md
2# Index
3
41
5bash-3.2$
6bash-3.2$ cat README.md
7# Index
8
91
102
11bash-3.2$
12bash-3.2$ git add README.md
13bash-3.2$
14bash-3.2$ git commit -m "Add number 2 in README.md"
15[main 3734fd5] Add number 2 in README.md
16 1 file changed, 1 insertion(+)
git status
shows there is one commit pending to be published.
1bash-3.2$ git status
2On branch main
3Your branch is ahead of 'origin/main' by 1 commit.
4 (use "git push" to publish your local commits)
5
6nothing to commit, working tree clean
Using git reflog
to see the history.
13734fd5 (HEAD -> main) HEAD@{0}: commit: Add number 2 in README.md
2866bfa8 (origin/main) HEAD@{1}: revert: Revert "Merge branch 'feature-1'"
3e2f6d08 HEAD@{2}: merge feature-1: Merge made by the 'ort' strategy.
423644da HEAD@{3}: checkout: moving from feature-1 to main
Now we can use git reset --soft <COMMIT_ID>
to undo the commit but keep the changes.
1bash-3.2$ git reset --soft 866bfa8
2bash-3.2$
3bash-3.2$ git status
4On branch main
5Your branch is up to date with 'origin/main'.
6
7Changes to be committed:
8 (use "git restore --staged <file>..." to unstage)
9 modified: README.md
git log
after using git reset
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.
We create a new commit so we can test the git reset --hard
command.
1bash-3.2$ git status
2On branch main
3Your branch is up to date with 'origin/main'.
4
5Changes to be committed:
6 (use "git restore --staged <file>..." to unstage)
7 modified: README.md
8
9bash-3.2$ git commit -m "Add number 2 in README.md - NEW COMMIT"
10[main 2e7193d] Add number 2 in README.md - NEW COMMIT
11 1 file changed, 1 insertion(+)
git log
now shows the new commit.
1commit 2e7193db650b9ba0762fe73525df599a08f8577d (HEAD -> main)
2Author: Julian Nonino <learn.software.eng+jnonino@gmail.com>
3Date: Thu Jan 19 08:32:57 2023 -0300
4
5 Add number 2 in README.md - NEW COMMIT
6
7commit 866bfa8a952d11240707ebfc87f3266034d42443 (origin/main)
8Author: Julian Nonino <learn.software.eng+jnonino@gmail.com>
9Date: Wed Jan 18 20:06:40 2023 -0300
10
11 Revert "Merge branch 'feature-1'"
12
13 This reverts commit e2f6d08d3b38a02a1c026cfb879f3131536757ac, reversing
14 changes made to 23644dab9fc5828ecdd358c6d3acb4196ed23546.
Now we can use git reset --hard <COMMIT_ID>
to undo the commit and discard all the changes.
1bash-3.2$ git reset --hard 866bfa8
2HEAD is now at 866bfa8 Revert "Merge branch 'feature-1'"
3bash-3.2$
4bash-3.2$ git status
5On branch main
6Your branch is up to date with 'origin/main'.
7
8nothing to commit, working tree clean
git log
remains as it nothing had happened.
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.