如何在 Git 中推送到上游?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40141425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:33:59  来源:igfitidea点击:

How to push to upstream in Git?

gitgithub

提问by typos

I have a project with a few friends in GitLab, and there is of course the master branch, and there are some others too. When I cloned the repository, I created also an upstream with the command git remote add upstream ....

我和几个朋友在 GitLab 有一个项目,当然有 master 分支,还有一些其他的。当我克隆存储库时,我还使用命令创建了一个上游git remote add upstream ...

Then, I issued the git fetch upstream. Followed by git checkout upstream/test1. Now, if I type git branch -a, I get an output like this:

然后,我发布了git fetch upstream. 紧随其后git checkout upstream/test1。现在,如果我输入git branch -a,我会得到这样的输出:

* (HEAD detached at upstream/test1)
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/upstream/test1
  remotes/upstream/master

This is all fine, but then I did some changes to the code in my upstream/test1branch. I don't want yet to push them to origin/test1, but I want to be able to push my changes to upstream/test1so that my other friend can see it. But, if I issue the following set of commands:

这一切都很好,但后来我对我的upstream/test1分支中的代码进行了一些更改。我还不想将它们推送到origin/test1,但我希望能够将我的更改推送到 ,upstream/test1以便我的其他朋友可以看到它。但是,如果我发出以下命令集:

git add .
git commit -m "Sample message"

After the commit I got the message:

提交后,我收到消息:

[detached HEAD 4f20e95] Sample message
 5 files changed, 12 insertions(+), 1 deletions(-)

And the hash value changes to 4f20e95in my command prompt. Then if I do git push, I get the following error messages:

并且哈希值4f20e95在我的命令提示符中更改为。然后,如果我这样做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>

How can I push to my upstream branch without actually pushing to origin.

如何在不实际推送到原点的情况下推送到我的上游分支。

回答by Scott Weldon

The branch upstream/test1is a remote tracking branch, which can't be updated manually. Such branches track branches on remote servers, and are only updated during git fetch/git push.

该分支upstream/test1是远程跟踪分支,无法手动更新。这样的分支跟踪远程服务器上的分支,并且只在git fetch/期间更新git push

Instead, check out a new local branch first:

相反,首先检查一个新的本地分支:

git checkout -b test1 upstream/test1

And commit there as usual. Since you have already committed, instead do:

并像往常一样在那里提交。既然你已经提交了,那么做:

git checkout -b test1 4f20e95

When you are ready, push to upstream:

准备好后,请推至upstream

git push upstream test1


When you do a plain git push, Git uses default values for the remote and branch to push based on certain config options. However, if you aren't on a branch (thus getting the detached HEADmessage), then Git doesn't know what branch to push, thus giving you the error you received.

当您执行普通 时git push,Git 使用远程和分支的默认值根据某些配置选项进行推送。但是,如果您不在分支上(因此会收到detached HEAD消息),则 Git 不知道要推送哪个分支,从而为您提供收到的错误。