在新的git存储库上重放提交最简单的方法 [英] Easiest way to replay commits on new git repository

查看:89
本文介绍了在新的git存储库上重放提交最简单的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用git-svn,最近,我在尝试提交时遇到错误(我认为这是由于libneon中的一个错误,但这超出了这个问题的范围)。解决方案是使用 git svn clone 重新克隆我的git存储库。但是,我在旧的git仓库中的master分支上发生了变化,我无法使用 git svn dcommit 提交给svn。我想重新播放使用git svn克隆的新存储库中的这些更改。我想我可能会使用 git format-patch 导出一个补丁集,然后在新的资源库中重播这些更改,但我不完全确定如何执行此操作,我不知道是否有更简单或更优雅的方法来完成这项工作。 解决方案

您的旧版本库:

  git remote添加临时文件:/// path / to / old / repo / on / your /机器

从旧回购中提取:

  git fetch temp 

检查您的旧分支repo:

pre $ git checkout temp / master -b wip

(wip代表正在进行的工作)



在当前存储库中重新编译这些更改:

  git rebase master 

更新主人以指向您的新主人:

  git checkout master 
git merge wip

删除对旧repo和你使用的wip分支的远程引用:

  git分支-d wip 
git remote rm temp

您实际在做什么:

首先,通过添加远程引用和提取,您将从您之前的存储库提取您当前存储库中尚未提供的提交。 Git知道如何做到这一点,因为无论在哪里或如何制作,相同的提交看起来都一样。它是几个众所周知的信息的SHA1哈希,包括目录树,提交者,时间戳,...

所以当你创建一个新的Git存储库基于相同的SVN存储库,所有提交都具有相同的SHA1总和。因此,您提交到当前存储库中的新提交继续指向正确的东西。这非常酷,而且很重要。



然后,您切换到临时主分支的顶端,并将其分配给您当前的主分支。因为SVN的主人可能没有从你的旧回购中的主人离开,但最好是安全的。rebase可能是不必要的,因为SVN的主人可能没有离开你的旧回购中的主人。两个提交之间最近的共同点,通过反向工作直到它们指向相同的父提交。然后它切换到你给它的分支名称(在这种情况下 master ),然后樱桃挑选从你的原始分支历史中丢失的每个提交。一旦完成,它指出你在开始应用它的最后一次提交时所处的分支。

最后,master和wip的合并只是为了快速转发master到最后。由于这是一条直线,它实际上只是一个快速前进。你可以轻松地做一个rebase或重置 - hard;这些中的任何一个都会改变主分支以指向正确的位置。合并只是最安全的,因为如果发生了奇怪的事情,它会让你知道这不是一个简单的快进。


I've been using git-svn, and recently, I've been getting errors when trying to commit (I think this is due to a bug in libneon, but this is beyond the scope of this question). The solution has been to re-clone my git repository using git svn clone. However, I have changes on the master branch in my old git repository that I was not able to commit to svn using git svn dcommit. I'd like to replay these changes on the new repository cloned with git svn. I think I could probably export a patch-set using git format-patch, and then replay these changes on the new repository but I'm not entirely sure how to do this, and I wonder if there's an even easier or more elegant way to accomplish this.

解决方案

From your new repository, add a remote reference to your old repository:

git remote add temp file:///path/to/old/repo/on/your/machine

Fetch from the old repo:

git fetch temp

Check out your master branch from the old repo:

git checkout temp/master -b wip

(wip stands for work-in-progress)

Rebase the changes on top of the stuff in your present repository:

git rebase master

Update master to point at your new HEAD:

git checkout master
git merge wip

Delete the remote reference to your old repo and the wip branch that you used:

git branch -d wip
git remote rm temp

What you are actually doing:

Firstly, by adding the remote reference and fetching, you are pulling the commits from your previous repository which you do not yet have in your current repository. Git knows how to do this because, no matter where or how it was made, the same commit looks the same everywhere. It is the SHA1 hash of a few well-known pieces of information, including the directory tree, the committer, timestamp, ...

So when you created a new Git repository based on the same SVN repository, all the commits had the same SHA1 sums. As a result, the new commits that you fetched into your current repository continued to point at the right stuff. This is very cool, and important to remember.

You then switched to the tip of temp's master branch and told it to rebase onto your current master. The rebase may have been unnecessary, as master from SVN may not have moved away from the master in your old repo, but it was best to be safe.

A rebase finds the nearest point of commonality between two commits by working backwards through their histories until they both point at the same parent commit. It then switches to the branch name you gave it (in this case master) and cherry-picks each of the commits that were missing from its history from your original branch. Once complete, it points the branch you were on when you started at the last commit it applied.

Finally, the merge of master with wip was just to fast-forward master to the end. As it was a straight line, it really was just a fast-forward. You could have just as easily done a rebase or a reset --hard; any of these would have altered the master branch to point at the correct location. Merge was just the safest of these, because if something weird had happened, it would have let you know that it was not a simple fast-forward.

这篇关于在新的git存储库上重放提交最简单的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆