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:
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.
To squash commits on a branch, you can use git rebase -i branchname~n
. while n
is the amount commits you want to start squash.
Such as if you need to squash commits on feature
branch(E
, F
, G
and H
as below graph) to a commit,
A---B---C---D develop
\
E---F---G---H feature
you can use git rebase -i feature~4
, then in the interactive windows, there will show the commits as:
pick E
pick F
pick G
pick H
You can need to change the last three commits F
, G
and H
to squash. input i
to insert, change them as:
pick E
squash F
squash G
squash H
Then enter Esc button and input :wq
to exit interactive window. And the commits history will like:
A---B---C---D develop
\
E' feature
Since you changed the history on branch, you should force push to remote by
git push -f