Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the first commit in git?

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?

like image 827
Weihang Jian Avatar asked Oct 16 '22 17:10

Weihang Jian


People also ask

How do I remove one commit in git?

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.

How do I remove a top commit?

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.

How do I revert back to a first commit?

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.


2 Answers

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>
like image 494
tzi Avatar answered Oct 19 '22 05:10

tzi


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

like image 44
CharlesB Avatar answered Oct 19 '22 07:10

CharlesB