Git pull 入错误的分支

标签 git branch undo pull

我自己和另一位开发人员一直在 merge 并将我们的工作推送到一个名为 toolwork 的非主分支。这样,我们就不会影响团队的其他成员。我的主题分支称为 DPM-93,我的 git 工作流程是这样的。

# do some work
git checkout DPM-93
git commit -m "did some work"

# catch up
git checkout toolwork
git pull origin toolwork

# rebase my topic branch
git checkout DPM-93
git rebase toolwork

# merge and push my changes
git checkout toolwork
git merge --no-ff DPM-93
git push origin toolwork

在我不小心发出这些 git 命令之前,这大部分工作正常

git checkout toolwork
git pull origin master

那时,分支工具中出现了一堆新东西,我不知道如何摆脱它,除非删除我的工作区并从存储库中重新克隆。

有什么办法可以恢复到 pull 前的状态吗?

最佳答案

git reset --hard ORIG_HEAD 

来自git reset man page (如果你只是 pull ):

撤消 merge 或 pull

$ git pull                         (1)
Auto-merging nitfol
CONFLICT (content): Merge conflict in nitfol
Automatic merge failed; fix conflicts and then commit the result.
$ git reset --hard                 (2)
$ git pull . topic/branch          (3)
Updating from 41223... to 13134...
Fast-forward
$ git reset --hard ORIG_HEAD       (4)
  1. Try to update from the upstream resulted in a lot of conflicts; you were not ready to spend a lot of time merging right now, so you decide to do that later.
  2. "pull" has not made merge commit, so "git reset --hard" which is a synonym for "git reset --hard HEAD" clears the mess from the index file and the working tree.
  3. Merge a topic branch into the current branch, which resulted in a fast-forward.
  4. But you decided that the topic branch is not ready for public consumption yet.
    "pull" or "merge" always leaves the original tip of the current branch in ORIG_HEAD, so resetting hard to it brings your index file and the working tree back to that state, and resets the tip of the branch to that commit.

参见 HEAD and ORIG_HEAD了解更多。

关于Git pull 入错误的分支,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3998881/

相关文章:

git - Powershell 在 TortoiseGit Hook 中不返回任何内容

git - 当前分支上有未提交的更改时 checkout 另一个分支

bit-manipulation - 无分支饱和度

vim - VIM中是否有 block 撤消功能?

emacs - undo、save-excursion、delete-region 之间的交互

vim - 我怎样才能保留 Vim 撤消历史但不允许隐藏修改过的缓冲区?

git - ERELEASEBRANCHES 发布分支在 `branches` 配置中无效。当我使用对象时

git - 查看应用于指定 Git 提交中指定代码行的更改?

git - git是否记录分支负责人的历史?

git - 删除本地开发分支