Reputation: 60107
I want to check out a past commit into a separate directory and I don't want the current repo to be affected by that at all.
Making a clone and checking out there does the job, but it seems like an overkill. Setting GIT_WORK_TREE or --work-tree fails to leave the current repo alone (although it does check out elsewhere, it still updates HEAD, and I don't want that).
Is there a better way?
Upvotes: 0
Views: 27
Reputation: 792557
You can use git worktree
for this.
git worktree add ../other-worktree <old-commit-id>
This is lets you have an independent working tree attached to the original repository. Once you're done you can simply remove the new directory.
See git help worktree for full details.
Before git worktree
you could have done something like this, but I don't see any reason not to use git worktree
now. It's simpler and less error prone:
( GIT_INDEX_FILE=$(mktemp) &&
export GIT_INDEX_FILE &&
git read-tree "$old-commit-id$ &&
git checkout-index -a --prefix=../old-commit-copy/ &&
rm "$GIT_INDEX_FILE"
)
Upvotes: 1