17

Hi I am a newbie to git and I don't understand what is the basic difference between git reset and git revert. Does git revert reverts the merge being pushed ?

Mohsen Esmailpour
  • 11,224
  • 3
  • 45
  • 66
arachnid
  • 196
  • 1
  • 1
  • 10
  • possible duplicate of [What's the difference between Git Revert, Checkout and Reset?](http://stackoverflow.com/questions/8358035/whats-the-difference-between-git-revert-checkout-and-reset) – Sascha Wolf Nov 20 '14 at 07:50
  • Yes i got the solution here as well as to the link you mentioned after soon after I posted the question – arachnid Nov 20 '14 at 08:59
  • `git revert` no files are deleted unlike the `git reset` command. Revert is often the better choice as a new SHA is created migrating all the files from the commit that you want to revert to. eg: `commit 123ab456 commit 223ab446 commit 223de446 commit 22356de44689 — — now is reverted to the information in commit 123ab456 ` `git reset — hard` is destructive and removes all tracked files from the branch and you will not find these files either in the working directory or staging area. Safer are soft and mixed resets [More](https://sumisastri.medium.com/git-and-git-heads-f1f8c3d9c7df) – Sumi Jan 17 '23 at 14:25

3 Answers3

42

As far as I know, they are doing totally different thing.

git revert aimed to revert effects of previous commit. For example,

A <- B <- C 
          ^ HEAD

If I found B I committed before is wrong, and I want to "undo" its change, git-revert-ing B will cause:

A <- B <- C <- B'
               ^ HEAD

for which B' is reversing the change done in B.

git reset is more straight-forward, it is simply setting the HEAD to a certain commit,

A <- B <- C 
          ^ HEAD

git-reset-ting to B will give you

A <- B <- C 
     ^ HEAD
Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
  • Is there any ways to show the "tree map" (ex: A <- B <- C ^ HEAD) with command line ? I really want to see it. thank you. – Chau Pham Nov 13 '16 at 14:32
  • @Catbuilts something like `git log --graph`? – Adrian Shum Apr 21 '17 at 02:05
  • Does it do anything other then this? I noticed that after accidentally merged brach A into branch B, and then reverted the merge commit, then it was not possible anymore to merge A into B again. But only by reverting the revert commit. – Crouching Kitten Jul 07 '17 at 09:10
  • git reset and push with -f can alter the repo commit history tree too, while git revert creates a new commit – Merkurial Sep 15 '17 at 02:46
4

Git reset -->move the tip of a branch to a different commit. This can be used to remove commits from the current branch. It moves the branch backwards by commits. Git Revert-->undoes a commit by creating a new commit. This is a safe way to undo changes, as it has no chance of re-writing the commit history.

3

git revert: Undoes a change part of a commit to the local/origin repo by creating a new commit.

command: git revert <id>

git reset: Git reset will delete/undo changes which are committed in local repo. It undoes changes by 3 ways, –soft, –mixed, –hard. Where mixed is the default value.

Working Dir (coding ) -> Staging Area(Index) -> local Repo (git push)

git reset –soft/mixed/hard –HEAD~N -> mixed is default

git reset --soft HEAD~N   # will move file/changes  from local commit to staging area
git reset --mixed HEAD~N  #will move file/changes  from local commit to working directory
git reset --hard HEAD~N  #will delete file /changes from working directory 
Aditya Malviya
  • 1,907
  • 1
  • 20
  • 25