OK. Query.
Rebase or merge into current?
I personally never rebase. It always seems to have some problem. I’m surely there’s a place and time for rebasing but I’ve never seen it in action I guess.
Always merge when you’re not sure. Rebasing rewrites your commit history, and merging with the squash flag discards history. In either case, you will not have a real log of what happened during development.
Why do you want that? Because it allows you to go back in time and search. For example, you could be looking for the exact commit that created a specific issue using git bisect. Rebasing all the commits in a feature branch makes it impossible to be sure they will even work, since they represent snapshots that never existed.
I’ll never understand why people suggest you should default to rebasing. When prompted about why, it’s usually some story about how it went wrong and it was just easier to do it the wrong way.
I’m not saying never squash or rebase. It depends on the situation but if you had to pick a default, it should be to simply merge.
Merge commits suck.
My biggest issue with GitHub is that it always squashes and merges. It’s really annoying as it not only takes away from commit history, but it also puts the fork out of sync with the main branch, and I’ll often realize this after having implemented another features, forcing me end up cherry picking just to fix it. Luckily LazyGit makes this process pretty painless, but still.
Seriously people, use FF-merge where you can.
Then again, if my feature branch has simply gone behind upstream, I usually pull and rebase. If you’ve got good commits, it’s a really simple process and saves me a lot of future headaches.
There’s obviously places not to use rebase(like when multiple people are working on a branch), but I consider it good practice to always rebase before merge. This way, we can always just FF-merge and avoid screwing with the Git history. We do this at my company and honestly, as long as you follow good practices, it should never really get too out of hand.
Merge commits suck.
My biggest issue with GitHub is that it always squashes and merges.
You are aware you’re talking about two different pieces of software?
Yeah, I am. However GitHub, being the biggest Git hosting provider and all that, makes you use merge commits. FF-merges must be done manually from the command line. While this definitely isn’t a problem for me, many people out there just don’t care and merge without a second thought (which, as I said in my comment, results in having to create a new branch and cherry picking the commits onto there).
You should check out the repo options on GitHub. It most definitely supports rebase merges, and you can disable other merge types if desired.
Sounds like I just gotta get better with rebasing. But generally I do my merges clean from local changes. I’ll commit and push, then merge in, push. Then keep working. Not too hard to track but I’ve found it’s the diff at MR time that people really pay attention to. So individual commits haven’t been too crucial.
I’ve always merged. Rebase simplifies the history graph, but realistically I can’t think of a time where that has been important to me, or any of the teams I’ve worked with.
Maybe on some projects with a huge number of concurrent branches it becomes more important, probably less so for smaller teams.
I try to structure my commits in a way that minimizes their blast radius, which usually likes trying to reduce the number of files In touch per commit.
For example, my commit history would look like this:
- Add new method to service class
- Use new service class method in worker
And then as I continue working, all changes will be
git commit --fixup
ed to one of those two commit’s hashes depending on where they occur.And when it’s time to rebase in full, I can do a
git rebase master --interactive --autosquash
.This is the way! Small commits with descriptive commit names, then just fixup into a few feature commits. Makes rebase a breeze.
What you do is create a third branch off master, cherry pick the commits from the feature branch, and merge in the third branch. So much easier.
That’s called rebasing
That is absolutely not what rebasing does. Rebasing rewrites the commit history, cherry picking commits then doing a normal merge does not rewrite any history.
I’m sorry but that’s incorrect. “Rewriting the commit history” is not possible in git, since commits are immutable. What rebase actually does is reapply each commit between upstream and head on top of upstream, and then reset the current branch to the last commit applied (This is by default, assuming no interactive rebase and other advanced uses). But don’t take my word for it, just read the manual. https://git-scm.com/docs/git-rebase
“Reapply” is rewriting it on the other branch. The branch you are rebasing to now has a one or multiple commits that do not represent real history. Only the very last commit on the branch is actually what the user rebasing has on their computer.
Cherry picking also rewrites the commits. This is equivalent to rebasing:
git branch -f orig_head git reset target git cherry-pick ..orig_head
for some reason it’s easier than normal rebasing though
Have you tried interactive rebase (rebase -i)? I find it very useful
Yeah, but then you deal with merge conflicts
You can get merge conflicts in cherry picks too, it’s the same process.
rerere is a lifesaver here.
(I’m also a fan of rebasing; but I also like to land commits that perform a logical and separable chunk of work, because I like history to have decent narrative flow.)
If your cherry-pick doesn’t run into conflicts why would your merge? You don’t need to merge to master until you’re done but you should merge from master to your feature branch regularly to keep it updated.
Git is weird sometimes.
I’ve definitely done this before…
I usually merge because I like to see commit history as it happened and because rebasing multiple commits with conflicts is more time-consuming than fixing it in one merge commit.
I do rebase smaller changes though to reduce merge commit clutter and like interactive rebase to clean up my local commit mess before pushing.
If the changes are small enough I just do
rebase -Xtheirs main
, and look to see if anything looks fucky (git diff main..
).I create a new branch locally with git switch --create, pull everything from main, sacrifice a small squirrel, and run the project to make sure everything still works.
If something doesn’t work or I can’t figure out how to resolve conflicts, I quietly switch back to my previous branch like nothing happened. That problem is for future me.
You can just undo the last commit with
git reset --soft HEAD~1
The little median in the middle is the dev who helps them fix their branch over screenshare
Curious question: how many people work on one repo/module/service at your workplace or organization?
I have been vomiting blood since day1 because of merge conflicts everywhere… I’m still trying to get better at git to make my life easier.
About 20 developers on one repo in my case, we use a rebase merge strategy with fairly small feature branches so I think that helps keep merge conflicts down. Another thing is our codebase is LARGE, so changes don’t often conflict.
I see… No one cares here and everyone just merges randomly. We have about 20 people working on one repo as well but a lot of times it’s the changes to the same file 😭