--squash
is the simplest way to do it, as Davin wrote.
If you still want to keep some granularity to your branch (ie the branch shows too many small commits, but you don't want to put everything into one single commit that would be too large to grasp), you can try an interactive rebase.
git rebase
lets you choose which commits to squash together, and which commits you want separated from the others. It lets you even split a single commit into several ones.
-i
--interactive
Make a list of the commits which are about to be rebased. Let the user edit that list before rebasing. This mode can
also be used to split commits (see SPLITTING COMMITS below).
If you actually don't mind having the granularity, but would like to have it visually separated from the main branch, you might be interested in the merge
option --no-ff
, see here.
This would only have effect if nothing happened in your main branch since you created the branch Test
(commit A
below). Starting example:
A - MainBranch
\
B---C---D - Test
After git checkout MainBranch && git merge Test
, you have a linear history line such as:
A---B---C---D - Test - MainBranch
The history of MainBranch
is not very clean or easy to follow.
Instead, you can run git merge --no-ff Test
, and keep the branch separated:
A-----------M - MainBranch
\ /
B---C---D - Test
, M being a merge commit.
This is my usual workflow.