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:
- stashed work
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.