Sourcetree How to Merge Branches Again
Merge or Rebase?
As you're no doubt aware, Git and Mercurial are swell at re-integrating divergent lines of development through merging. They have to be, since their design strongly encourages developers to commit changes in parallel in their own distributed environments. Eventually some or all of these commits have to be brought together into a shared graph, and merging and rebasing are two primary ways that let us do that. And then which one do you apply?
What does Merge or Rebase mean?
Let's kickoff by defining what merging and rebasing are.
Merging brings two lines of evolution together while preserving the ancestry of each commit history.
In contrast, rebasing unifies the lines of development by re-writing changes from the source branch so that they appear as children of the destination branch – effectively pretending that those commits were written on top of the destination branch all along.
Here'due south a visual comparing between merging and rebasing a branch 'feature/awesomestuff' back to the master co-operative (click for full size):
So merging keeps the separate lines of development explicitly, while rebasing always ends up with a single linear path of evolution for both branches. But this rebase requires the commits on the source branch to bere-written, which changes their content and their SHAs. This has important ramifications which we'll talk nearly below.
[An aside: merging in Git tin can sometimes result in a special instance: the 'fast forward merge'. This just applies if there are no commits in the destination branch which aren't already in the source branch. Fast-forwards merges create no merge commit and the result looks like a rebase, because the commits but move over to the destination co-operative – except no history re-writing is needed (we'll talk near this re-writing in a 2d). You can turn fast-forrad merges off in SourceTree so that a merge commit is always created if you want – cheque the 'Create a commit' selection in the Merge dialog or prepare information technology globally in Preferences > Git.]
And then, what are the pros and cons of merging and rebasing?
Pros and Cons
Merging Pros
- Elementary to use and understand.
- Maintains the original context of the source branch.
- The commits on the source branch remain separate from other branch commits, provided y'all don't perform a fast-forward merge. This separation can be useful in the example of feature branches, where y'all might desire to accept a feature and merge it into some other branch later.
- Existing commits on the source co-operative are unchanged and remain valid; information technology doesn't matter if they've been shared with others.
Merging Cons
- If the need to merge arises but because multiple people are working on the same branch in parallel, the merges don't serve any useful historic purpose and create clutter.
Rebase Pros
- Simplifies your history.
- Is the nearly intuitive and clutter-free way to combine commits from multiple developers in a shared branch
Rebase Cons
- Slightly more complex, especially under conflict conditions. Each commit is rebased in gild, and a conflict will interrupt the process of rebasing multiple commits. With a disharmonize, you have to resolve the conflict in order to continue the rebase. SourceTree guides y'all through this process, only it can still get a bit more than complicated.
- Rewriting of history has ramifications if you've previously pushed those commits elsewhere. In Mercurial, you simply cannot push commits that you later on intend to rebase, because anyone pulling from the remote will become them. In Git, you lot may push button commits you lot may want to rebase later (as a backup) butonly if information technology'south to a remote branch that onlyyou apply. If anyone else checks out that co-operative and you after rebase information technology, it's going to go very confusing.
Practical tips
Looking at the above pros/cons, it's clear that it's mostly non a case of choosing between i or the other, simply more a case of using each at the appropriate times.
To explore this farther, let'south say you piece of work in a development team with many committers, and that your squad uses both shared branches as well as personal characteristic branches.
Shared branches
With shared branches, several people commit to the same co-operative, and they find that when pushing, they get an error indicating that someone else has pushed outset. In this case, I would always recommend the 'Pull with rebase' approach. In other words, you'll exist pulling down other people'south changes and immediately rebasing your commits on top of these latest changes, allowing y'all to button the combined result back equally a linear history. It'south important to notation that your commits must not take been shared with others yet.
In SourceTree, y'all tin can do this in the Pull dialog:
You tin also set this every bit the default beliefs in your Preferences if you like:
Past taking this approach, when developing in parallel on a shared branch, you and your colleagues can notwithstanding create a linear history, which is much simpler to read than if each member merges whenever some commits are congenital in parallel.
The merely time yous shouldn't rebase and should merge instead is if you've shared your outstanding commits already with someone else via another mechanism, due east.thou. you've pushed them to some other public repository, or submitted a patch or pull asking somewhere.
Characteristic branches
Now let's take the example where you lot deliberately create a separate branch for a feature you're developing, and for the sake of this example, you are the only person working on that characteristic branch. This arroyo is common with git-flow and hg-menses for example. This feature branch may take a while to complete, and you'll only want to re-integrate it into other lines of evolution in one case you're done. So how exercise you manage that? In that location are actually 2 carve up bug here that we must accost.
The last merge: When building a feature on a split up branch, you lot're commonly going to want to go along these commits together in order to illustrate that they are part of a cohesive line of development. Retaining this context allows you to identify the feature development easily, and potentially use it as a unit later, such as merging it once again into a different branch, submitting information technology every bit a pull request to a dissimilar repository, and so on. Therefore, you're going to want to merge rather than rebase when you lot complete your final re-integration, since merging gives y'all a single divers integration point for that feature branch and allows easy identification of the commits that it comprised.
Keeping the feature branch up to date: While you're developing your characteristic branch, you may desire to periodically keep it in sync with the branch which information technology will eventually be merged back into. For example, y'all may desire to examination that your new feature remains compatible with the evolving codebase well earlier you perform that final merge. There are ii means you tin bring your feature co-operative upwards to date:
- Periodically merge from the (future) destination co-operative into your feature branch. This approach used to cause headaches in former systems like Subversion, simply actually works fine in Git and Mercurial.
- Periodically rebase your feature branch onto the current state of the destination co-operative
The pros and cons of each are generally like to those for merging and rebasing. Rebasing keeps things tidier, making your feature branch appear quite compact. If you use the merge approach instead, it ways that your feature branch will ever branch off from its original base commit, which might accept happened quite a long time ago. If your entire squad did this and there's a lot of activity, your commit history would contain a lot of parallel feature branches over a long period of fourth dimension. Rebasing continually compacts each characteristic branch into a smaller space by moving its base of operations commit to more recent history, cleaning up your commit graph.
The downside of rebasing your feature branches in order to continue them up to engagement is that this arroyo rewrites history. If you lot never push these branches outside your development car, this is no problem. Only bold that y'all do want to button them somewhere, say for backup or but visibility, and then rebasing can cause issues. On Mercurial, it's always a bad idea – you should never push button branches yous intend to rebase later. With git, you have a bit more flexibility. For case, y'all could button your feature branches to a different remote to keep them separate from the rest, or you lot could button your feature branches to your usual remote, as your development team is enlightened that these characteristic branches will likely be rewritten so they should non check them out from this remote.
Conclusion
The consensus that I meet most ofttimes is that both merge and rebase are worth using. The time to utilize either is entirely dependent on the state of affairs, the experience of your squad, and the specific DVCS you're using.
- When multiple developers work on a shared branch, pull & rebase your approachable commits to keep history cleaner (Git and Mercurial)
- To re-integrate a completed feature branch, employ merge (and opt-out of fast-frontwards commits in Git)
- To bring a characteristic branch up to date with its base branch:
- Prefer rebasing your characteristic co-operative onto the latest base of operations branch if:
- You oasis't pushed this branch anywhere yet, or
- You're using Git, and you lot know for sure that other people will not have checked out your feature branch
- Otherwise, merge the latest base changes into your feature branch
- Prefer rebasing your characteristic co-operative onto the latest base of operations branch if:
I promise this helps! Please let me know in the comments if you have any questions or suggestions.
Source: https://blog.sourcetreeapp.com/2012/08/21/merge-or-rebase/
0 Response to "Sourcetree How to Merge Branches Again"
Post a Comment