我正在开发一个由git支持并且愉快地投入的项目。最后,我意识到我忘了为我的项目创建一个git存储库。相反,我的提交将转到包含我的项目的文件夹中的父存储库。
是否可以保留这些提交,但是将它们移动到一个完全独立的存储库?
答案 0 :(得分:1)
克隆你的git存储库。这实际上没有必要,但在专用克隆中执行此操作可能是个好主意。
将提交的父存储库(或父存储库的克隆)作为新创建的克隆的远程存储库附加。
在新创建的克隆中:查看提交应该首先出现的分支。
此时,您应该能够从远程存储库中单独挑选提交到克隆中的右侧分支。有关详细信息,请参阅git cherry-pick
文档。您可以在git log
上使用remotename/branch
来获取在父级回购邮件中提交的提交的哈希值。
当你完成后,推动分支。
清理父存储库取决于所做的错误提交是否尚未推送。如果他们没有,您只需git reset
您的父存储库克隆,并在那里摆脱它们。如果它们已被推送,那么推动提交将它们退出可能会更好。
答案 1 :(得分:1)
不完全保持,但要复制到新的回购。假设您有2个目录,父目录和子目录,子目录为父目录。
您可以在子级中git init
创建新的存储库。这还没有产生太大影响,但如果您想将文件带到新的仓库,则需要这样做。要轻松将提交复制到新的repo,您需要将父级添加为远程,并cherry-pick
提交您想要的提交。
总结:
cherry-pick
使用子树策略从父级仓库提交到子仓库。请注意,以下顺序非常重要!
user@COMPUTER /c/projects/parent/child (master)
$ git init
Initialized empty Git repository in c:/projects/parent/child/.git/
user@COMPUTER /c/projects/parent/child (master)
$ git remote add parent /c/projects/parent
user@COMPUTER /c/projects/parent/child (master)
$ git fetch parent
remote: Counting objects: 7, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 7 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (7/7), done.
From c:/projects/parent
* [new branch] master -> parent/master
user@COMPUTER /c/projects/parent/child (master)
$ cd ../
# You could revert the commits too at this point, or delete the files. Your choice
user@COMPUTER /c/projects/parent (master)
$ git rm child/file.txt
rm 'child/file.txt'
user@COMPUTER /c/projects/parent (master)
$ git commit -m "Removed commits for other repo"
[master d228096] Removed commits for other repo
1 file changed, 1 deletion(-)
delete mode 100644 child/file.txt
user@COMPUTER /c/projects/parent (master)
$ git log --oneline
d228096 Removed commits for other repo
5f29100 Some commit I want to really put in child
3263034 Initial commit
user@COMPUTER /c/projects/parent (master)
$ cd child/
user@COMPUTER /c/projects/parent/child (master)
$ git status
On branch master
Initial commit
nothing to commit (create/copy files and use "git add" to track)
user@COMPUTER /c/projects/parent/child (master)
$ git cherry-pick 5f29100 --strategy=subtree -Xsubtree=child
[master (root-commit) a1b14e4] Some commit I want to really put in child
1 file changed, 1 insertion(+)
create mode 100644 file.txt
user@COMPUTER /c/projects/parent/child (master)
$ ls
file.txt
注意:如果您在从父级回购中删除之前尝试挑选,您将得到以下内容:
user@COMPUTER /c/projects/parent/child (master)
$ git cherry-pick parent/master --strategy=subtree -Xsubtree=child
error: The following untracked working tree files would be overwritten by merge:
file.txt
Please move or remove them before you can merge.
Aborting
error: could not apply 5f29100... Some commit I want to really put in child
答案 2 :(得分:1)
假设您的项目看起来像这样
project/
file1
file2
file3
your_subdir/
subfile1
subfile2
我假设当前的git存储库是跟踪project
的那个,而your_subdir
目录是你添加这两个新文件的地方(可能是两个单独的提交)您想要移动到一个单独的存储库。
您可以做的是使用
将your_subdir
目录拆分为单独的子树
git subtree split -P your_subdir
这将打印一个新树的头部的单个哈希,该树只具有以your_subtree
为根的提交。我们假设它打印abcd
。
现在使用
在该提交中剪切一个分支git branch foo `abcd`
现在你可以将这个分支(可能是master
)推送到一个新的远程,它只是为了容纳这个子项目而被创建。
git remote add subproj git@github.com:sam/subproj.git
git push subproj foo:master
现在,您可以按原样继续使用它。或者您可以将master
重置为这两次提交之前的位置,并将subproject
添加为子模块。