Say I have a git repository clone at:
/myrepos/foo
so there is a .git
directory here:
/myrepos/foo/.git
and say I want to create a new directory at:
/tmp/bar
with a copy of the tree as at revision 123
Such that:
- the
/myrepos/foo
directory (nor its.git
directory) is not modified - no
/tmp/bar/.git
folder
Is there a command to do that?
Something like:
/myrepos/foo$ git checkout --into-new-directory /tmp/bar 123
perhaps?
CodePudding user response:
There are multiple ways to do that.
The most straightforward, available since Git 2.5 (but with some important fixes in later versions if you intend to do any work with these), is to use git worktree add
. This will add a new working tree; you choose where it goes.
The resulting working tree does, however, have a .git
file. You may simply remove this file (rm /tmp/foo/.git
); to inform your local Git that this working tree is now gone, run git worktree prune
right afterwards. The actual checkout will remain in place. For instance:
$ git worktree add --detach /tmp/git HEAD~3
[output trimmed]
$ rm /tmp/git/.git
$ git worktree prune
If your Git predates 2.5, or for some reason you don't want to use git worktree
, you can:
- create the directory (
mkdir /tmp/foo
) - run
(export GIT_INDEX=$(mktemp -u) && git checkout --work-tree=/tmp/foo <hash> && rm $GIT_INDEX)
in any POSIX-compatible shell. (Note: untested, unlike the git worktree add
example.)
CodePudding user response:
Git can manage multiple working trees. Run this:
git worktree add /tmp/foo 123
The new folder will have a small .git
folder but you should be able to remove it. Run git worktree help
for a little more info and git help worktree
for a lot more info.
CodePudding user response:
Git can create an archive file of a specific commit and then you can extract that archive file wherever you would like.
git archive --prefix foo/ -o /tmp/foo.tar 123
cd /tmp
tar -xvf foo.tar
This shouldn't affect the main repository at all.