简体   繁体   English

如何在git中重写历史记录?

[英]How to rewrite history in git?

I cloned a project repository and did some commits to it. 我克隆了一个项目存储库,并对它做了一些提交。 The commits were pretty dirty; 这些提交很脏。 I messed up tabs-spaces and committed some trash along the way. 我弄乱了制表符空间,并在此过程中丢了一些垃圾。 So before making a pull request I want to make things tidy. 因此,在发出拉动请求之前,我想使事情整洁。 I think the best way would be making a new branch at origin/master and then apply commits from my old branch one by one to a working copy and doing clean commits to a new branch. 我认为最好的方法是在origin/master上创建一个新分支,然后将我旧分支中的提交一个接一个地应用于工作副本,然后对新分支进行干净提交。

So how to apply commit from other branch to a working copy? 那么如何将其他分支的提交应用于工作副本呢? Is it the best way to do it? 这是最好的方法吗? Such task seems pretty common and maybe there is already an established workflow and tools for such things? 这样的任务看起来很普通,也许已经建立了用于此类事情的工作流程和工具?

Let's say the work tree is something like: 假设工作树类似于:

img

and you want to apply only gh to the origin/master . 并且您只想将gh应用于origin/master First off, create a new branch using origin/master , and another for commits upto h only: 首先,使用origin/master创建一个新分支,然后另一个分支最多提交h

git checkout -b new-branch origin/master
git checkout -b daughter-branch h

Now, apply commits from g onwards 现在,从g开始应用提交

git rebase --onto new-branch g^

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

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