重写整个git历史记录 [英] Rewrite entire git history

查看:79
本文介绍了重写整个git历史记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很久以前,我在github上创建了一个仓库,并对几个不相关的功能进行了相当多的更改,当时我对git基本上一无所知,所以我的提交基本上只是自从我碰巧以来所做的一切每当我最后一次记得要提交的内容是.我想在上游贡献一些更改,但是在我的仓库处于当前状态的情况下这是不可能的.既然我实际上已经使用git足够长的时间来适应它了,并且更加了解良好实践,是否有可能重写我的整个提交历史记录以分解少量的大型提交,每个大型提交都包含许多不相关的更改,分成较小的单个提交,仅包含相关更改?如果没有其他问题,我可以删除Github上的fork,重新进行分叉,用当前的本地副本覆盖所有内容,并运行一堆部分提交,但是如果有更好的方法那就更好了.

Quite awhile back, I forked a repo on github and made quite a few changes to several unrelated features, and at the time I knew basically nothing about git, so my commits are basically just "whatever I happened to have worked on since whenever the last time I remembered to commit was". I would like to contribute some of my changes upstream, but that isn't possible with my repo in its current state. Now that I've actually been using git long enough to be comfortable with it, and more aware of good practices, is it possible to rewrite my entire commit history in order to break up the small number of large commits, each containing many unrelated changes, into smaller individual commits, only containing related changes? If nothing else, I can just delete the fork on Github, re-fork, overwrite everything with my current local copies and run a bunch of partial commits, but if there's a better way that would be nice.

推荐答案

是的,可以重写整个历史记录...但是对于您的情况,则不必这样做.您只需要重写您处理过的位即可.这可能只是对 master 分支的更改.为此,您可以为 master origin/master 之间的所有更改(它们是上游所做的)进行交互式变基.

Yes, it's possible to rewrite the entire history... but for your case you don't have to. You just need to rewrite the bits you worked on. This is, probably, just the changes to your master branch. For that you can do an interactive rebase for all the changes between master and origin/master which is what they have upstream.

要执行此操作,请先结帐master,然后再结帐 git rebase -i origin/master .这将使您进入编辑器,您可以在其中选择每次提交的处理方式.您可能要编辑它们( e ).保存文件后,Git会依次签出每个更改,您可以根据自己的喜好进行操作.

To do this, first checkout master and then git rebase -i origin/master. This will throw you into an editor where you can choose what to do with each commit. You probably want to edit them (e). Once you save the file Git will checkout each change in turn and you can do what you like with it.

如果您想拆分提交,请使用 git add -p git add -i 一次将更改添加到登台区域,然后 git commit 提交更改的一部分.

If you want to split up a commit, use git add -p and git add -i to add the change a piece at a time to the staging area, then git commit to commit just that one part of the change.

有关更多详细信息,请阅读 Pro Git中的重写历史记录和Github的关于Git Rebase .

For more detail, read Rewriting History in Pro Git and Github's About Git Rebase.

这篇关于重写整个git历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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