DEV Community

Discussion on: Git tips for trunk-based development

Collapse
 
alexisfinn profile image
AlexisFinn • Edited

I kindof use this at work:

  • branch
  • commit + push
  • commit --amend + push --force (to try and keep one commit)
  • rebase --interactive HEAD~x|sha1 to squash commits into one (this shouldn't happen because of the commit --amend, but well it does)
  • rebase master

---> go on to Merge Request.

The main problem with this approach is that while it does keep everything pretty clean, it messes up the history, crippling commands such as:

  • git blame: author can't be trusted because of squashing commits
  • git bisect: still works but since commits tend to become huge, it doesn't help much
  • git revert: same problem, commits tend to become huge so reverting a whole commit is usually out of the question

It's also pretty dangerous because when doing a push --force, if you don't pay attention at some point... well you're pretty much F'd (actually you can go explore the reflog, but that shouldn't happen)

So whereas it does make things more streamlined and easier to manage cherry-picking features into a production branch, it seriously diminishes the capabilities and reliability of the versioning system.

So i'm not saying its a bad process, I actually kind of like it, but boy does it get stressfull when the new junior dev/intern starts working because there's the potential to mess things up real bad real quick.

Collapse
 
itayronen profile image
Itay Ronen

The author did not recommend to squash all your commits into one.
I recommend you to do use commits to split your work and make PR easier and history cleaner.
Use rebase to edit your commit history and apply it on top of master.
Then when you go to PR, the reviewer will see your (edited) commits, on top of master.

Collapse
 
tonivdv profile image
Toni Van de Voorde

If you are using a cloud solution like github/bitbucket you can protect your branches to avoid messing it up.

About the history being messed up, well this usually happens when working on long living branches. Here the idea is that you don't have that, so there shouldn't be different authors working on a branch. And even if that is the case, it's not mandatory to squash to merge back to master once it is done. You can even keep the full history if that is what you prefer.

We tend to not squash doing PR reviews ... we only squash when putting everything back in master. And that is only in the case of small short lived branches. In very rare cases we didn't squash.

Collapse
 
alexisfinn profile image
AlexisFinn

I get that the method I'm currently using (abiding by?) is not exactly the same thing, and I actually really like rebasing and commit --amending or squashing. I think it makes for a clean, easier to manage repository.

That being said I just thought I'd point out some of the potential drawbacks to going this route which has a lot to do with User error.

I've met a lot of devs who had a hard time visualizing what was happening with Git, either because they're used to SVN or just because it's not an easy tool to apprehend.

I've also met a good number of project managers / client demands that really don't lend very well to small iterative branches and commits, I have often ended up working several weeks on a single feature.

So whereas I truly like this approach, it should be carefully thought over if your team is not very proficient with git or your manager prefers long all-encompasing features and branches over small ones.

But of course you're right that there are many ways in which you can safegard against dramatic problems or event circumvent them entirely through a well thought out branching tree, and that is one of the beauties of Git: It can pretty much do it all.