Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

I have a question about what git push origin master does:

  • I know that 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:

  • 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.
like image 911
Terry Avatar asked Sep 05 '11 19:09

Terry


People also ask

What does git push origin master do?

git push origin master will push your changes to the remote server. "master" refers to master branch in your repository. If you want to push your changes to any other branch (say test-branch), you can do it by: git push origin test-branch. This will push your code to origin of test-branch in your repository.

What is master and origin master in git?

One can push and pull data from a remote repository when you need to share work with teams. Origin and Master are two different terminologies used when working and managing the git projects. Origin is the name used for the remote repository. Master is the name of the branch.

What does git push origin head mean?

git push origin HEAD:master. Push the current branch to the remote ref matching master in the origin repository. This form is convenient to push the current branch without thinking about its local name.

What is git refs remotes origin head?

origin/HEAD is a local ref representing a local copy of the HEAD in the remote repository. (Its full name is refs/remotes/origin/HEAD.)


1 Answers

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 = [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.

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

Some useful resources:

  • http://www.kernel.org/pub/software/scm/git/docs/git-branch.html
  • http://gitready.com/beginner/2009/03/09/remote-tracking-branches.html
like image 105
ghickman Avatar answered Sep 22 '22 02:09

ghickman