什么是“git push origin master”?帮助 git 的 refs、heads 和 remotes

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7311995/
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-10 11:47:23  来源:igfitidea点击:

What is `git push origin master`? Help with git's refs, heads and remotes

git

提问by Terry

I have a question about what git push origin masterdoes:

我有一个关于什么的问题git push origin master

  • I know that originis the remote (aka GitHub)
  • git push origin masteris the same as git push origin master_on_my_machine:master_on_github
  • 我知道那origin是遥控器(又名 GitHub)
  • git push origin master是相同的 git push origin master_on_my_machine:master_on_github

I don't know if:

我不知道:

  • master_on_my_machineis equal to /refs/heads/master
  • master_of_githubis equal to /refs/remotes/origin/master
  • master_on_my_machine等于 /refs/heads/master
  • master_of_github等于 /refs/remotes/origin/master

If it's equal, should it be possible to do git push origin refs/heads/master:refs/heads/origin/master?

如果相等,应该可以做 git push origin refs/heads/master:refs/heads/origin/master吗?

Finally, what I want to do is only type git pushand git pullwhen:

最后,我想做的只是输入git pushgit pull何时:

  • I'm on a master branch
  • I want to push and pull from a my_test branch on github, only typing git pushand git pull.
  • 我在主分支上
  • 我想从 github 上的 my_test 分支推送和拉取,只输入git pushgit pull.

回答by ghickman

Git has two types of branches: localand remote. To use git pulland git pushas you'd like, you have to tell your local branch (my_test) which remote branch it's tracking. In typical Git fashion this can be done in both the config file and with commands.

Git 有两种类型的分支:localremote. 要使用git pull并且git push如您所愿,您必须告诉您的本地分支 ( my_test) 它正在跟踪哪个远程分支。在典型的 Git 方式中,这可以在配置文件和命令中完成。

Commands

命令

Make sure you're on your masterbranch with

确保你在你的master分支上

1)git checkout master

1)git checkout master

then create the new branch with

然后创建新分支

2)git branch --track my_test origin/my_test

2)git branch --track my_test origin/my_test

and check it out with

并检查它

3)git checkout my_test.

3)git checkout my_test

You can then pushand pullwithout specifying which local and remote.

然后push,您可以pull无需指定本地和远程。

However if you've already created the branch then you can use the -uswitch to tell git's pushand pullyou'd like to use the specified local and remote branches from now on, like so:

但是,如果您已经创建了分支,那么您可以使用-uswitch 来告诉 gitpush并且pull您希望从现在开始使用指定的本地和远程分支,如下所示:

git pull -u my_test origin/my_test
git push -u my_test origin/my_test

Config

配置

The commands to setup remote branch tracking are fairly straight forward but I'm listing the config way as well as I find it easier if I'm setting up a bunch of tracking branches. Using your favourite editor open up your project's .git/configand add the following to the bottom.

设置远程分支跟踪的命令相当简单,但我列出了配置方式,如果我设置一堆跟踪分支,我发现它更容易。使用您最喜欢的编辑器打开您的项目.git/config并将以下内容添加到底部。

[remote "origin"]
    url = [email protected]:username/repo.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "my_test"]
    remote = origin
    merge = refs/heads/my_test

This specifies a remote called origin, in this case a GitHub style one, and then tells the branch my_testto use it as it's remote.

这指定了一个名为 的远程origin,在本例中为 GitHub 风格的远程,然后告诉分支my_test使用它作为远程。

You can find something very similar to this in the config after running the commands above.

运行上述命令后,您可以在配置中找到与此非常相似的内容。

Some useful resources:

一些有用的资源:

回答by tfischbach

Or as a single command:

或者作为单个命令:

git push -u origin master:my_test

Pushes the commits from your local master branch to a (possibly new) remote branch my_testand sets up masterto track origin/my_test.

将提交从本地 master 分支推送到(可能是新的)远程分支my_test并设置master为跟踪origin/my_test.