Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
2 views
in DevOps and Agile by (19.7k points)

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 the 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. 

 

1 Answer

+1 vote
by (62.9k points)

Git has 2 forms of branches: local and remote. To use git pull and git push as you want, you have got to tell your local branch (say, my_test) which remote branch it's tracking.

In typical git fashion, this will 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 while not specifying which local and remote.

However if you have already created the branch then you'll use the -u switch to inform git's push and pull you'd like to use the required 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 favorite editor open up your project's .git/config and add the subsequent 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.

Browse Categories

...