简体   繁体   English

Git -- 将先前的提交克隆到与本地工作目录不同的文件夹中

[英]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.

相关问题 将 git 存储库克隆到本地 PC 后复制到其他文件夹 - Copying git repo after cloning it to local PC to a different folder 如何将特定目录提交到与git上工作分支不同的目录? - How can I commit specific directory to a different than a working branch on git? git commit在目录上不起作用 - git commit not working on directory 您可以将本地git repo保留在与工作目录不同的驱动器上吗? - Can you keep the local git repo on a different drive than the working directory? Git - 从之前的提交中恢复已删除的文件夹 - Git - restore a deleted folder from previous commit Git:如何用上一个提交而不是分支或git revert替换当前的工作目录? - Git: How do you replace your current working directory with a previous commit without branching or git revert? 更改本地git消息以进行先前的提交 - Change local git message for previous commit 将git提交到远程存储库,而无需克隆任何本地存储库 - Commit git to remote repository without cloning any local repository 如何用以前的git commit替换本地存储库 - How to replace local repository with a previous git commit GIT:如何使用批处理命令将工作目录中已经存在的.git文件夹提交到该存储库的更改? - GIT: How to commit a change with .git folder already inside a working directory to that repository via batch command?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM