简体   繁体   English

将一些git commit移到新的仓库中

[英]Move some git commits into a new repo

I am using this rest-more repository, which contains many implementations of the web-api (such as Facebook or Linkedin). 我正在使用这个休息的存储库,其中包含Web-api的许多实现(例如Facebook或Linkedin)。 I forked that repository, and in my branch I added my own implementation of another web api. 我分叉了该存储库,并在我的分支中添加了自己的另一个Web API实现。

However now I find that it is pretty hard to keep updated with the original repository. 但是现在我发现很难与原始存储库保持更新。 I rebase my branch every time the original repository changes, which is pretty tedious. 每当原始存储库更改时,我都会对分支进行重新设置,这非常繁琐。 So I want to split out my own implementation to a standalone repository. 因此,我想将自己的实现拆分为一个独立的存储库。

All my commits are in my branch grouped together. 我所有的提交都在我的分支中分组在一起。 Only one commit is touching a file from the original repository. 仅一次提交正在触摸原始存储库中的文件。

Is there an easy way to move my commits into a new repository? 有没有简单的方法可以将提交提交到新的存储库中? I want to do this so I can preserve my commit histories. 我想这样做,以便保留我的提交历史。

Easiest: just stop accepting updates from the original repository. 最简单:只需停止接受来自原始存储库的更新。 Remove the remote repository from your .git/config file. 从.git / config文件中删除远程存储库。

Otherwise, from my notes: 否则,根据我的注释:

Say you want to move dir1 from repository A to repository B : 假设您要将dir1从存储库A移至存储库B

Make a clone of repository A . 克隆存储库A。 To be safe, we break the link between the clone and A , to make sure none of our changes accidentally get sent back. 为了安全起见,我们断开了clone和A之间的链接,以确保我们的所有更改均不会意外发送回。

git clone git://git.foo.com/ProjectA.git NewProject
cd NewProject
git remote rm origin

Strip out everything except dir1 . 除去除dir1之外的所有内容。

git filter-branch --subdirectory-filter dir1

This will have made dir1 the new root, which you might want to undo: 这将使dir1成为新的根目录,您可能要撤消它:

mkdir dir1
mv * dir1
git commit -a

Now merge this repository into repository B 现在将此仓库合并到仓库B中

git clone git://git.foo.com/ProjectB.git ProjectB
cd ProjectB
git remote add repo-A-branch ~/NewProject
git pull repo-A-branch master
git remote rm repo-A-branch

I would recommend keeping a single repository , and creating a second branch for your changes . 建议保留一个存储库 ,并为您的更改创建第二个分支 You can then cherry pick your commits to this new branch, to isolate your changes, and merge/rebase upstream changes and your changes into a third branch as appropriate to create releases of your code. 然后,您可以选择对新分支的提交 ,以隔离您的更改,并将上游更改和您的更改合并/变基为第三个分支,以创建代码版本。 Keeping it in one repository makes it far easier to stay in sync with the upstream development, and having your changes on their own branch allows you to keep them isolated. 将其保存在一个存储库中将使其更容易与上游开发保持同步,并且将更改保存在自己的分支上可以使您保持隔离状态。 The third branch can serve as a "your changes applied to the upstream code" branch. 第三分支可以用作“您对上游代码所做的更改”分支。

Assuming that you have strictly linear history of your commits (no merges), you can simply export all your commits as follows: 假设您的提交具有严格的线性历史记录(无合并),则可以简单地按如下所示导出所有提交:

git format-patch <start>..<stop>

Where start and stop is range of commits you need ( start is commit you started from, and stop is probably current HEAD ). startstop是您需要的提交范围( start是您从其start的提交, stop可能是当前的HEAD )。

This will create lots of files named NNNNN-commit-description.patch , NNNNN is number starting from 00001. 这将创建许多名为NNNNN-commit-description.patch的文件,NNNNN是从00001开始的数字。

Then, create new empty repository and import these patches: 然后,创建新的空存储库并导入以下补丁:

git init newrepo
cd newrepo
find <oldrepo>/*.patch | xargs git am

Also, instead of starting off empty repo, you may want to make first commit with some original files that you depend upon, such that all your patches can apply cleanly. 另外,您可能希望不使用空存储库而进行第一次提交,而是使用您依赖的某些原始文件进行首次提交,以便所有补丁程序都可以干净地应用。

I first open a new repository: 我首先打开一个新的存储库:

git init new-repo

I then pushed my branch to that repository: 然后,我将分支推送到该存储库:

cd rest-more
git push ../new-repo/ my_branch

cd ../new-repo
git co my_branch

I created a new empty initial commit: 我创建了一个新的空初始提交:

git checkout --orphan newroot
git commit --allow-empty -m 'root commit'

Then I rebase my branch to this starting point, use interactive flag to pick only my commits: 然后,我将分支重新设置到此起点,使用交互式标志仅选择我的提交:

git rebase --onto newroot --root my_branch -i

Need to resolve conflicts on the way, and finally I end up with a repository with only my commits. 需要解决途中的冲突,最后我最终得到一个仅包含提交的存储库。

(There are probably improvements to be made. Feel free to suggest anything.) (可能有待改进。请随时提出任何建议。)

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

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