arachnid
arachnid

Reputation: 236

What is the difference between git reset and git revert?

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 ?

Upvotes: 21

Views: 22495

Answers (3)

Aditya Malviya
Aditya Malviya

Reputation: 2140

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 

Upvotes: 4

Vinita Arun
Vinita Arun

Reputation: 61

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.

Upvotes: 4

Adrian Shum
Adrian Shum

Reputation: 40036

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

Upvotes: 49

Related Questions