Up until now, I had only merged branches into one another but recently I discovered another option -- rebasing! Rebasing and merging are essentially doing the same thing so what exactly goes on behind the scenes for each?
Merge
Git merge combines multiple commits into one history. It will look at the two branches you want to combine and finds a common commit between them. Next, it takes the content of the feature or source branch and integrates it with the target (in our example, master) branch by creating an entirely new commit. This new commit holds the history of both of your branches. Because of that, you'll see all of the commits in chronological order.
Rebase
Rebasing is a bit different. Instead of merging the history of your two branches together from one base commit, you're actually making it seem like you created your branch from a different commit than you originally did. This allows you to keep a linear project history. This makes it a lot easier in the future to revert a change if you have a bug because you won't have to dig through to where the commit was made in the timeline.
So, which do you prefer? Rebase or merge? I'd love to hear in the comments below!
Be sure to follow me on Twitter for lots of tweets about tech, and if I'm being honest, lots of tweets about dogs too.
Top comments (29)
I'll quote Jesus himself on this. Both points are equally important
Totally agree. This post wasn’t meant to persuade people to use one or the other just explaining the differences since they are so similar!
You did a good thing. Honestly merge is abused and results in sloppy histories. I put
--ff-only
in mygitconfig
to prevent accidental merge bubbles.It's good for people to get more comfortable rebasing and pay attention to keeping the log clean--especially on larger teams
A final note--its best to
rebase -i
before pushingWhat does your final note do?
Rebase -i let's you squash and amend commits
It's always good to point out the consequences of got commands that rewrite history, so people are aware of it.
You forgot to mention the golden rule! If the working branch is public (someone besides you has a copy of it), please don't rebase!
Came here to say this. If you rebase a public (shared) branch, the other devs working on it are going to be very upset with you. You can rebase all you want while the branch is local, but the second you push it you have to go back to merging.
So if I fetch origin/master to my local origin/tired branch then I could rebase it. Work on the branch and when I'm done later in the day I can push to GitHub to origin/tired and make a PR to origin/master?
That’s correct!
I think the phrasing of this question could use some clarification.
A good general rule is not to rewrite history (any history) after it has been pushed to the shared repo.
Which means that when you are rebasing in your local repo, you’d only want to rebase your newer commits that haven’t been pushed yet.
When I was first starting out in development, one of my mentors told me that rebasing is rewriting git history. Whenever you rewrite history you must take extra precautions, It can be very easy to mess things up.
But it is a very important skill to have in order to GIT good :)
I might be miss something, but I always merge.
What is the point of having two tools doing almost the same thing? One of them is potentially dangerous and might screw the work if used in a public branch.
Rebase allows to have a clean linear history.
Well, I guess it's personal, but I don't mind merge commits. In our company we usually have max 3 developers working on the repo at the same time, so the git history looks well readable. But I suppose that for the project with 20 developers the git history with merge commits everywhere would look like a mess.
Clean commit messages +
amend
ing if necessary + merge - this is all I need.It's not about how it looks like. Rebasing allows backtracking to specific commits when hunting bugs. It makes git-bisect a very powerful tool for pin-pointing buggy commits. When you know which small commit introduced the bug, you are able to fix it more quickly. This is also why smaller commits are way better than large commits.
It's possible to backtrack to specific commits and git-bisect with
merge
as well.I used to rebase in my personal projects because I am controlling my code and git push —force isn’t a problem. At work, I prefer use to merge because I think few dangerous use that project that everybody is writing code.
Hi! I'm confused. I can create a feature branch, push it, and do rebase (thinking that is my branch) or I have to follow the golden rule in this case too?
Private/public, in this regard, is not about who can see the branch but about who is going to work on it.
If only you push to that feature branch, and others are only reading it to review your code, then it's safe to rebase it. Your peers will not lose their work due to your rebase because they don't have any work inside that branch (except from commits it shares with
master
, of course).If more developers other than you have pushed to that branch, you can still rebase - but you have to coordinate the rebase with them to make sure you don't lose any of their work. If there aren't that many of them, and if they only pushed to that branch one specific commit that you have personally requested from them (e.g. - you needed something that's out of your expertise), then it's easy. But if many developers are constantly pushing to that branch, coordinating a rebase becomes much harder, much riskier, and with much more damage potential - so it's best to avoid it and just merge.
Thanks! 😊
I personally prefer rebase but only on my local/feature branch before it gets merged to master.
I like to commit as often as I like but I only want one coherent commit message on the master branch.
Definitely! This is my preferred workflow as well.
I basically always rebase if I'm moving changes away from master, and I merge towards master. Unless I'm on a shared branch, then I always merge...
Same here!
I rebase on my feature branches through development and right before merging so in the history it looks like i branched off the latest master and merged. This makes it easy to see branches in isolation.
If your place of employment does not enforce some sort of git style rules, this will throw your tidiness out the window.
Great Article Kara!
Pretty much the same as everyone else. Rebase up until I push it into the public sphere, then I go to merging.
Thanks so much, Luke!