206

How can I reset a remote and local Git repository to remove all commits?

I would like to start fresh with the current Head as the initial commit.

5
  • related: stackoverflow.com/questions/495345/…
    – miku
    Jan 5, 2010 at 13:11
  • I don't want to cherry pick or do anything else, I just want to remove all changes and reset the public repo also. As I am new Git user, I made some wrong commits. Removing .GIT directory is not an option as there is a public repository also. Jan 5, 2010 at 13:22
  • You can do a force push also, so removing the .git dir is actually an option. Jan 5, 2010 at 13:24
  • 2
    just nitpicking, but "revision" is an svn terminology and doesn't make much sense in a tree shaped history. Nov 30, 2011 at 15:41
  • 3
    @TamásSzelei "Revision" is a perfectly acceptable synonym for "commit". It's used both in the Pro Git book (e.g. here) and in the Git man pages.
    – jub0bs
    Apr 10, 2015 at 15:44

3 Answers 3

429

Completely reset?

  1. Delete the .git directory locally.

  2. Recreate the git repostory:

    $ cd (project-directory)
    $ git init
    $ (add some files)
    $ git add .
    $ git commit -m 'Initial commit'
    
  3. Push to remote server, overwriting. Remember you're going to mess everyone else up doing this … you better be the only client.

    $ git remote add origin <url>
    $ git push --force --set-upstream origin master
    
8
  • 1
    but the commits are gone, and repository is reset. Can we remove those previous messages also in front of the filenames. Jan 5, 2010 at 13:45
  • 5
    Remove the remote repository directly on GitHub and recreate it there.
    – Bombe
    Jan 5, 2010 at 14:14
  • 1
    Should be git push --force instead of git push -force Nov 30, 2011 at 7:26
  • 13
    If you don't want to commit any files in your "Initial commit", you can just not add any files, and add the --allow-empty flag at the end of git commit -m 'Initial commit'.
    – user2005477
    Jul 3, 2014 at 12:49
  • 2
    Using Git 2.3.2, I had to use git push --force --set-upstream origin master But all the rest was working as described May 6, 2015 at 16:42
7

First, follow the instructions in this question to squash everything to a single commit. Then make a forced push to the remote:

$ git push origin +master

And optionally delete all other branches both locally and remotely:

$ git push origin :<branch>
$ git branch -d <branch>
3

Were I you I would do something like this:

Before doing anything please keep a copy (better safe than sorry)

git checkout master
git checkout -b temp 
git reset --hard <sha-1 of your first commit> 
git add .
git commit -m 'Squash all commits in single one'
git push origin temp

After doing that you can delete other branches.

Result: You are going to have a branch with only 2 commits.

Use git log --oneline to see your commits in a minimalistic way and to find SHA-1 for commits!

1
  • This is a great idea if you're not changing the remote origin Nov 17, 2020 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.