我曾经问过如何压缩git存储库中的前两次提交。

虽然这些解决方案相当有趣,而且不像git中的其他一些东西那样令人费解,但如果您需要在项目开发过程中多次重复这个过程,那么它们仍然是一个众所周知的伤害。

所以,我宁愿只经历一次痛苦,然后能够永远使用标准的交互式rebase。

那么,我想做的是有一个空的初始提交,它的存在仅仅是为了成为第一个。没有代码,什么都没有。只是占地方,这样就可以做地基了。

我的问题是,有了一个现有的存储库,我如何在第一个提交之前插入一个新的空提交,并将其他所有人向前移动?


当前回答

以下是我的bash脚本基于肯特的回答与改进:

it checks out the original branch, not just master, when done; I tried to avoid the temporary branch, but git checkout --orphan only works with a branch, not detached-head state, so it's checked out long enough to make the new root commit and then deleted; it uses the hash of the new root commit during the filter-branch (Kent left a placeholder in there for manual replacement); the filter-branch operation rewrites only the local branches, not remotes too the author and committer metadata is standardised so that the root commit is identical across repositories.


#!/bin/bash

# Save the current branch so we can check it out again later
INITIAL_BRANCH=`git symbolic-ref --short HEAD`
TEMP_BRANCH='newroot'

# Create a new temporary branch at a new root, and remove everything from the tree
git checkout --orphan "$TEMP_BRANCH"
git rm -rf .

# Commit this empty state with generic metadata that will not change - this should result in the same commit hash every time
export GIT_AUTHOR_NAME='nobody'
export GIT_AUTHOR_EMAIL='nobody@example.org'
export GIT_AUTHOR_DATE='2000-01-01T00:00:00+0000'
export GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME"
export GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL"
export GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
git commit --allow-empty -m 'empty root'
NEWROOT=`git rev-parse HEAD`

# Check out the commit we just made and delete the temporary branch
git checkout --detach "$NEWROOT"
git branch -D "$TEMP_BRANCH"

# Rewrite all the local branches to insert the new root commit, delete the 
# original/* branches left behind, and check out the rewritten initial branch
git filter-branch --parent-filter "sed \"s/^\$/-p $NEWROOT/\"" --tag-name-filter cat -- --branches
git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d
git checkout "$INITIAL_BRANCH"

其他回答

合并亚里士多德Pagaltzis和Uwe Kleine-König的答案和理查德布罗诺斯基的评论。

git symbolic-ref HEAD refs/heads/newroot
git rm --cached -r .
git clean -f -d
# touch .gitignore && git add .gitignore # if necessary
git commit --allow-empty -m 'initial'
git rebase --onto newroot --root master
git branch -d newroot

(只是为了把所有的东西放在一个地方)

实现这一目标有2个步骤:

创建一个新的空提交 重写历史,从这个空提交开始

为了方便起见,我们将把新的空提交放在一个临时分支newroot上。

1. 创建一个新的空提交

有很多方法可以做到这一点。

只使用管道

最简洁的方法是使用Git的管道直接创建一个提交,这避免了触及工作副本、索引或检出哪个分支等。

为空目录创建一个树对象: Tree = ' git hash-object -wt Tree——stdin < /dev/null ' 在它周围包装一个提交: 提交= ' git Commit -tree -m '根提交' $tree ' 创建对它的引用: Git分支newroot $commit

当然,如果您足够了解shell,您可以将整个过程重新排列为一行程序。

没有管道

使用常规的瓷器命令,如果不签出新根分支并重复更新索引和工作副本,就不能创建空提交。但有些人可能会觉得这更容易理解:

git checkout --orphan newroot
git rm -rf .
git clean -fd
git commit --allow-empty -m 'root commit'

注意,在非常旧的Git版本中,没有——孤儿开关来签出,你必须用这个替换第一行:

git symbolic-ref HEAD refs/heads/newroot

2. 重写历史,从这个空提交开始

您有两个选择:重设基础,或重写干净的历史记录。

变基

git rebase --onto newroot --root master

这具有简单的优点。但是,它还将在分支上的每次提交时更新提交者名称和日期。

此外,对于一些边缘历史案例,它甚至可能由于合并冲突而失败——尽管事实上您正在基于一个不包含任何内容的提交。

历史改写

更清晰的方法是重写分支。与git rebase不同,你需要查找你的分支是从哪个提交开始的:

git replace <currentroot> --graft newroot
git filter-branch master

显然,重写发生在第二步;这是需要解释的第一步。git replace所做的是告诉git,无论何时它看到一个你想要替换的对象的引用,git都应该查看该对象的替换。

通过这个-嫁接开关,你会告诉它一些与正常情况略有不同的东西。你说的是还没有替换对象,但你想要替换<currentroot>提交对象与自己的精确副本,除了替换的父提交应该是你列出的(即newroot提交)。然后git replace继续为你创建这个commit,然后声明这个commit替换了你原来的commit。

现在如果你做一个git日志,你会看到事情已经像你想要的那样:分支从newroot开始。

但是,请注意,git replace实际上并不修改历史记录——它也不会传播到存储库之外。它只是在存储库中添加一个从一个对象到另一个对象的本地重定向。这意味着没有人能看到这种替代的影响——只有你自己。

这就是为什么过滤器分支步骤是必要的。使用git replace,你可以创建一个精确的副本,调整了根提交的父提交;然后,Git过滤器分支对接下来的所有提交也重复这个过程。这就是历史被改写的地方,这样你就可以分享它。

切换根提交。

首先,创建您想要作为第一个提交的提交。

其次,使用以下命令切换提交顺序:

Git rebase -i—root

在根节点提交之前,会出现一个编辑器,比如:

选择1234旧根消息

在中间提交

选择你想放在根目录下的5678提交

然后,您可以将您想要的提交放在第一行中。在这个例子中:

选择你想放在根目录下的5678提交

选择1234旧根消息

在中间提交

退出编辑器,提交顺序将会改变。

PS:要更改git使用的编辑器,运行:

Git配置——全局核心。编辑name_of_the_editor_program_you_want_to_use

Git将——root——重基到$emptyrootcommit

应该很容易做到吗

结合最新和最好的。没有副作用,没有冲突,保持标签。

git log --reverse

tree=`git hash-object -wt tree --stdin < /dev/null`
commit=`git commit-tree -m 'Initialize empty repository' $tree`
echo $commit # copy below, interpolation didn't work for me

git filter-branch --parent-filter 'sed "s/^\$/-p <commit>/"' --tag-name-filter cat master

git log --reverse

请注意,在GitHub上,您将丢失CI运行数据和PR可能会搞砸,除非其他分支也得到修复。