git 如何删除git中的第一个提交?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10911317/
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
How to remove the first commit in git?
提问by Jian Weihang
I am curious about how to remove the first commit in git.
我很好奇如何删除 git 中的第一个提交。
What is the revision before committing any thing? Does this revision have a name or tag?
提交任何事情之前的修订是什么?此修订版是否有名称或标签?
回答by tzi
For me, the most secure way is to use the update-ref
command:
对我来说,最安全的方法是使用update-ref
命令:
git update-ref -d HEAD
It will delete the named reference HEAD
, so it will reset (softly, you will not lose your work) all your commits of your current branch.
它将删除命名引用HEAD
,因此它将重置(轻柔地,您不会丢失您的工作)您当前分支的所有提交。
If what you want is to merge the first commit with the second one, you can use the rebase
command:
如果您想要将第一个提交与第二个提交合并,则可以使用以下rebase
命令:
git rebase -i --root
A last way could be to create an orphan branch, a branch with the same content but without any commit history, and commit your new content on it:
最后一种方法可能是创建一个孤立分支,一个具有相同内容但没有任何提交历史的分支,并在其上提交您的新内容:
git checkout --orphan <new-branch-name>
回答by CharlesB
There is nothing before the first commit, as every commit is referring a parent commit. This makes the first commit special (an orphan commit), so there is no way to refer to a previous "state".
在第一次提交之前没有任何内容,因为每个提交都引用一个父提交。这使得第一次提交变得特别(孤儿提交),因此无法引用先前的“状态”。
So if you want to fix the commit, you can simply git commit --amend
: this will modify the commit without creating another one.
所以如果你想修复提交,你可以简单地git commit --amend
:这将修改提交而不创建另一个提交。
If you just want to start all over, delete the .git
repository, and make another one with git init
如果您只想从头开始,请删除.git
存储库,然后再创建一个git init
回答by Megha Sahu
# 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
回答by Max Beikirch
You might just want to edit your first commit (as there is always a first commit in a git repo). Consider using git commit --amend --reset-author
instead of the usual git commit --amend
.
您可能只想编辑您的第一次提交(因为 git 存储库中总是有第一次提交)。考虑使用git commit --amend --reset-author
而不是通常的git commit --amend
.
回答by kumarharsh
Another way you can do is:
你可以做的另一种方法是:
- Checkout to a branch you want to keep (say dev)
git checkout dev
- Now, delete the branch you want to reset
git branch -D master
- Now, create an empty branch with the same name
git checkout --orphan master
- 结帐到您要保留的分支(例如 dev)
git checkout dev
- 现在,删除您要重置的分支
git branch -D master
- 现在,创建一个同名的空分支
git checkout --orphan master
Ofcourse, all of this would depend on your usecase, but if you have more than one branch, deleting the .git
directory does not make sense.
当然,这一切都取决于你的用例,但如果你有多个分支,删除.git
目录是没有意义的。
回答by user1338062
If you want to keep other branches, but for example make the master
branch start anew without common history to other branches, one safe way to achieve this is to create a new repository, and push contents of that in your old one:
如果你想保留其他分支,但例如让master
分支重新开始,而没有其他分支的共同历史,实现这一点的一种安全方法是创建一个新存储库,并将其内容推送到旧存储库中:
cd ..
git init newrepo
cd newrepo
# make some initial commits
git push ../oldrepo master:newmaster
This creates newmaster
branch in the old repository, with history that is not common with any of the other branches. Of course, you can just overwrite the master
as well, with git push -f
.
这会newmaster
在旧存储库中创建分支,其历史记录与任何其他分支都不常见。当然,您也可以master
使用git push -f
.
If you want to destroy all branches and all existing content, then just run
如果要销毁所有分支和所有现有内容,则只需运行
rm -rf .git/
回答by kp123
I was looking for a way to undo all git commits from a repo, like they never happened.
我正在寻找一种方法来撤消所有来自 repo 的 git 提交,就像它们从未发生过一样。
Rebasing will work up to a point. However, the very first (chronologically the oldest git commit) is always going to be problematic, since it does not have a parent, so that will error out.
重新定位将在一定程度上起作用。然而,第一个(按时间顺序最老的 git 提交)总是会有问题,因为它没有父级,所以会出错。
None of these answers quite solved it for me. But after lots of searching and trial-and-error, I found this to work!
这些答案都没有为我解决。但是经过大量的搜索和反复试验,我发现这可行!
git update-ref -d HEAD
git push origin master -f
Hope this helps you. Have a great day.
希望这对你有帮助。祝你有美好的一天。
回答by Ali Sajid
If you have just committed it but not pushed it then just remove .gitdirectory and git init
again...
如果您刚刚提交但没有推送它,那么只需删除.git目录并git init
再次...
回答by MikeBeaton
The answer to the question depends on whether:
问题的答案取决于:
You want to remove the first AND ONLY commit on a branch (whilst leaving other branches as they were), or whether
You want to remove the first commit on some branch, whilst 'leaving in place' the subsequent commits (and the other branches).
您想删除一个分支上的第一个 AND ONLY 提交(同时保留其他分支原样),或者是否
您想删除某个分支上的第一个提交,同时“保留”后续提交(和其他分支)。
The second of these is relatively simpler. You essentially have to rebase on to root - most of the answers here are about ways to do this.
其中第二个相对简单。您基本上必须重新定位到 root - 这里的大多数答案都是关于执行此操作的方法。
To do the second (removing the first and only commit from a branch whilst leaving other branches alone) is harder. Or, at least, especially harder if you want it to happen and for the change to be reflected back in GitHub or Bitbucket. There is (as far as I can tell) no way at all in Git to push or force push a branch with no commits. And there is also (again, as far as I can see) no way at all to create a new, empty branch with no commits on it in either GitHub or Bitbucket. So you essentially have to create a new repository in order to create a completely empty branch, and then add back the branches which you want (including the commits which you want) - as per @user1338062's answer.
做第二个(从一个分支中删除第一个也是唯一的提交,同时不理会其他分支)更难。或者,至少,如果您希望它发生并且更改反映在 GitHub 或 Bitbucket 中,则尤其困难。(据我所知)在 Git 中根本没有办法推送或强制推送一个没有提交的分支。而且(同样,据我所知)根本无法在 GitHub 或 Bitbucket 中创建一个没有提交的新的空分支。因此,您基本上必须创建一个新存储库以创建一个完全空的分支,然后添加回您想要的分支(包括您想要的提交) - 根据@user1338062 的回答。
So I hope this answer clarifies what might not have been obvious - that there are two different approaches that need to be taken, for two different (more or less reasonable) scenarios which are boththings you might want to be able to do, in order to fully master doing what the OP asks.
所以我希望这个答案澄清了可能不明显的事情 - 需要采取两种不同的方法,用于两种不同(或多或少合理)的场景,这两种情况都是您可能希望能够做的,以便完全掌握执行 OP 要求的操作。