TIL - How to Git Reset the First Commit

The First Git Commit

So, you find your self in a situation where you have created your first git commit in a brand new repo, but you have committed some files you won’t want to track.

To solve this you can go the Git way or you can just recreate the Git repo.

The Git Ways of Resetting the First Git Commit

Using one of the Git approaches will reset the first git commit without loosing:

  • files
  • stashed work
  • branches
  • reflog

Reset in Current Branch

This command will DELETE ALL your git history, but will preserve all the files in the staged area.

git update-ref -d HEAD

IMPORTANT: Be sure to not run this command in a repo that where you don’t want too loose all your git history.

Now, you can choose to unstage all the files with git reset or just unstage one file at a time.

Reset to a New Branch

git checkout --orphan new-branch-name

Recreate the Branch

git checkout --orphan new-branch-name
git branch -D previous-branch-name
git checkout -b previous-branch-name
git branch -D new-branch-name

Alternative Way - Recreate the Git Repository

rm -rvf .git
git init

Disclaimer: What I expressed here is only in my behalf and doesn't represent the company I work for, or any previous one, neither my family, friends, colleagues or anyone else unless I explicitly say so.

Keeping me Motivated

Was this post useful to you? Did you enjoyed reading it? Did you learned something new?

If so, in return I only ask you to show your appreciation by Sharing It:

Don't miss my next article. To stay on the loop, just Follow Me at:

If you are feelling really generous you can always offer me a coffee or a beer.

Your actions will keep me MOTIVATED to continue writing more Blog Posts 😃

Comments

Disqus is disabled on my Blog because I don't agree with their policies.