什么是“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
What is `git push origin master`? Help with git's refs, heads and remotes
提问by Terry
I have a question about what git push origin master
does:
我有一个关于什么的问题git push origin master
:
- I know that
origin
is the remote (aka GitHub) git push origin master
is the same asgit 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_machine
is equal to/refs/heads/master
master_of_github
is 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 push
and git pull
when:
最后,我想做的只是输入git push
和git pull
何时:
- I'm on a master branch
- I want to push and pull from a my_test branch on github, only typing
git push
andgit pull
.
- 我在主分支上
- 我想从 github 上的 my_test 分支推送和拉取,只输入
git push
和git pull
.
回答by ghickman
Git has two types of branches: local
and remote
. To use git pull
and git push
as 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 有两种类型的分支:local
和remote
. 要使用git pull
并且git push
如您所愿,您必须告诉您的本地分支 ( my_test
) 它正在跟踪哪个远程分支。在典型的 Git 方式中,这可以在配置文件和命令中完成。
Commands
命令
Make sure you're on your master
branch 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 push
and pull
without specifying which local and remote.
然后push
,您可以pull
无需指定本地和远程。
However if you've already created the branch then you can use the -u
switch to tell git's push
and pull
you'd like to use the specified local and remote branches from now on, like so:
但是,如果您已经创建了分支,那么您可以使用-u
switch 来告诉 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/config
and 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_test
to 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_test
and sets up master
to track origin/my_test
.
将提交从本地 master 分支推送到(可能是新的)远程分支my_test
并设置master
为跟踪origin/my_test
.