Search code examples
gitcommitgit-branchpull-requestgit-remote

Keep state of latest git commit--but remove my own, earlier commits I do not want to show.


I could not quite find an answer to this simple problem. I have created a feature branch and have added several commits. No one else has committed. I have already pushed to remote. I wish to present my code as it exists in my latest commit for pull request, as an initial, clean version, without my own, earlier commits. As the code stands right now, the git change history unnecessarily clutters the pull request.

I have done this:

  1. creation of of this feature branch
  2. earlier commit -- my own changes
  3. earlier commit -- my own changes
  4. earlier commit -- my own changes
  5. latest commit -- in pull request I want to present this code as initial version

How do I remove the history from the intermediate commits 2 through 4, and so show the code as it exists in commit 5 as new code showing changes only from commit 1?

Should I simply remove the files from git and re-add them in a new push? or otherwise clean up commits 2 through 4, while keeping the code as it currently stands in no. 5?


Solution

  • Squash your commits. Run git rebase -i HEAD~4 (where 4 is the number of commits you want to squash), then replace "pick" with "squash" for all but the first.