I like to commit often and for small, logical parts of code. If I'm working on a large feature (2+ weeks), there will be many commits such as:
- Paged menu controller working
- progress in XYZ (SAVING WORK AT END OF DAY so if laptop is lost, work is saved)
At the end of the feature, there are many commits and I fear that I'm polluting the commit history. I've considered Git squash, but it doesn't work once you pull in updates, or after you've pushed to your branch.
I'm considering just trying to have less-and-larger commits, and using squash locally on small commits before pushing to my fork feature branch. However, this would seem to hinder the detailed commit history that I like to have.
What is the best solution here?
Even you have pushed your branches to remote, you can still squash your local commits and then force push to remote.
1. Squash commits
To squash commits on a branch, you can use
git rebase -i branchname~n
. whilen
is the amount commits you want to start squash.Such as if you need to squash commits on
feature
branch(E
,F
,G
andH
as below graph) to a commit,you can use
git rebase -i feature~4
, then in the interactive windows, there will show the commits as:You can need to change the last three commits
F
,G
andH
to squash. inputi
to insert, change them as:Then enter Esc button and input
:wq
to exit interactive window. And the commits history will like:2. Push changes to remote
Since you changed the history on branch, you should force push to remote by