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:
origin
is the remote (aka GitHub) git push origin master
is the same as 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
If it's equal, should it be possible to do 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
and git pull
. 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.
Commands
Make sure you're on your master
branch with
1) git checkout master
then create the new branch with
2) git branch --track my_test origin/my_test
and check it out with
3) git checkout my_test
.
You can then push
and pull
without specifying which local and remote.
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:
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.
[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.
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
.