Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undo git pull, how to bring repos to old state

Is there any way to revert or undo git pull so that my source/repos will come to old state that was before doing git pull ? I want to do this because it merged some files which I didn't want to do so, but only merge other remaining files. So, I want to get those files back, is that possible?

EDIT: I want to undo git merge for clarification. After seeing some answers, I did this

git reflog bb3139b... HEAD@{0}: pull : Fast forward 01b34fa... HEAD@{1}: clone: from ...name... 

Now, what should I do ? Doing git reset --hard is OK ? I don't want to screw it again, so asking for detailed steps ?

like image 284
seg.server.fault Avatar asked Aug 03 '09 16:08

seg.server.fault


People also ask

How do I revert to a previous state in git?

git reset --hard This command reverts the repo to the state of the HEAD revision, which is the last committed version. Git discards all the changes you made since that point. Use the checkout command with two dashes, then the path to the file for which you want to revert to its previous state.

How do I revert a pull in git conflict?

In case you've made a mistake while resolving a conflict and realize this only after completing the merge, you can still easily undo it: just roll back to the commit before the merge happened with "git reset --hard " and start over again.


2 Answers

Running git pull performs the following tasks, in order:

  1. git fetch
  2. git merge

The merge step combines branches that have been setup to be merged in your config. You want to undo the merge step, but probably not the fetch (doesn't make a lot of sense and shouldn't be necessary).

To undo the merge, use git reset --hard to reset the local repository to a previous state; use git-reflog to find the SHA-1 of the previous state and then reset to it.

Warning

The commands listed in this section remove all uncommitted changes, potentially leading to a loss of work:

git reset --hard 

Alternatively, reset to a particular point in time, such as:

git reset --hard master@{"10 minutes ago"} 
like image 141
jkp Avatar answered Oct 05 '22 22:10

jkp


Same as jkp's answer, but here's the full command:

git reset --hard a0d3fe6 

where a0d3fe6 is found by doing

git reflog 

and looking at the point at which you want to undo to.

like image 36
Jeffrey Sun Avatar answered Oct 05 '22 23:10

Jeffrey Sun