Is there anyway for me to delete my github commit history? I don't want to lose all my commits, just the last 10 or so that were pushed to github. I can't seem to figure out how to do this.
-
4See http://stackoverflow.com/questions/495345/git-removing-selected-commits-from-repository Note that doing this kind of thing is a bad idea if there are other people collaborating on the same repository. It's usually better to get over the bumps in the history and just move forward. – John Flatness Aug 13 '11 at 05:04
4 Answers
Just pick the hash you want to go back to and in your clone do:
git reset --hard hash#
git push -f origin branch
where branch
is the name of the branch you want to push. Voilà. Be carefully with the force push. You may want to copy your working directory until you are familiar with it.

- 32,300
- 7
- 79
- 115

- 1,446
- 12
- 16
-
1Worked like a charm! Except that I didn't realize that branch meant the name of my branch :P – Manish Burman Aug 13 '11 at 05:29
-
1
-
1i use tig to look through my hash history to find the one i want to go back to. You can also use gitk or the git log or another tool like source tree. A variety of options exist to inspect the history and find a hash – matthewdaniel Dec 18 '13 at 16:05
-
4This should not be the accepted answer. This does not delete the history, merely resets the history to a previous point. All commits can still be accessed with the hash identifier. Please read, as posted by @Droogans, [Delete sensitive information](https://help.github.com/articles/remove-sensitive-data/) – ctatro85 Jan 05 '15 at 22:18
-
To know your current branch use: git branch. The one with a star '*' is your current branch. – Ashesh Jan 07 '15 at 18:14
-
@MrBandersnatch - once the branch is reset, how do you get hold of the hashes if you don't already have them? – Charles Prakash Dasari Apr 13 '15 at 22:27
-
1@CharlesPrakashDasari Git reflog is a good place to start. Helps recover an accidental delete or overwrite or forced pull. Not to mention github caches there pages so it is possible ( Although not probable ) someone visited that page and may have it bookmarked ( As mentioned in the link provided prior ) Perhaps I misunderstood the authors question. – ctatro85 Apr 15 '15 at 20:31
-
Very clean :). hash# is the commit id, something like "48cbceedfa2546322c20bab3466fe6257ef750f2d2cc" – Wasim Sep 22 '15 at 09:49
If you want to do this, you can use HEAD~10
git reset --hard HEAD~10
git push -f origin master
It is not recommended to do delete server commit history, especially: if you have a team of people working on this repo.
If you have a team of people working on this, I would recommend to fall back by adding another commit that undo your code you don't want.

- 13,008
- 21
- 97
- 158

- 25,806
- 45
- 116
- 158
I had uploaded all of my files to my first github project, including some settings files with secret keys. I had to go back and remove all the versions that were included before I added my .gitignore
file, and deleted all settings files from my project's cache.
gedit ./.git/logs/HEAD
Find the 40-character SHA hash of the 'new root' you wish to delete everything prior to. Copy it to the clipboard and close it. (You may or may not use gedit
, a default text editor for linux).
$> gedit ./.git/info/grafts
Paste the SHA here. Go to your project's directory.
$> cd ../..
$> git filter-branch
Force the push to the master, otherwise it will block your attempt to erase your history.
$> git push --force -u origin master
Delete your grafts
file.
$> rm ./.git/info/grafts
NEVERMIND
Well, my answer is only half the battle. My public activity RSS feed still has links to all the diffs that detail the very information I wanted to remove. Apparently, there is no way to delete this, but you should probably read Change your Password, and update any sensitive data you may have accidentally uploaded.
-
1I just wanted to add to this since I ran into it today due to a _very_ unusual situation I created for myself. But one solution you can do once you've done the [Remove Sensitive Data](http://help.github.com/remove-sensitive-data/) step is, create a new repository and push all your _now clean_ commits, and delete the old repository. This invalidates links to your old branches/diffs in your public activitiy feed and github will throw a 404 since the repo no longer exists. – C Jones May 16 '16 at 17:21
With Git installed, you could also right-click your repository folder - Git GUI Here.
Select Repository - Visualize All Branch History.
Right-click the desired commit - Reset master branch history to here.
Now you have 3 options:
Once chosen, select File - Reload.
Managed to figure this out after selecting Revert on a commit in GitHub Desktop, then Revert again so we're back to the original code, then simply wishing to clean up these couple Revert commits.

- 10,407
- 3
- 45
- 62