Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between git reset and git revert?

Tags:

git

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 ?

like image 632
arachnid Avatar asked Nov 20 '14 06:11

arachnid


People also ask

What is difference between reset revert and restore?

Notice that revert and reset are history altering (making a new commit in former, and moving the tip in the latter), where as restore does not modify history. Let's consider an example of when you'd want to use restore instead of revert or reset .

What is git reset?

Summary. To review, git reset is a powerful command that is used to undo local changes to the state of a Git repo. Git reset operates on "The Three Trees of Git". These trees are the Commit History ( HEAD ), the Staging Index, and the Working Directory.

Does git reset hard remove commits?

Removing the last commitTo remove the last commit from git, you can simply run git reset --hard HEAD^ If you are removing multiple commits from the top, you can run git reset --hard HEAD~2 to remove the last two commits. You can increase the number to remove even more commits.


2 Answers

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
like image 113
Adrian Shum Avatar answered Oct 05 '22 00:10

Adrian Shum


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 
like image 45
Aditya Avatar answered Oct 05 '22 02:10

Aditya