首页 > 解决方案 > 如何在 Github Repo 下重命名两个同名目录

问题描述

我已经分叉了一些 GitHub 存储库,其中包含多个目录:

dir_1
dir_2
Dir_1
dir_4
README.md

为了您的信息,dir_1 和 Dir_1 具有相同的名称,但包含不同的文件。当我尝试在本地克隆我的分叉存储库时,我最终拥有以下目录和 README 文件:

dir_1
dir_2
dir_4 
README.md

发生的事情是 Dir_1 不再存在,但唯一的 dir_1 被保留。因此,为了解决这个问题,我认为我需要在本地克隆分叉项目之前重命名 dir_1 和 Dir_1。例如,dir_1 将保持相同的名称,但 Dir_1 将重命名为 dir_1_old。这是解决此问题的正确方法吗?如果是这样,我怎样才能做到这一点,否则这种问题的更好解决方案是什么?

非常感谢你提前

标签: gitgithubversion-control

解决方案


如果您正在处理不区分大小写的文件系统(Windows 或 MacOS),则目录和dir_1实际上Dir_1“合并”在磁盘上:您应该在本地dir_1目录中看到.dir_1Dir_1

这是一种从不区分大小写的文件系统中重命名其中一个文件夹的方法:

# start from a clean worktree :
$ git stash

# the following will :
#  * on your disk: move the combined content of Dir_1 and dir_1 to Renamed_1
#  * in git's index: stage the renaming of Dir_1 only
$ git mv Dir_1 Renamed_1

# this will restore the content of dir_1 on your disk :
$ git checkout .
# this will remove the content of dir_1 from Renamed_1 on your disk :
$ git clean -fd Renamed_1

# check your git status : you should see all files from Dir_1/... renamed to Renamed_1/...
$ git status

另一种方法是从区分大小写的文件系统(例如:Linux 工作站或 VM)中重命名文件夹


说明如何在这种情况下着陆,以及一些示例命令来检查 git 中存储的内容:

从以下情况开始:

$ git log --graph --oneline --all
* ba1a040 (HEAD -> right) created Dir_1 and file_right.txt on branch right
| * 3cb290d (left) created dir_1 and file_left.txt on branch left
|/
* 0d2a900 (master) first commit (README.txt)

合并两个分支leftright将导致:

$ git merge left
Merge made by the 'recursive' strategy.
 dir_1/file_left.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 dir1/file_left.txt

# in git's storage : there are two distinct directories dir_1 and Dir_1
$ git ls-tree HEAD
040000 tree d6317389dbdebb9fcec7a5f60ba49666bb55cdfb    Dir_1
100644 blob 72943a16fb2c8f38f9dde202b7a70ccc19c52f34    README.txt
040000 tree 9cd36c945344bf39eb3b3fcb642f791f80847dbf    dir_1
$ git ls-tree HEAD:Dir_1
100644 blob 5716ca5987cbf97d6bb54920bea6adde242d87e6    file_right.txt
$ git ls-tree HEAD:dir_1
100644 blob 257cc5642cb1a054f08cc83f2d943e56fd3ebe99    file_left.txt

# when checked out on disk, both directories get "merged"
# (which name wins depends on the order in which the directories were created on disk)
$ ls
README.txt  Dir_1/
$ ls Dir_1/
file_left.txt  file_right.txt

推荐阅读