Zooming Out for the Big Picture
Zooming Out for the Big Picture κ΄λ ¨
In the beginning of this guide, I started by mentioning the similarity between git merge
and git rebase
: both are used to integrate changes introduced in different histories.
But, as you now know, they are very different in how they operate. While merging results in a diverged history, rebasing results in a linear history. Conflicts are possible in both cases. And there is one more column described in the table above that requires some close attention.
Now that you know what "Git rebase" is, and how to use interactive rebase or rebase --onto
, as I hope you agree, git rebase
is a super powerful tool. Yet, it has one huge drawback when compared with merging.
Git rebase changes the history.
This means that you should not rebase commits that exist outside your local copy of the repository, and that other people may have based their commits on.
In other words, if the only commits in question are those you created locally β go ahead, use rebase, go wild.
But if the commits have been pushed, this can lead to a huge problem β as someone else may rely on these commits, that you later overwrite, and then you and they will have different versions of the repository.
This is unlike merge
which, as we have seen, does not modify history.
For example, consider the last case where we rebased and resulted in this history:
Now, assume that I have already pushed this branch to the remote. And after I had pushed the branch, another developer pulled it and branched out from "Commit C". The other developer didn't know that meanwhile, I was locally rebasing my branch, and would later push it again.
This results in an inconsistency: the other developer works from a commit that is no longer available on my copy of the repository.
I will not elaborate on what exactly this causes in this guide, as my main message is that you should definitely avoid such cases. If you're interested in what would actually happen, I'll leave a link to a useful resource below. For now, let's summarize what we have covered.