Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Git, local branches can track one another - how is this useful?

Tags:

I heard that in Git, you can let a local branch A track another local branch B.

Why would someone want to do that?

like image 405
python dude Avatar asked Dec 08 '11 14:12

python dude


People also ask

What does tracking a branch do?

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. Now, your local branch sf will automatically pull from origin/serverfix .

Why are branches in Git useful?

Branches serve as an abstraction for the edit/stage/commit process. You can think of them as a way to request a brand new working directory, staging area, and project history. New commits are recorded in the history for the current branch, which results in a fork in the history of the project.

How does Git keep track of branches?

Git stores all references under the . git/refs folder and branches are stored in the directory . git/refs/heads . Since branch is a simple text file we can just create a file with the contents of a commit hash.


1 Answers

The main things that come to mind for having a local branch track another local branch are (1) more informed messages from Git regarding a branch being ahead/behind of the tracked branch and (2) trigger hooks.

One area Git displays more information is when creating a branch. Creating a basic branch looks like the following:

$ git co -b A master Switched to a new branch 'A' 

While creating a tracking branch looks like:

$ git co --track -b B master Branch B set up to track local branch master. Switched to a new branch 'B' 

This would add the following in .git/config:

[branch "B"]     remote = .     merge = refs/heads/master 

After committing some changes on branches A and B, executing git status -s -b on branch A displays ## A while on branch B it displays ## B...master [ahead 1, behind 1], providing some quick information regarding the relationship between branches B and master.

The other area where you might want a local branch track another local branch is to trigger hooks; in particular pre-receive, update, post-receive and post-update during a git push. You might have hooks to, for example, trigger a build on a continuous integration server, do some license header checks, check for white space format errors, etc.

like image 109
Dan Cruz Avatar answered Nov 05 '22 20:11

Dan Cruz