我在一个名为XXX的文件夹中有一个Git存储库,还有一个名为YYY的Git存储库。
我想将XXX存储库作为名为ZZZ的子目录导入到YYY存储库中,并将所有XXX的更改历史添加到YYY中。
之前的文件夹结构:
├── XXX
│ ├── .git
│ └── (project files)
└── YYY
├── .git
└── (project files)
文件夹结构后:
YYY
├── .git <-- This now contains the change history from XXX
├── ZZZ <-- This was originally XXX
│ └── (project files)
└── (project files)
这可以做到吗,或者我必须使用子模块?
添加另一个答案,因为我认为这有点简单。将repo_dest拉入到repo_to_import中,然后推入——set-upstream url:repo_dest master。
这种方法对我来说很有效,我把几个较小的回购导入一个较大的回购中。
如何将repo1_to_import导入到repo_dest
# checkout your repo1_to_import if you don't have it already
git clone url:repo1_to_import repo1_to_import
cd repo1_to_import
# now. pull all of repo_dest
git pull url:repo_dest
ls
git status # shows Your branch is ahead of 'origin/master' by xx commits.
# now push to repo_dest
git push --set-upstream url:repo_dest master
# repeat for other repositories you want to import
重命名或移动文件和dirs到原始回购所需的位置,然后再进行导入。如。
cd repo1_to_import
mkdir topDir
git add topDir
git mv this that and the other topDir/
git commit -m"move things into topDir in preparation for exporting into new repo"
# now do the pull and push to import
以下链接中描述的方法启发了这个答案。我喜欢它,因为它看起来更简单。但是要小心!有龙!https://help.github.com/articles/importing-an-external-git-repository git push——镜像url:repo_dest将本地的回购历史和状态推送到远程(url:repo_dest)。但是它会删除旧的历史记录和远程状态。乐趣随之而来!: - e
我认为你可以使用'git mv'和'git pull'来做到这一点。
我是一个公平的git新手-所以要小心你的主存储库-但我刚刚在一个临时目录中尝试了这一点,它似乎工作。
首先-重命名XXX的结构,以匹配你想要它在YYY中的样子:
cd XXX
mkdir tmp
git mv ZZZ tmp/ZZZ
git mv tmp ZZZ
现在XXX是这样的:
XXX
|- ZZZ
|- ZZZ
现在使用'git pull'来获取更改:
cd ../YYY
git pull ../XXX
现在YYY是这样的:
YYY
|- ZZZ
|- ZZZ
|- (other folders that already were in YYY)
下面是脚本,将立即工作。
#!/bin/bash -xe
# script name: merge-repo.sh
# To merge repositories into the current.
# To see the log of the new repo use 'git log --follow -- unprefixed-filename'
# So if the file is repo/test.cpp use 'git log --follow -- test.cpp'
# I'm not sure how this will work when two files have the same name.
#
# `git branch -a` will show newly created branches.
# You can delete them if you want.
merge_another() {
repo="$1" # url of the remote repo
rn="$2" # new name of the repo, you can keep the same name as well.
git remote add ${rn} ${repo}
git fetch ${rn}
git merge -s ours --no-commit --allow-unrelated-histories ${rn}/master
git read-tree --prefix=${rn}/ -u ${rn}/master
git commit -m "Imported ${rn} as a subtree."
git pull -s subtree ${rn} master
}
merge_another $1 $2
运行脚本。转到您希望合并另一个repo的repo,并运行脚本。
cd base-repo
./merge-repo.sh git@github.com:username/repo-to-be-merged.git repo-to-be-merged-new-name
现在将主分支上的更改推到remote/origin。根据您要做的事情,可能不需要此步骤。
git push origin master
添加另一个答案,因为我认为这有点简单。将repo_dest拉入到repo_to_import中,然后推入——set-upstream url:repo_dest master。
这种方法对我来说很有效,我把几个较小的回购导入一个较大的回购中。
如何将repo1_to_import导入到repo_dest
# checkout your repo1_to_import if you don't have it already
git clone url:repo1_to_import repo1_to_import
cd repo1_to_import
# now. pull all of repo_dest
git pull url:repo_dest
ls
git status # shows Your branch is ahead of 'origin/master' by xx commits.
# now push to repo_dest
git push --set-upstream url:repo_dest master
# repeat for other repositories you want to import
重命名或移动文件和dirs到原始回购所需的位置,然后再进行导入。如。
cd repo1_to_import
mkdir topDir
git add topDir
git mv this that and the other topDir/
git commit -m"move things into topDir in preparation for exporting into new repo"
# now do the pull and push to import
以下链接中描述的方法启发了这个答案。我喜欢它,因为它看起来更简单。但是要小心!有龙!https://help.github.com/articles/importing-an-external-git-repository git push——镜像url:repo_dest将本地的回购历史和状态推送到远程(url:repo_dest)。但是它会删除旧的历史记录和远程状态。乐趣随之而来!: - e
我当时在找-s他们的,当然,这个策略不存在。我的历史是我在GitHub上分叉了一个项目,现在由于某种原因,我的本地master不能与上游/master合并,尽管我没有对这个分支做任何本地更改。(真的不知道那里发生了什么——我猜上游在幕后做了一些肮脏的推动,可能吧?)
我最后做的是
# as per https://help.github.com/articles/syncing-a-fork/
git fetch upstream
git checkout master
git merge upstream/master
....
# Lots of conflicts, ended up just abandonging this approach
git reset --hard # Ditch failed merge
git checkout upstream/master
# Now in detached state
git branch -d master # !
git checkout -b master # create new master from upstream/master
所以现在我的master再次与upstream/master同步(你可以对任何其他分支重复上面的步骤,你也想进行类似的同步)。