简体   繁体   English

pull而不是fetch - 意外地将远程master合并到本地分支

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

I just did git pull instead of git fetch . 我只是做了git pull而不是git fetch I was in a local branch that I didn't want to merge with origin/master . 我在一个本地分支机构,我不想与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. 现在我的暂存区域充满了来自origin/master的变化我想恢复,但是如果我取消它们,它们将与我未经分级的未更改的变化混合。

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. git stash您未提交的更改,以便我们不会丢失它们。
  3. git log -3 , this will show your merge commit along with 2 previous commits. git log -3 ,这将显示您的合并提交以及之前的2次提交。 In the merge commit you will see 2 SHAS. 在合并提交中,您将看到2 SHAS。 One is the latest commit from origin/master and the other the last commit on your branch. 一个是origin / master的最新提交,另一个是您的分支上的最后一次提交。
  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. git reset --hard <SHA of YOUR last commit>这将在你合并之前将你的分支重置为你去掉你从master中引入的所有提交/更改。
  5. git stash pop to get your uncommitted changes back on to your branch. git stash pop可以将您未提交的更改恢复到您的分支。

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. 如果您确实需要任何暂存文件,请执行git reset -- <filenames>以取消它们,然后按照上述步骤操作。

I think you probably want git merge --abort . 我想你可能想要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. 如果在合并开始时存在未提交的工作树更改,则git merge --abort在某些情况下将无法重建这些更改。 It is therefore recommended to always commit or stash your changes before running git merge. 因此,建议在运行git merge之前始终提交或存储您的更改。

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

I have TortoiseGit to manage my git repos and it's pretty convenient. 我有TortoiseGit来管理我的git repos,它非常方便。 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 这些是我要做的步骤(使用TortoiseGit) - 首先存储您的更改(TortoiseGit-> 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. 第二,(右键单击你的repo-> TortoiseGit)“显示日志”,这将显示所有更改日志,(假设你知道你在合并之前的位置 - 也许你已经完成了一些提交)寻找它,并且右键单击该更改日志,在下一节中说“重置为此提交”,选择“硬重置”,这将从所选提交中删除所有提交到顶部,并删除更改。

  • re apply your stash (right click, TortoiseGit->Stash) and then apply the stash you saved earlier 重新应用您的存储(右键单击,TortoiseGit-> Stash),然后应用您之前保存的存储

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" 如果你执行一个$ git log --graph --oneline你将不得不寻找类似于“合并分支”foo“of”remote_x“blah blah”的提交“

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 意外将远程分支合并到本地主服务器。 我该如何退出? - Accidentally merged remote branch to local master. How do I back out? 如何将尚未合并到master中的远程分支拉到本地分支中? - How to take a pull of a remote branch that has not been merged into master into a local branch? git:我不小心将功能分支合并到master中,而不是进行合并 - git: I accidentally merged feature branch into master instead of develop 删除已经与master合并的未使用的分支(本地和远程) - Delete unused branch (local and remote) which is already merged with master 远程分支后面的本地分支(pull,rebase,fetch,merge) - Local branch behind remote branch (pull, rebase, fetch, merge) 当您意外删除本地分支时,如何从远程拉分支? - How to pull a branch from remote when you accidentally deleted local branch? 将其与本地主服务器合并后,是否应该删除LOCAL remote / origin / master分支? - Should I delete the LOCAL remote/origin/master branch after I have merged it with my local master? git request-pull从本地分支到远程主服务器 - git request-pull from local branch to remote master 意外地从主题分支而非主分支 - Accidentally branched from a topic branch instead of master 从主分支恢复合并的拉取请求 - Revert a merged pull request from master branch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM