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.

Promise Preston
  • 24,334
  • 12
  • 145
  • 143
Priyank Bolia
  • 14,077
  • 14
  • 61
  • 82
  • related: http://stackoverflow.com/questions/495345/git-removing-selected-commits-from-repository – miku Jan 05 '10 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. – Priyank Bolia Jan 05 '10 at 13:22
  • You can do a force push also, so removing the .git dir is actually an option. – Lilith River Jan 05 '10 at 13:24
  • 2
    just nitpicking, but "revision" is an svn terminology and doesn't make much sense in a tree shaped history. – Tamás Szelei Nov 30 '11 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](http://git-scm.com/book/en/v2/Git-Tools-Revision-Selection)) and in the Git man pages. – jub0bs Apr 10 '15 at 15:44

3 Answers3

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
    
Lilith River
  • 16,204
  • 2
  • 44
  • 76
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>
Community
  • 1
  • 1
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
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!

Kwnstantinos Nikoloutsos
  • 1,832
  • 4
  • 18
  • 34