Good post! I'd like to share an additional perspective when it comes to rebasing.
If you started some work a month ago...
In this case and many others I often do the following:
Another way to do this:
These are simple ways to copy your commit history without dealing with some of the challenges that can come with collaborative rebases.
Probably the most common time I use this workflow is the following:
Developer A is working on feature A and opens a merge request.
Developer B needs code from feature A, and is in a dilemma of how to begin. Do they copy the code they need from feature A? Do they branch off feature A branch?
I recommend Developer B branches off Feature A branch. They can do their work and commit along the way.
When Feature A merges (probably a squash merge) the real problem enters. Developer B now has a bunch of commits from feature A in the Feature B branch that no longer exist in main due to the squash.
This can really challenge peoples git knowledge... but it's quite simple really. It's as easy as 1, 2, 3.
Again, I think if there are many commits you are better off doing an interactive rebase to squash the feature B commits into a single commit (as long as the history isn't important.). Or the lazy way, `git reset --soft HEAD~<commit count> (or alternatively, git reset --soft <SHA of the original HEAD commit of the main branch>). Then you create a new commit, copy the SHA, and cherry pick that onto your new branch.
In general I pretty strongly discourage force pushes on my teams to branches that have been pushed up to a remote. Squash and merge being enabled means that the commit cleanliness in main is preserved, and the commits in a branch can be as messy as they need to be (reality in development is rarely beautiful... why rewrite history anyways?) Very rarely are perfectly curated commit histories worth the time for a developer. I'd rather see beautiful code and a nice squash merge than a perfect few commit messages in a PR history. If you follow that line, then it's fine to have some merge commits in there anyways.
All in all, a great article. Just thought I'd share my thoughts on this as well. I stumbled here from the gitlab CI monorepo post. It will be a useful starting point for me tomorrow :).
What do you think Marcin Wosinek?
Marcin Wosinek
JavaScript developer