Single working branch with Git

前端 未结 7 1150
予麋鹿
予麋鹿 2021-01-01 15:21

Bounty short description

Is there a portable way to use a single repository w/ multiple checkouts? As an alternative to ha

相关标签:
7条回答
  • 2021-01-01 15:44

    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
    0 讨论(0)
  • 2021-01-01 15:52

    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.

    0 讨论(0)
  • 2021-01-01 15:53

    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.

    0 讨论(0)
  • 2021-01-01 16:01

    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)

    0 讨论(0)
  • 2021-01-01 16:07

    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.

    0 讨论(0)
  • 2021-01-01 16:07

    Maybe the submodules could help you having independent directories?

    0 讨论(0)
提交回复
热议问题