Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I revert `git fetch upstream; git merge upstream/master`? [duplicate]

Tags:

git

revert

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?

like image 210
eugene Avatar asked May 07 '13 09:05

eugene


People also ask

How do I revert a git merge?

You can use the Git reset command to undo a merge. Firstly, you need to check for the commit hash (or id) so you can use it to go back to the previous commit. To check for the hash, run git log or git reflog . git reflog is a better option because things are more readable with it.

Can you undo a git fetch?

There is no command to explicitly undo the git pull command. The alternative is to use git reset, which reverts a repository back to a previous commit.

How do I revert a master branch to a previous merge?

In case you are using the Tower Git client, undoing a merge is really simple: just press CMD+Z afterwards and Tower will undo the merge for you!


1 Answers

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.

like image 95
John Szakmeister Avatar answered Oct 18 '22 10:10

John Szakmeister