简体   繁体   中英

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

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?

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.

I think you probably want git merge --abort .
From the documentation:

Abort the current conflict resolution process, and try to reconstruct the pre-merge state.

If there were uncommitted worktree changes present when the merge started, git merge --abort will in some cases be unable to reconstruct these changes. It is therefore recommended to always commit or stash your changes before running git merge.

http://git-scm.com/docs/git-merge

I have TortoiseGit to manage my git repos and it's pretty convenient. these are the steps that I'd do (using TortoiseGit) - first stash your changes (TortoiseGit->Stash) and name appropriately, so all your uncommitted changes are saved onto the stash

  • second, (right click in your repo->TortoiseGit) "Show Log" and that will bring up all the change logs, (assuming you know where you were before the merge - maybe you had some commits already done) look for it, and right click on that change log, say "reset to this commit" in the next section choose "Hard Reset" this will remove all the commits from the selected commit to top and also remove the changes.

  • re apply your stash (right click, TortoiseGit->Stash) and then apply the stash you saved earlier

You can just revert the merge step and it will append a new commit that undoes all the file changes from the merge

$ git revert <merge_commit_hash>

If you do a $ git log --graph --oneline you'll have to look for the commit that says something like: "Merge branch "foo" of "remote_x" blah blah"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM