繁体   English   中英

将 git 项目移动到文件夹中

[英]Move git project into folder

我在一个文件夹中有一个 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 中 init new 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 不起作用。

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


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

一、清空索引

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

然后移动.git文件夹

cd ..
mv folder2/.git .

重新填充索引并提交

git add folder2
git commit

然后,您可能希望使用忽略规则来防止意外添加folder1

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

不过,除非是在水平等内容parent (或其他子目录parent )应包含在回购,这似乎是一个很大的头痛正好有回购的顶层文件夹中包含一个名为单个条目folder2 ; 我真的想不出有什么理由不根据问题中提供的信息从/在folder2创建 repo。

最后,我在 parent_folder 中初始化一个新 git,拉取,删除所有内容,将 folder_2 放在 parent_folder 中,“添加 .”,提交并推送。 所以本地和远程有 parent_folder > folder_2 而没有别的。 之后,恢复 parent_folder 内容,现在,在推送之前只需“添加文件夹_2”。

也许不是最优雅的解决方案,但有效。

  1. 转到parent_folder/folder_2
  2. parent_folder/folder_2创建folder_2目录
  3. 将所有内容从parent_folder/folder_2移动到parent_folder/folder_2/folder_2
  4. parent_folder/folder_1复制到parent_folder/folder_2
  5. git add --all . && git commit -m "Import folder_1" git add --all . && git commit -m "Import folder_1"
  6. git push
  7. 将所有内容(不要忘记.git/ )从parent_folder/folder_2parent_folder 使用git push验证结果

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM