Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: how to delete all commits except last one

Tags:

github

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.

like image 477
Aleksandr Avatar asked Dec 26 '22 07:12

Aleksandr


1 Answers

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
like image 119
stevek-pro Avatar answered Jan 13 '23 13:01

stevek-pro