9

This question is different from other questions in that, I want to keep some commits while I revert some commits.

Here 's my setup.

Upstream repo on github (public repo which I don't own)
Personal repo on my server(git clone --bare $upstream)

My workflow was like this:

Need to customize my personal branch
   1. check out origin/master(Personal)
   2. make changes
   3. push to origin/master

Need to get new feature or bug fix from the original author(github)
   1. git fetch upstream
   2. git merge upstream/master

Now I find upstream/master has a bug and want to revert,
How do I go back to the state before I fetch/merged upstream for the last time?

Edit.

Suppose

  1. I merged upstream
  2. I merged what a team member pushed to origin/master(Personal)

Now I want to undo the step 1.

git reset --hard commit_sha_of_one_prior_the_merge_1

(Although finding the sha is not easy. git log shows a lot of commit sha's of upstream, so it's not easy to find commit-sha-of_one_prior_the_merge_1 which is not of upstream but of origin/master)

How do I keep the step 2 merge?

Suppose a slightly more complex scenario

  1. I merged upstream
  2. I merged what another team member pushed to Personal
  3. I also pushed my work to Personal

Now I want to undo the upstream merge. How can I do it?

eugene
  • 39,839
  • 68
  • 255
  • 489

2 Answers2

3

Since you have an actual merge commit--because you've been introducing your own changes--you should be able to do:

git reset --hard HEAD~1

Assuming your last commit was the merge commit in question.

John Szakmeister
  • 44,691
  • 9
  • 89
  • 79
-1

You can reset your local master branch to the origin/master state (if you haven't push your merge to origin/master) with :

git reset --hard origin/master

If that's not possible you can use the reflog :

git reflog

You can see undoing a git rebase for more details

Community
  • 1
  • 1
cexbrayat
  • 17,772
  • 4
  • 27
  • 22