首页 > 解决方案 > 将 git 项目移动到文件夹中

问题描述

我在一个文件夹中有一个 git 项目,我想在我的 git projet 中添加这个文件夹,但不在同一个父文件夹中添加其他文件夹。所以我不能只在父文件夹中初始化新的 git 并推送,因为 folder_1 也会被添加。

folder_2 包含一个应用程序插件,它必须被命名为“folder_2”。所以当有人从 git 下载时,我希望他有一个“folder_2”文件夹,里面有插件文件。

现在 folder_2内容在 git 中:

parent_folder
|_ folder_1
|_ folder_2
   |_ .git
   |_ machin.html => in git
   |_ truc.html => in git
   |_ bidule.html => in git

我想要的是 git 中的 folder_2 本身,但没有 folder_1 :

parent_folder
|_ .git
|_ folder_1 => NOT in git
|_ folder_2 => in git
   |_ machin.html => in git
   |_ truc.html => in git
   |_ bidule.html => in git

我尝试在 parent_folder 中初始化新 git,并仅在新分支中添加 folder_2。但我现在不能合并:

$ git push -u origin master
To https://gitlab.......git



 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://gitlab........git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

注意: --force 不起作用。

标签: git

解决方案


更新- 从原始问题中不清楚,OP 想要移动现有仓库中的路径,而不是像之前暗示的那样创建新的仓库


好吧,实际上你可以git init在父文件夹中——因为只有你暂存(通过git add)和提交的文件才会被推送——如果你正在创建一个新的 repo。由于您正在修改现有的存储库,虽然您仍然可以使其工作,但不同的方法更简单:

一、清除索引

cd .../parent/folder2
git rm --cached -r .

然后移动.git文件夹

cd ..
mv folder2/.git .

重新填充索引并提交

git add folder2
git commit

然后,您可能想要使用忽略规则来防止folder1被意外添加。

(您可以将忽略规则放在.gitignore工作树的根部,如果排除在回购的更广泛上下文中“有意义”;或者如果folder1' 的存在实际上只是该特定机器上的一个东西,您可以放入中的排除git/info/exclude

但是,除非repo 中parent(或其他子目录)级别的其他内容应该包含在 repo 中,否则 repo 的顶级文件夹包含一个名为;parent的条目似乎很让人头疼。folder2我真的想不出任何理由不folder2根据问题中提供的信息从/in 创建 repo。


推荐阅读