简体   繁体   中英

Git -- cloning a previous commit in a different folder than local working directory

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 -- ie, they are the commit abcdef.

(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 .

(3) From inside of c:\temp_outside_of_source_control\ , I ran

git clone ghijkl

This does not work:

fatal: repository 'ghijkl' does not exist

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 (eg 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM