Combine multiple Commits into a single one
The git log in the own branch can get quite messy sometimes. It’s a good idea to clean that up, before creating a Pull Request. The following workflow combines
Don’t use this approach on a branch with many developers working on it!
1.) Make sure working directory is clean:
- Commit or stash any changed files.
2.) Identify the number of commits (
git log --pretty=oneline --abbrev-commit
to list commits in short form:
a45e273 (HEAD -> <MYBRANCH>, <ORIGIN>) A commit message
e5bdaca Improved something
64a4cb1 Reverted
aeb755a Ups
c36e41a Everything better now
ae16a49 It should work now
[...]
- If I want to combine all commits including
aeb755a Ups
and newer I have to combinecommits, so usually
3.) Rebase:
git rebase -i HEAD~N
withN
being the number of commits () to combine. - Replace the leading
pick
by as
(squash) for all commits instead the latest.
pick a45e273 A commit message
s Improved something
s 64a4cb1 Reverted
s aeb755a Ups
[...]
- Save and exit.
- A new editor opens, in which we can adjust the new commit message (a list of all old messages is proposed as default).
- Save and exit.
4.) Force push:
git push -f
to bring the changes to remote. It will rewrite git history, that’s why it should only be done on a branch where you are working alone.