git - 尝试将 fork 与上游存储库同步时出现 "You are not currently on a branch"错误

标签 git github rebase git-fork

我做了a fork of a GitHub repository 。我修复了a bug ,编写了一个测试并创建了 a pull request这是 merged到原始存储库中。

到目前为止,一切都很好。

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

在没有完全掌握我在做什么的情况下,我一直在从其他 StackOverflow 答案中复制/粘贴 git 命令。但我无法让它发挥作用。

目前的情况如下:

$ 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)

Changeset 46f5c0e 是改进的测试,我想从中创建新的 pull 请求。

如何将我的分支与上游存储库同步,以便我可以创建新的 pull 请求?

最佳答案

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

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

继续 rebase

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

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

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

继续解决问题并git rebase --continue直到完成。这会将您的头部移动到工作分支的尖端。然后您可以推送。

或者

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

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

我个人的看法

给定

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

我会git rebase --abort并从头开始。如果您多次停止并必须解决许多 merge 冲突,您可以使用 git pull --no-ff <remote-name> <local-branch>明确指示 git 不要尝试快进,即使可以。

最后的选择

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

这不是一个优雅的解决方案,但它有效,有时比管理悠久的历史要容易得多。但你将失去你所拥有的 promise 。有一种方法可以preserve them with patching ,如果你真的感兴趣的话。

进一步阅读

更多关于什么detatched HEAD means .

关于git - 尝试将 fork 与上游存储库同步时出现 "You are not currently on a branch"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47017608/

相关文章:

windows - 如何在 Windows 上通过 ssh 阻止 git 解析错误的路径?

git - 如何使用 SSH URL 推送到存储库

Github:删除 fork 后 fork

git - 如何在 git 中备份私有(private)分支

git - 在没有 refspec 参数的情况下运行 git push、pull 和 fetch

C# LOC 使用 GIT 计数

git - 禁用 (GitGutter) 关于提交 Sublime Text 的消息

docker - 推送图像时GitHub CI解析HTTP 404响应主体

git - 如何修复 GitHub pull 请求中被 git rebase 破坏的提交顺序?

Git 工作流 : Rebasing Published/Shared Branches