Mike
Mike

Reputation: 1170

Git - new branch and remote tracking (configure for push/pull with origin/master)

What does Track Remote Branch do? Also what does it mean configure for push/pull with origin/master?

Upvotes: 6

Views: 8007

Answers (3)

Hemendr
Hemendr

Reputation: 1060

Example Scenario:

Suppose you have 1 remote master branch on Azure and 1 local master branch on your PC
enter image description here

On you local PC enter image description here Now enter image description here

After commiting changes on masterA and masterB and pushing changes to remote repository your branches will apper like this on your PC

enter image description here

and on remote repositry like azure branches will apper like this

enter image description here

Upvotes: 4

Greg Bacon
Greg Bacon

Reputation: 139691

Tracking a remote branch means you want to automatically merge changes from the remote branch you’re tracking into your local branch. The default behavior of git push pushes changes into a configured upstream only when the names of the local and remote are the same. In your case, you would not modify origin/master with a push to feature1.

For a feature branch where you’re collaborating with others, you generally want to track, say, origin/feature1 rather than origin/master. If you are working a feature solo, you may not want to set up tracking at all.

The git branch documentation describes what happens under the hood to set up tracking.

When creating a new branch, set up branch.<name>.remote and branch.<name>.merge configuration entries to mark the start-point branch as "upstream" from the new branch. This configuration will tell git to show the relationship between the two branches in git status and git branch -v. Furthermore, it directs git pull without arguments to pull from the upstream when the new branch is checked out.

The same section continues

This behavior is the default when the start point is a remote-tracking branch.

A remote-tracking branch (or just tracking branch for short) has a names of the form remote/branch-name. A git repository typically has a branch named master, so a clone will almost always have a local master branch and a tracking branch named origin/master. Think of origin/master as marking the point in history where the master branch was on your origin remote at the time of your most recent git pull or git fetch.

Upvotes: 8

mnestorov
mnestorov

Reputation: 4494

Here's the docs from git:

A 'tracking branch' in Git is a local branch that is connected to a remote branch. When you push and pull on that branch, it automatically pushes and pulls to the remote branch that it is connected with.

Use this if you always pull from the same upstream branch into the new branch, and if you don't want to use "git pull" explicitly.

If you track your local branches to your remote ones, ie feature1 tracks origin/feature1 and the same for master, then you are safe!

Edit: Here is a great explanation form the ProGit book. Basically:

Tracking branches are local branches that have a direct relationship to a remote branch. If you’re on a tracking branch and type git pull, Git automatically knows which server to fetch from and which branch to merge in.

Upvotes: 1

Related Questions