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.
(assume master working copy/repo is at /foo/base/.git)
simple solution
I want to work on an upstream branch other than “master”.
cd /foo/base git branch alt (make sure you have a branch called 'alt' in the main repo before cloning) cd .. git clone /foo/base /foo/alt cd /foo/alt git branch -a (see that alt is not there because only master is tracked; unless another branch was active!) git checkout --track -b alt origin/alt (this creates a local branch 'alt' based on the upstream branch and switches your working copy to that branch; git push, git fetch, git pull are all configured automatically) git pull git push origin alt (you can also just run 'git push' unless you don't want to update other branches)
Your .git/config file will show the alt branch configured like this:
[branch "alt"] remote = origin merge = refs/heads/alt
manual solution
:!:Please use the “simple solution” above instead. This solution is kept to see how it can manually be done.
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.
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
manually adding git pull capability
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.
External Links
* Git Guide (see section titled I want to work on an upstream branch other than “master”)