In the world of software development, managing changes and versions efficiently is critical, especially when multiple branches of development are involved.
Bringing Changes from Main Branch to Feature Branch
Let’s say we have created a new feature branch from main:
We add commits A
, B
, and C
on the feature branch:
At the same time, the main branch has commits D
and E
:
We can picture this as two branches of a tree growing in different directions:
Keeping the feature branch updated with the main branch is a crucial part of the Git workflow. We can do this with either Git merge or Git rebase.
Git Merge
Git merge pulls in the latest changes from main into the feature branch, creating a new merge commit in the process.
It’s like tying the two branches with a knot.
Git Rebase
Git rebase changes the base of the feature branch to the latest commit on the main branch, and then we place our changes from there.
It gives us a clean, straightforward history.
Bringing Changes from Feature Branch to Main Branch
Once we finish developing the feature, we need to bring it to the main branch.
We have a few options.
Git Merge
Git will create a new commit that ties together the history of the feature and main branches.
It’s like creating a knot on the rope, showing where the branches joined. But if we do this a lot, it ends up creating a messy history:
Git Rebase and Fast Forward Merge
We can use Git rebase to move the changes of our feature branch to the tip of the main branch, then perform a fast-forward merge.
It is like straightening a rope, as if it’s all in one line.
Squash Commit
The last option is squash commit. With squashing, all the feature branch commits are squeezed into a single commit and merged into the main branch.
This keeps the main branch’s history linear, like rebasing, while still creating a single merge commit. But remember that we lose fine details of individual feature commits in the main branch commit history.
With that said, squashing commits is a popular strategy on GitHub because it allows us to tidy up history in the main branch while still preserving the detailed commit history in the feature branch. It’s more like a hybrid approach.