I am new to git. I have a lot of unnecessary commits committed to my branch.
I want to keep the current state of files and remove all commits history. I don't mind resetting the branch. What's the best way to do this?
I think this should be something like
git reset --hard
git add file
git commit -m 'commit'
git push -f origin
Probably the best way to do what you want is firstly to turn all the local changes into a commit.
$ git commit -a -m "work-in-progress"
Then, use interactive rebase to squash the commits back to just before a given commit
$ git rebase -i <sha>^
Interactive rebase brings up an edit window with a list of commits. For each one
you can specify one of several actions, like
fixup and others. If you put
squash for each commit, they will be picked and collapsed together, and you will be able to edit the commit message, which will be initially based on a catenation of their individual commit messages.
The interactive rebase workflow is one of the features in
git that is really worth learning.