5

I load on git some project which contains some personal data. Now I changed all lines and commit it. Now I need to erase all commits except last one to prevent loosing personal data.

Aleksandr
  • 787
  • 6
  • 22
  • The best way is to merge all commits into one, which is answered by this question: http://stackoverflow.com/questions/7425725/how-to-merge-all-previous-commits-into-a-single-commit?rq=1 – Jon Cairns Mar 25 '13 at 14:43
  • finally, I just bought account to hide this code – Aleksandr Apr 23 '13 at 19:36

2 Answers2

13

Given that your main branch is called master, and you want to remove all commits except the last one from your master:

  1. git checkout --orphan tmp
  2. git add . --all
  3. git commit -m "Init."
  4. git push origin tmp
  5. On your remote git repo select tmp as main branch
  6. git branch -D master
  7. git push origin :master
  8. git checkout -b master
  9. git push origin master
  10. On your remote git repo select master as main branch
  11. git branch -D tmp
  12. git push origin :tmp
Stephan Kristyn
  • 15,015
  • 14
  • 88
  • 147
0

The easiest solution is to remove the .git directory.

Open the Linux Terminal.
Move to your repository and:

cat .git/config

Note the url variable and the name of the branch:
$my_url
$my_branch

rm -rf .git

git init
git add .
git commit -m "Initial commit"

git remote add origin $my_url
git push -u --force origin $my_branch

I made this bash script which can be improved. Awaiting suggestions.

Claudio Fsr
  • 106
  • 6