Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pull instead of fetch - accidentally merged remote master into local branch

Tags:

git

I just did git pull instead of git fetch. I was in a local branch that I didn't want to merge with origin/master. Moreover, I had some uncommited changes.

Now my staging area is full of changes from origin/master I'd like to revert, but if I unstage them, they will mix with my uncommited changes in unstaged.

How can I remedy this situation?

like image 873
prasopes Avatar asked Dec 29 '25 19:12

prasopes


1 Answers

I am assuming that the whole mess of changes are due to conflicts from your merge. What I would suggest to do is.

  1. Commit the changes from the merge (finishing it). You will end up with a commit for now this is temporary (DO NOT PUSH!!!)
  2. git stash your uncommitted changes so that we don't lose them.
  3. git log -3, this will show your merge commit along with 2 previous commits. In the merge commit you will see 2 SHAS. One is the latest commit from origin/master and the other the last commit on your branch.
  4. git reset --hard <SHA of YOUR last commit> This will reset your branch to before you merged and get rid of all the commits/changes that you brought in from master.
  5. git stash pop to get your uncommitted changes back on to your branch.

Because your changes are all on your local branch, you can modify history to get things back to a state that you want without any problems. If there are any staged files that you actually want do a git reset -- <filenames> to unstage them and then follow the above steps.

like image 137
Schleis Avatar answered Dec 31 '25 11:12

Schleis