Wednesday, April 9, 2014

Git Tracking remote branches

Why i need to track some remote branch?

First af all, It's nessesary for git system to know where to push/pull the changes from your local branch. Whatever, u can explicity define where u want to push your changes via using the $ git push <remote-name> <branch-name>. But let's ssume that u're a bit lazy and won't to type your remote branch name extra times when u're working in the same branch all the time. So Let's try it:


# Create the local branch 'boom_branch' and switch on it:
$ git branch boom_branch

$ git checkout boom_branch
Switched to branch 'boom_branch'

# Create the remote branch 'boom_branch' from it:
$ git push origin boom_branch
To ...
 * [new branch]      boom_branch -> boom_branch

# Assume that meanwhile there were some modifications
# so u want to invoke pull or push command without the extra typing
$ git pull
There is no tracking information for the current branch...

# This happenes cause git doesn't know about  
# with what remote branch u want to interact with:(
To define the remote branch to interact with, U have just to start track it.
So we can do it in a few ways:

If u already have a local and remote branches (our case) u can track it via using $ git branch --set-upstream-to <remote-name>/<branch-name>

# Track your remote branch 'bloom_branch':
$ git branch --set-upstream-to origin/boom_branch
Branch boom_branch set up to track remote branch boom_branch from origin.

# Or u could to use the short form
$ git branch --u origin/boom_branch

# Let's try to pull now (great-it's working)
$ git pull
Already up-to-date. 
U also had an ability to track this branch at once during creation via using -u argument:
$ git push -u <remote-name> <branch-name>.

# Create the remote branch 'boom_branch' and track at once:
$ git push -u origin boom_branch
To ...
 * [new branch]      boom_branch -> boom_branch
Branch boom_branch set up to track remote branch boom_branch from origin.

# Let's try to pull now (great-it's working)
$ git pull
Already up-to-date.
You just saw that tracking branches are local branches that have a direct relationship to a remote branch. So If you’re on a tracking branch, Git automatically knows(with no arguments) which server and branch to push to or pull from.

When you clone a repository, it generally automatically creates masterbranch that tracks origin/master and pull/push work from box.

Also when you checkout a new branch, and you are using a remote branch as the starting point, the upstream tracking branch will be setup automatically:

# Checkout remote branch 'boom_branch' as a new local branch
# with the same name and track it at once:
$ git checkout boom_branch
Branch boom_branch set up to track remote branch boom_branch from origin.
Switched to a new branch 'boom_branch'
Or if u want to have a local branch with the different name use $ git checkout -b <local-branch-name> <remote-name>/<branch-name>:

# Checkout remote branch 'boom_branch' as a new local branch
# with the 'local_boom' name and track it at once:
$ git checkout -b local_boom_branch origin/boom_branch
Branch local_boom_branch set up to track remote branch boom_branch from origin.
Switched to a new branch 'local_boom_branch'
So now u know that upstream tracking branches are incredibly useful:)
@see Git tips & trics

No comments:

Post a Comment