git:将文件夹及其所有子文件夹/文件恢复到不同名称下

3
master分支上,我有一个包含文件的新版本文件夹,例如:
config
  - db
    - rdbms
      - postgres.conf
      - redis.conf
    - nosql
      - mongo.conf
  - web
    - apache.conf
  - security.conf

我需要获取这些文件的旧版本,并将它们添加到主代码库中,但顶层文件夹 config 应该重命名为 config-old,以便同时存在 configconfig-old

config
  - db
    - rdbms
      - postgres.conf
      - redis.conf
    - nosql
      - mongo.conf
  - web
    - apache.conf
  - security.conf

config-old
  - db
    - rdbms
      - postgres.conf
      - redis.conf
    - nosql
      - mongo.conf
  - web
    - apache.conf
    - nginx.conf

我知道如何使用git showgit cat-file恢复单个文件,并使用shell重定向将它们保存在另一个名称下,但如何对整个文件夹及其所有子文件夹/子文件进行操作呢?

显然,我可以切换到所需的分支/版本,将文件夹复制到git根目录之外的某个地方,然后切换回主分支并将其以不同的名称复制回来,但我想知道是否可以使用git本身完成此操作?

2个回答

2
如果您事先创建一个空文件夹config-old,则可以使用git archive从所选修订版本中获取config/,并将其提取到文件夹config-old/中,如下所示:
git archive --format=tar <COMMIT/BRANCH/TAG> -- config | tar --extract --directory=./config-old --strip-components=1

2

您可以使用基本命令轻松完成此操作:

git checkout $OLD_VERSION
cp -R config config-old
git checkout master

此时config-old未被跟踪,因此:

git add config-old
git commit -m 'Copy config from $OLD_VERSION to config-old'

太好了!确实,我为什么会错过这个?谢谢! - xaxa
1
这是一个更简单/更明显的方法。不错 :) 但是:如果您在config/中有.gitignore文件,请小心使用此方法。这些文件也将被复制,并可能因为其.gitignore模式仅限于其原始文件夹而意外跟踪。此外,如果git status不干净,您可能会遇到问题(检出取消)。 - Jay

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接