- When to undo a commit and when to revert a commit in git?
- What are the git commands to do both of them?
- How to perform both tasks using VS code (UI)?

- 401
- 6
- 10
-
1Why grouping questions here? Better keep them focused on *one* thing, you'll have better quality answers. – Romain Valeri Jul 19 '23 at 10:34
-
I think there must be a place where all the questions should be addressed related to one question. – Muhammad Faraz Ali Jul 19 '23 at 10:45
2 Answers
Undo is to ditch last commit. It will not exists anymore in the current branch.
Command(s depending on how it is implemented in VSCode):
git reset --soft HEAD~
Or (to not keep changes in staging area):
git reset --mixed HEAD~
Revert is creating a new commit that contains all the opposite changes of the commit you will revert. So original commit and new reverting commit are in the current branch.
Command:
git revert <HASH_COMMIT_TO_REVERT>
Both strategies have pros and cons. Read about it. The main idea is if the purpose is to fix an error just made and the commit has still not been pushed, undo is perfectly fine. If commit/history has already been pushed, except for special cases, revert is more likely the way to go...
PS: another solution instead of undo could be just to amend the last commit...

- 28,207
- 6
- 54
- 78
A-undo commit (most recent(s))
undo commit-> You want to jump back
Git commands:
To undo a local commit
in Git, you can use the git reset
command.
Types/Options:
- --mixed (default)
- --soft
- --hard
1. --mixed: undo commit + unstaged the changes
and puts them back into the working directory
.
git reset HEAD~
or
git reset –mixed HEAD~1 //1-> undo one most recent commit
or
git reset –mixed HEAD~2 //2-> undo two most recents commit
2. --soft: undo commit + keep the changes in the staging area (index)
.
git reset --soft HEAD~
3. --hard: undo commit + discard/remove
all the changes.
git reset --hard HEAD~
VS Code:
Note1: In all the reset
commands, the branch pointer
of your current branch will be moved to the previous commit (HEAD~
refers to the commit before the current one) as shown in the above image.
Now if you want your changes in :
- the
working directory
use--mixed
option, - the
staging area (index)
use--soft
option, and - for
discard/remove
changes use--hard
option.
Note2: Undo commit **DOES NOT** creates a new commit
.
Note3: You should undo commit(s) if your commit(s) is/are local
, means you did not pushed into remote
.
B-revert commit
revert commit-> You want your changes (committed) back but in a new commit
Git commands:
To undo a published/remote commit
in Git, you can use the git revert
command.
Types/Options:
- --edit (default) : soft revert
- --no-edit :hard revert
1. --no-edit: undo commit + creates a new commit (changes discarded in this commit)
git revert --no-edit commit-id/commit hash
2. --edit: undo commit + creates a new commit but you can review the changes.
git revert --edit commit-id/commit hash
Note2: Revert commit ALWAYS creates a new commit
.
Note3: You should revert a commit
if your commit is remote
, which means you pushed into the remote
branch in order to maintain commits history

- 401
- 6
- 10