简体   繁体   English

什么是`git push origin master`?帮助git的refs,head和remotes

[英]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做什么的问题:

  • I know that 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 mastergit 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 pushgit pull

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

Git has two types of branches: local and remote . Git有两种类型的分支: localremote 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 pullgit 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. 然后,您可以pushpull ,而无需指定其本地和远程。

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的pushpull你想使用指定的本地和远程分支机构从现在开始,像这样:

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.

相关问题 Git 中的“refs/heads/master”是否与“refs/remotes/origin/master”相同? - Is "refs/heads/master" same as "refs/remotes/origin/master" in Git? git push origin master:refs / heads / master这是做什么的 - git push origin master:refs/heads/master what does this do 致命:refspec的远程部分不是.git / refs / heads / master中的有效名称:.git / refs / remotes / origin / master - fatal: remote part of refspec is not a valid name in .git/refs/heads/master:.git/refs/remotes/origin/master 为什么git fetch origin + refs / heads / master:refs / remotes / origin / mymaster和git fetch origin master:mymaster的行为不同? - Why behaviour of git fetch origin +refs/heads/master:refs/remotes/origin/mymaster and git fetch origin master:mymaster is different? Git push Refspecs:`refs / heads / *:refs / heads / origin`与`refs / heads / *:refs / heads / *` - Git push Refspecs: `refs/heads/*:refs/heads/origin` vs `refs/heads/*:refs/heads/*` Git推送到refs / remotes / mine / master - Git push to refs/remotes/mine/master git fetch不会更新.git / refs / remotes / origin / master - git fetch does not update .git/refs/remotes/origin/master Git refs / remotes / origin / master不指向有效对象 - Git refs/remotes/origin/master does not point to a valid object Git:内部错误:refs / remotes / origin / master不是有效的打包引用 - Git: Internal error: refs/remotes/origin/master is not a valid packed reference Git远程命令返回致命:无效的refspec + refs / heads / *:refs / remotes /:origin / * - Git remote command returns fatal: Invalid refspec +refs/heads/*:refs/remotes/:origin/*
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM