docs:git:working_copies_of_multiple_branches

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
docs:git:working_copies_of_multiple_branches [2009/11/07 22:50] – created billhdocs:git:working_copies_of_multiple_branches [2009/11/08 01:07] (current) billh
Line 1: Line 1:
 ====== working copies of multiple branches ====== ====== 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. +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.
- +
-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) (assume master working copy/repo is at /foo/base/.git)
 +
 +===== simple solution =====
 +I want to work on an upstream branch other than "master".
 +<code>
 +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)
 +</code>
 +
 +Your .git/config file will show the alt branch configured like this:<code>
 +[branch "alt"]
 +    remote = origin
 +    merge = refs/heads/alt
 +</code>
 +
 +===== 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.
 +
 <code> <code>
 mkdir /foo/alt mkdir /foo/alt
Line 11: Line 40:
 git branch alt git branch alt
 git checkout alt git checkout alt
 +</code>
 +
 +This line actually modifies your .git/config file:<code>
 git config remote.origin.push alt:alt git config remote.origin.push alt:alt
 +</code>
 +
 +The way it modifies the file looks like this:<code>
 +# 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
 +</code>
 +
 +<code>
 # work work work # work work work
 git push git push
Line 27: Line 75:
  * [new branch]      alt -> alt  * [new branch]      alt -> alt
 </code> </code>
 +
 +===== 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:<code>
 +git config branch.alt.remote origin
 +git config branch.alt.merge refs/heads/alt
 +</code>
 +
 +The above commands will modify your /foo/alt/.git/config file as follows:<code>
 +[branch "alt"]
 +     remote = origin
 +     merge = refs/heads/alt
 +</code>
 +
 +You should now be able to 'git pull' and merge any changes from the main repo.
 +
 +===== External Links =====
 +* [[http://www.sourcemage.org/Git_Guide|Git Guide]] (see section titled **I want to work on an upstream branch other than "master"**)
 +
  • docs/git/working_copies_of_multiple_branches.1257659455.txt.gz
  • Last modified: 2009/11/07 22:50
  • by billh