Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single working branch with Git

Tags:

git

Bounty short description

Is there a portable way to use a single repository w/ multiple checkouts? As an alternative to having multiple clones where there is just too much overhead (pushing/pulling/syncing...) and the risk of overwriting the .git/objects hard links (which does not even work on Windows).


New to git and curious to hear thoughts from experienced git users.

Is there a conceptual reason why git only works with ONE branch at a time? It seems absolutely impractical having to switch back and forward between branches, when most of the time I need to work on at least two different branches at the same time, e.g. building, running in parallel a.s.o.

Ok, so maybe a developer does not really need to work on two branches exactly at the same time. But checking out another branch does not automatically carry ignored stuff like build output files. So a new rebuild is required a.s.o.

There is this script git-new-workdir that is supposed to allow multiple working branches, but for one, it is not part of the git release althoug it has been around for about 3 years, so I do not trust it to keep my files consistent. And secondly, I cannot find it as part of the Windows distribution, which is one of the machines I use for development.

So the only official option is to create a new "clone" for every branch, which seems incorrect, since each clone is a full-blown repository. I would not even know what to call the clone directories -- do I use the repository name or the branch name or both? What if I create another branch off that branch, a.s.o.

Real-use case update (@Philip's suggestion)

I usually work on two major/minor releases, with features going on both in parallel. There is also an occasional development branch, where experimental features go into, before being merged to some future release.

During feature development, often behaviour degrades and it is more efficient to compare it with the behaviour before the changes. Checking out a previous branch/revision is not good enough, because many times it comes down to debugging side-by-side, which means both revisions need to be checked out simultaneously on the harddrive.

So the more convenient and natural approach would be to keep a few so-called "active" branches checked out each in its own directory, with its own compiled binaries. This would also save compilation time and occasional local setup (i.e. configuration files that need to be changed after every checkout in order to get the product running).

like image 450
Nick Avatar asked Sep 07 '11 09:09

Nick


People also ask

What is single branch in git?

The git clone –single-branch –branch command clones a specific branch. This command lets you copy the contents of a repository without downloading all the branches on the repository. It is useful if a repository is large and you only want to download the code you will use.

What is a working branch in git?

A branch represents an independent line of development. Branches serve as an abstraction for the edit/stage/commit process. You can think of them as a way to request a brand new working directory, staging area, and project history.


2 Answers

A branch when checked out is only a pointer to a commit (within a graph of commit).
As such, Git cannot check out two commits of that graph at the same time.

Actually, see "Multiple working directories with Git?".
With Git 2.5+ (Q2 2015), you can have multiple working trees for one git repo, with git checkout --to=<path>.

That allows you to Check out a branch in a separate working directory at <path>. A new working directory is linked to the current repository.

That link is "portable" (ie works even on Windows) as it is recorded in the main repo $GIT_DIR/worktrees directory.


Original answer (2011):

git branches

(Source: Scott Chason, ProGIT Book, http://progit.org/book/ch3-1.html, CC-BY-NC-SA)

If you need to work on two different branches at the same time, simply clone your repo and select (git checkout) the other branch in that clone. You can use the name of the branch as the name of the root directory for that clone.
And you can create branches in any of those repos.


So the question is why can't Git have TWO pointers, one for each directory? –

As mentioned in "Popularity of Git/Mercurial/Bazaar vs. which to recommend", Git is at its core a content management. Each commit represents a full snapshot of the repo.
You cannot view two contents in the same container (working directory), even though you can keep reference of as many pointer (branches) as you want.

Git Local operations

You only populate the working directory with one content.

like image 184
VonC Avatar answered Oct 14 '22 11:10

VonC


If you git clone with a local path, everything under .git/objects (that is, most of the commits and data in your repository) is hardlinked to the old repo wherever possible and takes up next to no disk space. So if you want two different working directories, it's not very expensive to just clone your repo locally. Trying to manage two different working directories from one repository might work, but it's not worth the trouble.

Basically, run git clone /path/to/old/repo new_repo and everything will Just Work.

like image 37
Clueless Avatar answered Oct 14 '22 12:10

Clueless