Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the use-cases for local branches tracking other local branches?

In Git we can do this:

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

What are the use-cases of such branches?

like image 402
Eli Bendersky Avatar asked Dec 26 '12 19:12

Eli Bendersky


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.

How do I track a local remote branch?

When you're publishing a local branch. You can tell Git to track the newly created remote branch simply by using the -u flag with "git push".


1 Answers

Setting a local branch to track another local branch isn't generally useful, but I can think of a couple of cases where it could be handy:

  • You may have some aliases or scripts that do stuff with upstream branches (e.g., via the @{u} shorthand; see git help revisions). If so, configuring a local branch to track another local branch would allow you to test your aliases or scripts without affecting the remote repository or creating a temporary dummy repository.

  • Suppose you are working on a new local branch named foo off of your local master branch (which tracks origin/master) but you aren't ready to publish foo to origin yet. Or maybe you don't ever want to push to origin -- perhaps the commits added a bunch of obnoxious printf() calls you sprinkled in to help with debugging. Anyway, whenever master is updated, you want to rebase foo onto master. Normally you would just type git rebase master, but if you're like me you may find yourself frequently typing just git rebase out of habit. If you do, you'll get this error message:

    $ git rebase
    There is no tracking information for the current branch.
    Please specify which branch you want to rebase against.
    See git-rebase(1) for details
    
        git rebase <branch>
    
    If you wish to set tracking information for this branch you can do so with:
    
        git branch --set-upstream-to=origin/<branch> foo
    

    You may be tempted to do this:

    git branch --set-upstream-to=origin/master foo
    

    but that can have devastating effects if you're in the habit of typing git push every few commits (you'll end up pushing your work-in-progress commits on your local foo branch to origin's master branch, assuming you have push.default set to upstream like I do).

    Instead, you can configure the local foo branch to track your local master branch. Now your habit of typing git rebase does what you want, and an absent-minded git push will only mess up your local master branch, not origin's master branch.

    But it's usually not worthwhile to configure a local branch to track another local branch just for git rebase. I think it's easier to live with remembering to type git rebase master instead of git rebase. If I forget, I get an error message that jogs my memory and I try again. No big deal.

like image 174
Richard Hansen Avatar answered Sep 20 '22 21:09

Richard Hansen