Shuzheng
Shuzheng

Reputation: 13996

How is it possible to reset a Git repository to the state before the first commit?

I know that I can go back in history to earlier commits using git reset --hard <commit>. However, this only lets me roll back to the first commit (not before the first commit).

I want to do this, since I created a Git repository and committed a lot of wrong files and directories. Simply deleting the repository and starting over is not an option here, since repositories are created by an admin.

How can I reset my Git repository to the state before the first commit?

Upvotes: 1

Views: 451

Answers (3)

Quentin
Quentin

Reputation: 63154

git checkout --orphan temp      # Start a new history with a dummy initial branch
# Prepare your first commit...
git commit                      # As usual, this will actually create the temp branch
git checkout -B master          # Bring master onto this new commit
git push --force origin master  # Update the remote, dropping the old history
git branch -d temp              # Delete the temporary branch

Upvotes: 3

Joachim Sauer
Joachim Sauer

Reputation: 308149

As an alternative to amending the first commit, you can also create a fully new branch using git checkout --orphan and then reset master to that one:

git checkout --orphan new_master
# add files
git commit -m "new initial commit"
git checkout master
git reset --hard new_master
git branch -D new_master

Upvotes: 3

Maroun
Maroun

Reputation: 95998

You can do whatever changes you want, and amend to the last commit:

# do changes
git commit --amend
# enter a new commit message if you wish

If you want to merge two commits, you can use git rebase -i --root which allows you to handle the very first commit with the next ones.

Upvotes: 3

Related Questions