2

Hi guys : Im not particularly excited about git branches, but my organization uses them heavily. In any case, I need to work on a branch of head. In the past I have "switched" branches while developing in a single place, however, I feel like this gets confusing.

Is it common/idiomatic in git to simply have two separate git directories, one for a branch1, and another for branch2 ?

Or

Is it more common to have a single working directory, switching between branches in that directory .

Also, im curious about the benefits/drawbacks of these two approaches. If either is completely ridiculous, please let me know as well - im somewhat new to git.

svick
  • 236,525
  • 50
  • 385
  • 514
jayunit100
  • 17,388
  • 22
  • 92
  • 167

3 Answers3

4

It is very common to switch between branches. If you have some unsaved changes, use git stash command (see in the documentation).

It is very uncommon to have separate directories for different branches from the same repository - this is unnecessary, inefficient and reasonable only if you have eg. two working applications / websites at the same time, one for each branch.

Tadeck
  • 132,510
  • 28
  • 152
  • 198
2

Git makes branching easy -- use branches.

Having one directory per branch is woefully inefficient: it means you have two git repositories, and you'd need to keep both in sync.

Among other niceties, when you switch branches (using git checkout branchname), git will only ever rewrite files which change between the two branches. This is MUCH more efficient than a mechanism when you have to manage n directories.

If you want to create a branch off of master, just git branch newbranch master and git checkout newbranch -- or in one command, git checkout -b newbranch master.

fge
  • 119,121
  • 33
  • 254
  • 329
1

The idea behind switching branches within one directory is sustained by:

  • the high frequency of merges between branches which means:
    • the delta between your usual branches isn't very important
    • as a consequence of that small delta, the switch itself is very (very) fast)

.

  • the cost associated with the definition of a new environment for a different path (which means your IDE -- Eclipse or Visual Studio -- projects and other configuration files might have to adjust to a new path.
    If everything is done within one directory, you only define your development environment once.
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • See also http://stackoverflow.com/questions/2850369/why-does-git-use-fast-forward-merging-by-default/2850413#2850413 – VonC Jan 11 '12 at 15:24