开发者

Single working branch with 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 supp开发者_如何学Pythonosed 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).


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):

Single working branch with Git

(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.

Single working branch with Git

You only populate the working directory with one content.


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.


What you are wanting is called alternates, which lets one repo be dependent on another for its objects. Its use isn't very common because disk space is cheap and you have to be careful not to accidentally delete objects the dependent repo needs.


If you want to be doing this,

I'd 100% recommend to write a custom application1 that manages this process in a foolproof way.

A starting point could be

GIT_WORK_TREE=/worktrees/stable   git checkout -f origin/stable
GIT_WORK_TREE=/worktrees/unstable git checkout -f origin/unstable

etc.

I would specifically make sure that none of your working copies look (remotely) like a usual repository so that normal git commands do not apply.

Using the usual git (sub)commands is recipe for disaster because someday you'll run into a script that doesn't use the GIT_DIR, GIT_WORK_TREE, GIT_INDEX quite the way it should (or you expected).


1 (perhaps based on NGit, or similar Git bindings for Python, Ruby -- watever is portable in your universe)


Actually, git-new-workdir was made for exactly this problem. I have the same problem, and use it without any issues. About your reservations:

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.

Well, I (and many others) have used it for years, and I have not come across any bugs myself, nor have I heard of any real problems (apart from a minor limitations, see below). That may not sound like much, but even with git itself (or any -free- SW project, for that matter), the only real guarantee you get is "no one has found a problem yet". I don't know why git-new-workdir is still in contrib, but I don't think that should deter you.

And secondly, I cannot find it as part of the Windows distribution, which is one of the machines I use for development.

That may just be because you distribution chose not to include the contrib/ stuff.

If you use git under Cygwin, you can just download and use the original git-new-workdir script. It works fine, I use it myself. If you use msysGit, there are ports available: https://github.com/joero74/git-new-workdir/blob/master/git-new-workdir.cmd and https://github.com/dansmith65/git/blob/master/contrib/workdir/git-new-workdir-win . I don't know about their quality, though.

Limitations of git-new-workdir

Just for completeness' sake, here are the limitations I know of:

  • If you checkout the same branch in both working directories, and then commit something in one working directory, the state of the other will get confused: git-new-workdir: Commit in working tree A causes bogus changes in tree B
  • Apparently, you should not use git-new-workdir to copy a working directory already created by git-new-workdir. Just always copy the original clone. Actually, I believe recent versions of the script will detect this situation and error out.
  • git prune does not properly respect reflog entries


Maybe the submodules could help you having independent directories?


If I understand correctly, you are looking for the ability to CopyOut a specific branch head or commit on a branch, onto an independent directory, for investigation and comparison purposes, while still having your current development/bugfix branch still checked out as your main git 'reference'.

You expect that the independently 'copied out' directory would be unlikely to be re-committed directly to git.

An untested approach would be to use the base git command with its [--git-dir=<path>] [--work-tree=<path>] options, and to, through a careful script, adjust/correct your HEAD / Index as you do the CopyOut and step back to your current branch.

The usual caveats apply..

  1. Create new work_dir
  2. Start git pointing to work_dir
  3. Save current HEAD
  4. Checkout required branch/commit (it should go into work_dir)
  5. Set HEAD back to saved value (This is a hack and may not work without steps to keep the index right!)
  6. close git to 'forget' the work_dir setting
  7. restart git back in your old directory and its associated branch

This would definately need careful testing, and probably needs adjustment around steps 3 & 5, such that step 6-7 is true.

The git work-tree option could give a mechanism for bring in changes from the copyout directory if it had bug fixes that needed committing into the appropriate branch.

UPDATE --------------------------------
The clunx instructions are based on a Windows user's view.

# this is run from your main work dir (that contains .git)
Copy .git/HEAD MyBackup
# HEAD will contain the line similar to "ref: refs/heads/master"
mkdir <path>
# the path should be 'somewhere else', not in your existing directory
# If the path doesn't exist the following checkout will fail
Git --work-tree=<path> checkout <branch>
# this will extract the latest commit on that branch to the work tree path
# or use a commit sha1 instead of <branch>, if you want an exact commit
# Note: your index will be updated to reflect the checked out files, as will HEAD.
Copy MyBackup .git/HEAD
# set the HEAD back to where it was
Git reset
# reset the index appropriate for the previous Head.
# note we dropped the --work-tree parameter

A similar technique can be used to grab any revisions from the extracted path by pointing --work-tree at the right place at the right time and making sure the index is correctly set. Don't forget the usual caveats [backup, backup, and backup].

This worked for me on a test repo I have. Some of the actions were in Windows.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜