docs:git:working_copies_of_multiple_branches

This is an old revision of the document!


working copies of multiple branches

Sometimes during development we need to have working copies of more than one branch at the same time. With svn this was simple, because working copies of each branch (and/or trunk) would exist at the same time after a checkout.

A single working copy using git would only reflect the active branch. My preference for doing this with git was to have a “master” working copy with my repo that would hold all changes, and another cloned copy that would act as the alternate branch.

(assume master working copy/repo is at /foo/base/.git)

mkdir /foo/alt
cd /foo/alt
git clone /foo/base .
git branch alt
git checkout alt

This line actually modifies your .git/config file:

git config remote.origin.push alt:alt

The way it modifies the file looks like this:

# BEFORE
[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = /htdocs/public_html/js/Autofill-clone/../Autofill

# AFTER
[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = /htdocs/public_html/js/Autofill-clone/../Autofill
    push = alt:alt
# work work work
git push

Output should be something similar to this:

Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 424 bytes, done.
Total 3 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To /foo/base/
 * [new branch]      alt -> alt

git pull actually does a git fetch then a git merge operation for you. If you want to also be pulling (merging) any changes that occur on this alt branch on the main repo (/foo/base), you can run this:

git config branch.alt.remote origin
git config branch.alt.merge refs/heads/alt

The above commands will modify your /foo/alt/.git/config file as follows:

[branch "alt"]
     remote = origin
     merge = refs/heads/alt

You should now be able to 'git pull' and merge any changes from the main repo.

  • docs/git/working_copies_of_multiple_branches.1257665015.txt.gz
  • Last modified: 2009/11/08 00:23
  • by billh