Illusionist Illusionist -5 years ago 189
Git Question

Git show only current state of files on branch and delete commit history

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 stash
git reset --hard
git pop
git add file
git commit -m 'commit'
git push -f origin

help is appreciated.

Answer Source

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 <sha>:

$ 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 pick, squash, 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download