git 如何从一开始就重新设置所有提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8605447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 06:17:17  来源:igfitidea点击:

How to rebase all the commits from the beginning

gitgithub

提问by plus-

So I'm migrating from svn (code.google.com) to git(github).

所以我要从 svn (code.google.com) 迁移到 git(github)。

I've imported my project from the svn repo, and it imported all the commit history along way. I'm not really proud of those comments as it was one of my first project, not really serious.

我已经从 svn repo 导入了我的项目,并且它沿途导入了所有提交历史记录。我对这些评论并不感到自豪,因为这是我的第一个项目之一,并不是很认真。

I'd like to rebase everything into a single 'initial import' commit.

我想将所有内容重新设置为单个“初始导入”提交。

I've pulled everything on my computer and I'm trying to do that. But all I found was: git rebase -i masterbut it only rebases new modifications and commits.

我已经把我电脑上的所有东西都拉出来了,我正在努力做到这一点。但我发现的只是: git rebase -i master但它只会重新调整新的修改和提交。

How can I clean my github repository from all history with a rebase?

如何使用 rebase 从所有历史记录中清除我的 github 存储库?

回答by Tobias J

git rebase -i --rootwill start an interactive rebase of all commits from the beginning.

git rebase -i --root将从头开始对所有提交进行交互式变基。

From there, you can squash all commits into one and/or perform other edits.

从那里,您可以将所有提交压缩为一个和/或执行其他编辑。

回答by Cascabel

You could rebase and squash everything if you wanted to (except the initial commit) but why bother? Simply delete your .git directory, run git initto recreate it, git addeverything, and git committo make a new initial commit.

如果你愿意,你可以变基和压缩一切(除了最初的提交),但为什么要麻烦呢?只需删除您的 .git 目录,运行git init以重新创建它,git add所有内容,并git commit进行新的初始提交。

回答by Kevin M Granger

Jefromi's answer will work, but if you want to keep your existing repo configuration, or even leave around the commits just in case, you could do the following:

Jefromi 的答案会起作用,但如果您想保留现有的 repo 配置,或者甚至保留提交以防万一,您可以执行以下操作:

git checkout master

git checkout master

git branch backupoptionally leave another branch here in case you want to keep your history.

git branch backup如果您想保留历史记录,可以选择在此处留下另一个分支。

git reset --soft $SHA_OF_INIT_COMMITthis will update what HEAD is pointing to but leave your index and working directory in their current state. You can get the SHA with git log --pretty=format:%h --reverse | head -n 1, and make this one step with git reset --soft $(git log --pretty=format:%h --reverse | head -n 1)

git reset --soft $SHA_OF_INIT_COMMIT这将更新 HEAD 指向的内容,但将索引和工作目录保持在当前状态。您可以使用 SHA 获取 SHA git log --pretty=format:%h --reverse | head -n 1,并使用git reset --soft $(git log --pretty=format:%h --reverse | head -n 1)

git commit --amendchange your initial commit to point to the current state of your repo.

git commit --amend更改您的初始提交以指向您的回购的当前状态。

回答by Abizern

Find the hash of the commit that you want to start squashing from say abcd12and then rebase against that hash specifically.

找到您要从 say 开始压缩的提交的哈希,abcd12然后专门针对该哈希进行变基。

git rebase -i abcd12

You are using masterto rebase against, which performs the rebase against the tip of the master branch.

您正在使用masterrebase,它针对 master 分支的尖端执行 rebase。

回答by Jan

If you'd like to reduce all your history to a single "Initial import" commit, simply remove .gitdirectory and create a new local repository (keeping a backupof the old one). git init . && git add . && git commit -m "Initial import".

如果您想将所有历史记录减少为单个“初始导入”提交,只需删除.git目录并创建一个新的本地存储库(保留旧存储库的备份)。git init . && git add . && git commit -m "Initial import".

Such new repository won't have a common ancestor with the one you've pushed to GitHub, so you'll have to git push --forceyour newly created repository.

这样的新存储库与您推送到 GitHub 的存储库没有共同的祖先,因此您必须使用git push --force新创建的存储库。