"You are not currently on a branch" 尝试与上游存储库同步分支时出错

"You are not currently on a branch" error when trying to sync fork with upstream repository

我将 a fork of a GitHub repository. I fixed a bug, wrote a test and created a pull request which was merged 放入原始存储库。

到目前为止,还不错。

现在我改进了本地机器上的测试。我想用这个改进的测试创建一个新的拉取请求。但与此同时,已经对原始的上游存储库进行了其他提交。所以我想让我的复刻与上游存储库同步,以便我可以创建新的拉取请求。

在没有完全理解我在做什么的情况下,我一直在 copy/pasting git 来自其他 Whosebug 答案的命令。但我无法让它工作。

这是目前的情况:

$ git push
fatal: You are not currently on a branch.
To push the history leading to the current (detached HEAD)
state now, use

    git push origin HEAD:<name-of-remote-branch>
$ git status
rebase in progress; onto 4378fa4
You are currently rebasing branch 'master' on '4378fa4'.
  (all conflicts fixed: run "git rebase --continue")

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        Tests/.vscode/

nothing added to commit but untracked files present (use "git add" to track)

或视觉上:

$ git log --all --graph --pretty=format:'%C(auto)%h%C(auto)%d %s %C(dim white)(%aN, %ar)'
* 46f5c0e (HEAD) Change to non-UTF8 locale C (ASCII only) inside test (BioGeek, 3 hours ago)
| *   fe24176 (master) merge changes from upstream (BioGeek, 3 days ago)
| |\
| |/
|/|
* | 4378fa4 (upstream/master) Thank Jerven for ExPASy URL updates (Peter Cock, 3 days ago)
* | 03de45c Update ExPASy tests for HTTPS (Peter Cock, 3 days ago)
* | 47cdbf7 Move expasy URLs to https (Jerven Bolleman, 4 days ago)
* | 9bfb8b1 replace expasy.ch by expasy.org (Jerven Bolleman, 4 days ago)
* | 1a32c50 Determine encoding for urllib handles from HTTP headers (Jeroen Van Goey, 3 days ago)
* | ccf88fc Tests do not require postgresql CREATE DATABASE permissions (Adhemar Zerlotini, 3 days ago)
| | *   6b7d235 (refs/stash) WIP on master: 6311677 remove Doc/examples/tree1.nwk which was committed accidentally (created during testing?) (BioGeek, 3 days ago)
| | |\
| |/ /
| | * 4ead5fa index on master: 6311677 remove Doc/examples/tree1.nwk which was committed accidentally (created during testing?) (BioGeek, 3 days ago)
| |/
| * 6311677 (origin/master, origin/HEAD) remove Doc/examples/tree1.nwk which was committed accidentally (created during testing?) (BioGeek, 3 days ago)
| * 41ff4cc Address flake8 warnings (BioGeek, 3 days ago)
| * 815bbfd Add encoding and prefic unicode string with 'u' (BioGeek, 3 days ago)
| * 0e7451d If the handle has a headers attribute, then use it's content charset as encoding. Otherwise fall back to the default behaviour. (BioGeek, 4 days ago)
| * 6a7d05b Add test for correct handling of encodings in Entrez.efetch. Also add some missing docstrings in other tests and close handles everywhere (BioGeek, 4 days ago)
|/
* 4ccdace mmCIF parser fix to check for both '?' and '.' as unassigned values (Joao Rodrigues, 5 days ago)

变更集 46f5c0e 是改进的测试,我想从中创建新的拉取请求。

如何将我的复刻与上游存储库同步,以便我可以创建新的拉取请求?

如 git 中有些有用的错误消息所述:

$ git status
rebase in progress; onto 4378fa4
You are currently rebasing branch 'master' on '4378fa4'.
  (all conflicts fixed: run "git rebase --continue")

继续变基

这意味着您可能 git pull -r 来自您的上游,或者正在将一个分支重新定位到本地的另一个分支,并且您在尝试自动合并时遇到了问题(可能是合并冲突)。

当git在尝试快进时遇到合并冲突时,它会停止变基并等待用户手动修复问题。

完成后,您需要暂存已修复的更改文件(使用 git add <pathspec>)但不进行提交。暂存文件后,git 将知道您的更改并在您 git rebase --continue.

时尝试集成它们

继续解决问题,git rebase --continue直到完成。这会将您的 HEAD 移至工作分支的顶端。然后你就可以推了。

或者

您可以 git rebase --abort 放弃整个事情,这也将撤消您在此过程中所做的任何修复,并尝试另一种合并策略,例如三向合并。

与快进相比,三向合并的一个优点是所有合并冲突都将同时出现,您可以创建一个包含所有修复的新合并提交。然而,在快进时,您可能需要多次停下来解决冲突,因为 git 在将连续提交应用到您的工作分支时遇到它们。

个人看法

给定

Without fully grasping what I was doing, I've been copy/pasting git commands from other Whosebug answers. But I can't get it to work.

我会 git rebase --abort 从头开始​​。如果您多次停止并且不得不修复许多合并冲突,您可以使用 git pull --no-ff <remote-name> <local-branch> 明确指示 git 不要尝试快进,即使它可以。

最后的选择

如果历史记录难以管理并且您的更改很小,您可以随时 "shelve" 您的更改,这意味着将更改文件的版本复制到您的桌面 and then overwrite your local branch with the exact content of the upstream。然后,只需将更改的文件复制回您的存储库并进行提交。

这不是一个优雅的解决方案,但它确实有效,而且有时比管理悠久的历史要容易得多。但是你会失去你的承诺。 preserve them with patching有一个方法,如果你真的感兴趣的话。

进一步阅读

更多关于什么detatched HEAD means