[英]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
这是 git 远程提交/推送到名为
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):假设存储库(在线和本地)具有以下按时间倒序提交的历史记录 (SHAID):
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
?如何下载/克隆/repo 将
ghijkl
提交到c:\temp_outside_of_source_control\
而不会以任何方式影响/触及本地c:\myprojects\
和远程myprojects
之间存在的关系?
What I have tried:我试过的:
(1) From inside of c:\temp_outside_of_source_control\
, I ran (1) 从
c:\temp_outside_of_source_control\
里面,我跑了
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.并且这个文件夹的内容是最新的——即它们是提交 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. (2)从 git 软件(VSCode 中的 Git Graph,如果重要的话)显示提交历史,我右键单击提交
ghijkl
并按下checkout...
希望它会问我我会在本地哪个文件夹想退房,但没有这样的选择。 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
.这可能会影响
c:\myprojects\
和在线存储库myprojects
之间存在的关系。
(3) From inside of c:\temp_outside_of_source_control\
, I ran (3) 从
c:\temp_outside_of_source_control\
里面,我跑了
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 clone
接受存储库的 URL 并下载存储库的完整历史记录并默认检查默认分支的最新提交。 git checkout
usually expects a commit and will make your working tree on disk match the given commit from your local repository. git checkout
通常需要提交,并使磁盘上的工作树与本地存储库中的给定提交相匹配。 The newer switch
subcommand does the same.较新的
switch
子命令也是如此。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.