简体   繁体   English

如何将工作移至新分支并删除所有提交历史记录?

[英]How to move work on to a new branch and remove all commit history?

I have royally messed up a branch. 我已经搞砸了一个分支。

I've been key slamming and have merged/ pulled/ squashed and i don't know what else. 我一直在猛击,已经合并/拉扯/挤压,我不知道还有什么。

All the work on my feature branch should be in 1 commit 我的功能分支上的所有工作都应该在1次提交中

I want to keep where my work is at but I don't want the 10 commit histories. 我想保留我的工作位置,但是我不希望有10个提交历史。

I can't squash commits anymore as there are conflicts and it's just gotten to be such a mess. 由于冲突,我再也无法压扁提交了,只是一团糟。

Squashing is no longer and option because git rebase-i HEAD~2 i've done a merge at some point and can't do that - because in the middle of a rebase my changes couldn't be applied because I some how got behind remote by squashing - I must have done it wrong. 压扁不再是一种选择,因为git rebase-i HEAD〜2我在某个时候完成了合并,但是无法做到这一点-因为在重新设置基础期间,由于某些原因,我的更改无法应用压扁遥控-我一定做错了。

So what I would like to do is checkout a new branch and delete all of my commit history prior to the last one. 因此,我想做的是签出一个新分支,并删除上一个分支之前的所有提交历史记录。

What is the best way to do this? 做这个的最好方式是什么? Also can you please explain the solution like I'm 5 please? 您还可以像我5岁时一样解释解决方案吗?

If you have a commit whose files have the right content, you can create a new branch from it. 如果提交的文件内容正确,则可以从中创建一个新分支。 Suppose the commit hash is abc123 and the new branch is foo 假设提交哈希为abc123 ,新分支为foo

git checkout --orphan foo abc123
git commit

Now you have foo and it has only one commit, which has the same files with abc123 . 现在您有了foo ,并且只有一个提交,该提交与abc123具有相同的文件。 And then you can delete the old branch. 然后,您可以删除旧分支。

git branch -D <old_branch>

But in case you may regret, just keep the old branch until you are sure you don't want it any longer. 但是,以防万一您可能后悔,只需保留旧分支,直到确定您不再想要它为止。

If you have all the files you need on your working directory, you can just erase the current git repository and create a new one: 如果您在工作目录中拥有所需的所有文件,则只需删除当前的git存储库并创建一个新的git存储库:

rm -rf .git
git init
git add .
git commit -m "initial commit"

What you could also do if you wanted to keep the reflog intact is to use reset as a cheap way of rebasing. 如果您想保持reflog不变,您还可以做的是使用reset作为廉价的重新定基方法。 Find out the hash of your initial commit, reset --mixed to it, keeping all of the changes in your working directory and then commit --amend to add these changes to the initial commit: 找出您的初始提交的哈希值,将其重置-与之混合,将所有更改保留在您的工作目录中,然后提交--amend将这些更改添加到初始提交中:

# find out the id of your first commit
git rev-list --max-parents=0 --abbrev-commit HEAD

# reset to it
git reset --mixed <first commit id>

# add files to index and amend
git add .
git commit --amend -m "initial commit"

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

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