我很好奇如何删除git中的第一次提交。

做任何事情之前的修改是什么?这个修订版有名称或标签吗?


当前回答

# Check out to a temporary branch:
git checkout --orphan TEMP_BRANCH

# Add all the files:
git add -A

# Commit the changes:
git commit -am "Initial commit"

# Delete the old branch:
git branch -D master

# Rename the temporary branch to master:
git branch -m master

# Finally, force update to our repository:
git push -f origin master

其他回答

我正在寻找一种方法来撤销所有的git提交从一个回购,就像他们从来没有发生过。

调整基准在一定程度上会起作用。但是,第一次(按时间顺序来看,最早的git提交)总是会有问题,因为它没有父节点,所以会出错。

这些答案都不能完全解决我的问题。但经过大量的搜索和试错,我发现这个工作!

git update-ref -d HEAD
git push origin master -f

希望这对你有所帮助。祝你有愉快的一天。

在第一次提交之前什么都没有,因为每次提交都引用父提交。这使得第一次提交非常特殊(孤儿提交),因此没有办法引用前一个“状态”。

所以如果你想修复提交,你可以简单地git commit——amend:这将修改提交而不创建另一个。

如果你只是想从头开始,删除.git存储库,并使用git init创建另一个

如果你只是提交了它,但没有推送它,那么只需删除。git目录并再次init git…

# Check out to a temporary branch:
git checkout --orphan TEMP_BRANCH

# Add all the files:
git add -A

# Commit the changes:
git commit -am "Initial commit"

# Delete the old branch:
git branch -D master

# Rename the temporary branch to master:
git branch -m master

# Finally, force update to our repository:
git push -f origin master

如果你想保留其他分支,但例如让主分支重新启动,而不向其他分支提供公共历史记录,一个安全的方法是创建一个新的存储库,并将其内容推送到旧的存储库中:

cd ..
git init newrepo
cd newrepo
# make some initial commits
git push ../oldrepo master:newmaster

这将在旧存储库中创建newmaster分支,其历史记录与任何其他分支都不同。当然,你也可以用git push -f来覆盖master。

如果您想销毁所有分支和所有现有内容,那么只需运行即可

rm -rf .git/