I am curious about how to remove the first commit in git.
What is the revision before committing any thing? Does this revision have a name or tag?
The easiest way to undo the last Git commit is to execute the “git reset” command with the “–soft” option that will preserve changes done to your files. You have to specify the commit to undo which is “HEAD~1” in this case. The last commit will be removed from your Git history.
To remove the last commit from git, you can simply run git reset --hard HEAD^ If you are removing multiple commits from the top, you can run git reset --hard HEAD~2 to remove the last two commits.
The git revert command will undo a commit so you can return a repository to the previous commit. Instead of deleting the commit, revert will create a new commit that will reverse the changes of a published commit. This preserves the initial commit as a part of the project's history.
For me, the most secure way is to use the update-ref
command:
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.
If what you want is to merge the first commit with the second one, you can use the rebase
command:
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>
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.
If you just want to start all over, delete the .git
repository, and make another one with git init
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With