git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Phillip Wood <phillip.wood123@gmail.com>
To: Ondra Medek <xmedeko@gmail.com>, git@vger.kernel.org
Subject: Re: Checkout to different directory at certain commit without changing index
Date: Mon, 13 May 2024 16:28:44 +0100	[thread overview]
Message-ID: <13fa579b-8987-4430-967c-42b9de8acb6d@gmail.com> (raw)
In-Reply-To: <CAJsoDaEJn1Y0CgtxpkGqVRsTiDnMxjNFrtbTuUVOvT87N23JNg@mail.gmail.com>

Hi Ondra

On 13/05/2024 08:26, Ondra Medek wrote:
> Hello,
> I need a simple script for unskilled users to do a fast checkout (LFS
> friendly) of the current local Git clone at a certain commit to a
> different directory I.e. something like "copy at a point in history".

I think using

     git archive "$commit" --format=tar |
        { cd "$output_directory" && tar -xf -; }

is probably the simplest solution. If you don't want to rely on tar then 
something like

     GIT_DIR="$(git rev-parse --path-format=absolute --git-dir)" &&
     GIT_COMMON_DIR="$(git rev-parse --path-format=absolute 
--git-common-dir)" || exit
     GIT_INDEX_FILE="$GIT_DIR/tmp-index-$$"
     export GIT_DIR GIT_COMMON_DIR GIT_INDEX_FILE
     unset GIT_WORK_TREE
     mkdir "$output_directory" && cd "$output_directory" &&
     git read-tree -u "$commit"
     status=$?
     rm "$GIT_INDEX_FILE"
     exit $status

Which uses a temporary index file should work (note I haven't tested 
it). You may want to add "--recurse-submodules" and/or 
"--no-sparse-checkout" to the "git read-tree" commandline.

Best Wishes

Phillip

> IMO all possible solutions are summarized in this thread
> https://stackoverflow.com/questions/160608/do-a-git-export-like-svn-export
> I describe some of them with my remarks:
> 
> - git checkout-index : works with HEAD only.
> - git archive: influenced by export-ignore and export-subst
> attributes, so may not produce exact copy of sources. (And needs tar).
> - git worktree add -d : needs cleanup: git prune or git remove.
> - git clone: Unfortunately, -b param cannot work with commit hash and
> does not respect local worktree settings (e.g. autocrlf). So, a
> solution may be a bit complicated: git clone -s -n . dest/path ; cp
> .git/config dest/path/.git ; cd dest/path ; git co -q <commit-ish> ;
> rm -rf .git
> - git checkout: Unfortunately, modifies Git index, so some action to
> revert index is necessary after: git --work-tree=/path/to/checkout/
> checkout -f -q <tree-ish> -- ./
> 
> For me, the best solution is with git clone, because it does not
> modify Git index nor any source working tree settings, so no cleanup
> is necessary. But it's a bit complicated, though. It seems to me that
> "git checkout" could do this better and simpler if it would have some
> param to not modify the Git index. Is it possible to enhance git
> checkout? Or is there any other simple solution not mentioned in the
> SO thread?
> 
> Thank you
> Ondra Medek
> 

  reply	other threads:[~2024-05-13 15:28 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-13  7:26 Checkout to different directory at certain commit without changing index Ondra Medek
2024-05-13 15:28 ` Phillip Wood [this message]
2024-05-13 17:23   ` Ondra Medek
2024-05-14  6:29     ` Ondra Medek
2024-05-14 10:07       ` phillip.wood123
2024-05-15  5:07         ` Ondra Medek
2024-05-14 10:01     ` phillip.wood123

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=13fa579b-8987-4430-967c-42b9de8acb6d@gmail.com \
    --to=phillip.wood123@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=phillip.wood@dunelm.org.uk \
    --cc=xmedeko@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).