我有一个位于项目内部的本地 git 存储库。我决定一些代码需要位于另一个项目中,但我只想要一个 git 存储库。
当前目录结构:
Solution->
- Project1
- .git
- Project2
Run Code Online (Sandbox Code Playgroud)
所需的目录结构:
Solution->
- .git (tracks both projects)
- Project1
- Project2
Run Code Online (Sandbox Code Playgroud)
如何移动.git到根文件夹(Solution ),以便它可以开始监视 Project1 和 Project2,而不会丢失修改/提交的跟踪?
笔记:我并不真正关心保留将要移动到 Project2 文件夹的文件的历史记录。
注意:这不是(这个问题)的重复,因为我没有移动 git 存储库及其全部内容。我只想将 git 存储库移至根文件夹。
git add -u解决方案最优雅的,需要 6 个命令。
cd Solution/
#dot means "move it here"
mv Project1/.git .
#make git add deleted files
git add -u
#make it recognize them as moved
git add Project1/
Run Code Online (Sandbox Code Playgroud)
现在git status应该显示“重命名...”
git commit -m'moved to a subfolder'
Run Code Online (Sandbox Code Playgroud)
现在检查不需要的文件并更新 .gitignore
#add the project2:
git add --all
git commit -m'added project2'
Run Code Online (Sandbox Code Playgroud)
Solution/Project1/创建一个同名的子文件夹:Solution/Project1/Project1/..git/、.gitignore和.gitconfignew 子文件夹)移动Solution/Project1/到Solution/Project1/Project1/cd Polution/project1, git add --all,git commit -m'moved to a subfolderSolution/Project1将所有内容移至Solution。现在项目文件已返回Solution/Project1/,并且.gitgit 文件已在Solution/cd Solution/, git status. 留意不需要的文件。将它们添加到.gitignoregit add --all,git commit -m'added project2'git mv解决方案cd Solution/Project1/
mkdir Project1/
#Now move all your files with git mv
git mv foo.txt Project1/
git mv bar.js Project1/
git mv fizz/ Project1/
....
git commit -m'moved files to a subfolder'
cd ../
Run Code Online (Sandbox Code Playgroud)
现在将所有文件从 移动Solution/Project1/到Solution。这可能是通过mv命令完成的,但我不明白确切的语法。
# this should work now
git status
# fix .gitignore if necessary
git add --all
git commit -m'added project2'
Run Code Online (Sandbox Code Playgroud)