On my local machine, I have a folder structure thus:
c:\myprojects\project1\
c:\myprojects\project2\
This has been git committed/pushed remotely to repository titled myprojects
I have a folder on my machine which is not under source control. This is
c:\temp_outside_of_source_control\
Suppose the repositories (online and local) have the following history of commits in reverse chronological order (SHAIDs):
abcdef #<---- current upto date one
ghijkl #<---- older commit
How can I download/clone/repo commit ghijkl
to c:\temp_outside_of_source_control\
without affecting/touching in any way the relationship that exists between the local c:\myprojects\
and remote myprojects
?
What I have tried:
(1) From inside of c:\temp_outside_of_source_control\
, I ran
git clone url_path_to_myprojects_repository
This created
c:\temp_outside_of_source_control\myprojects\project1\
c:\temp_outside_of_source_control\myprojects\project2\
and the contents of this folder are upto date -- i.e., they are the commit abcdef. This, however, has the unfortunate side effect that this affects the content of c:\myprojects\.git\
folder. I want the current relationship between my online repository and local repository to be completely untouched. Essentially, I want an earlier commit to be replicated in a non-source-controlled folder without this in any way impacting the current relationship between my source controlled folder locally and the online repository.
(2) From a git software (Git Graph in VSCode, if it matters), that shows a history of commits, I right clicked on commit ghijkl
and pressed checkout...
in the hope that it would ask me which folder locally I would like to check out to, but there was no such option. There was a warning:
Are you sure you want to checkout commit ghijkl? This will result in a detached HEAD state.
This possibly will affect the relationship that exists between c:\myprojects\
and the online repository myprojects
.
ETA: As suggested by user knittl in the answer, I was able to zip the contents of a previous commit without affecting much else and download that locally via:
git archive --format zip --output previous_commit.zip ghijkl
However, I would still like to know if there is a way to directly replicate a previous commit to a user specified folder that is not under source control without "disturbing" the current relationship between the online repository and the source controlled folder.
ETA2: Indeed the method suggested by knittl is perfect for my purposes on ubuntu/wsl:
git archive ghijkl | tar -xC temp_outside_source_control
CodePudding user response:
You are confusing clones/repositories and commits. Your command
git clone url_path_to_myprojects_repository
Is only missing one step:
git checkout ghijkl
This will get you the older commit. git clone
accepts URLs to a repository and downloads the complete history of the repository and checks out the latest commit of the default branch by default. git checkout
usually expects a commit and will make your working tree on disk match the given commit from your local repository. The newer switch
subcommand does the same.
That said, if you are not interested in the history at all, you can easily create an archive from any tree-ish (e.g. a commit):
cd url_path_to_myprojects_repository
git archive -o source.tar ghijkl
Or possibly even, depending on your remote repository configuration:
git archive -o source.tar --remote=url_of_origin_repository ghijkl
Later, you can extract the archive in any directory you wish. It is also possible to immediately extract the archive, without writing the file to disk:
git archive ghijkl | tar -xC temp_outside_source_control