[英]What is `git push origin master`? Help with git's refs, heads and remotes
I have a question about what git push origin master
does: 我有一个关于git push origin master
做什么的问题:
origin
is the remote (aka GitHub) 我知道origin
是遥控器(又名GitHub) git push origin master
is the same as git push origin master_on_my_machine:master_on_github
git push origin master
与git push origin master_on_my_machine:master_on_github
相同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_on_my_machine
等于/refs/heads/master
master_of_github
is equal to /refs/remotes/origin/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
:
git push
and git pull
. 我想从github上的my_test分支推送和拉取,只需输入git push
和git pull
。 Git has two types of branches: local
and remote
. Git有两种类型的分支: local
和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. 要根据需要使用git pull
和git push
,你必须告诉你的本地分支( my_test
)它正在跟踪哪个远程分支。 In typical Git fashion this can be done in both the config file and with commands. 在典型的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
开关来告诉Git的push
和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 = git@github.com: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: 一些有用的资源:
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
. 将提交从本地主分支推送到(可能是新的)远程分支my_test
,并将master
设置为跟踪origin/my_test
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.