【问题标题】:"You are not currently on a branch" error when trying to sync fork with upstream repository尝试将 fork 与上游存储库同步时出现“您当前不在分支上”错误
【发布时间】:2018-04-11 13:43:29
【问题描述】:

我制作了a fork of a GitHub repository。我修复了a bug,编写了一个测试并在原始存储库中创建了a pull request,即merged

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

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

在没有完全掌握自己在做什么的情况下,我一直在从其他 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 是改进的测试,我想从中创建一个新的拉取请求。

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

【问题讨论】:

    标签: git github rebase git-fork


    【解决方案1】:

    正如 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 &lt;pathspec&gt;),但不要提交。暂存文件后,git 将知道您的更改并在您 git rebase --continue 时尝试集成它们。

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

    或者

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

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

    我个人的看法

    给定

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

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

    最后的选择

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

    这不是一个优雅的解决方案,但它确实有效,而且有时比管理悠久的历史要容易得多。但是你会失去你拥有的提交。如果您真的有兴趣,可以联系preserve them with patching

    进一步阅读

    更多关于detatched HEAD means的内容。

    【讨论】:

    • 感谢您的详细解答!我有一些阅读要做,以便更好地理解我在做什么,但我会听从你的建议,做git rebase --abort
    • @BioGeek 我很高兴。我花了几分钟阅读your fork 并看到您的历史确实有所不同This branch is 7 commits ahead, 17 commits behind biopython:master. rebase 通常是合适的解决方案。也许只是中止并再次尝试快进(所以git pull -r &lt;upstream-name&gt; master)从一个新的开始,解决冲突,继续,然后推动。然后你可以用 biopython 对比一下分支,看看是不是好看。
    • @BioGeek 顺便说一句,我在日常工作和操作系统贡献中都养成了习惯,基本上每次我坐下来开始工作时都从上游拉动,以确保潜在的合并数量冲突是可控的。在那个频率下并不总是必要的,但很方便。
    • 我成功创建了pull request!它仍然不完美(git 提交历史给出了合并冲突),但我们正在取得进展。 :-)
    • @BioGeek 很高兴听到它!我最初的几个公共 PR 是完全一团糟,你做得很好。 Git 分支允许你创建测试分支并不受惩罚地做任何你想做的事情,所以继续试验吧。
    猜你喜欢
    • 2014-09-11
    • 1970-01-01
    • 1970-01-01
    • 2014-11-15
    • 2022-11-21
    • 2017-01-11
    • 2018-12-20
    • 1970-01-01
    相关资源
    最近更新 更多