I'd like to present the no pull strategy and if you use Pull/Merge Requests to merge into shared branches, then you don't need to have a local copy of dev
at all!
Create a new branch:
git fetch
git switch -c my-branch origin/dev --no-track
Periodically update your branch:
# checkout your branch if not already
git fetch
git rebase origin/dev
Merging branches into dev:
When branches are ready to be PR'd/MR'd into dev
, assuming they will be up to date (perhaps because developers are rebasing their branches onto the latest origin/dev
to keep them up to date), then whether to allow fast-forward merges or force a merge commit is a matter of preference. If the merges only have a single commit there is less to gain by forcing the merge commit. If they have multiple commits my preference is to force a merge-commit (git merge --no-ff
). To summarize some of the advantages of using --no-ff
from another answer:
The merge (with --no-ff) forces a merge commit, and this is helpful because each PR contains the list of commits associated with just that PR, enabling you to view the first-parent history which shows all merges into the branch, and easily compare them. Another benefit of forcing the merge commit is that it's easy to revert an entire PR by simply reverting the merge commit, rather than individually reverting every commit that was in the original PR.
The only con of using --no-ff
is that if you don't care about any of the pros of using it, then you are needlessly adding extra merge commits.
Note: when merging dev
into main
I would recommend always using --no-ff
, just so you can use git log --first-parent
to see the differences between releases to production.
Why not keep a local copy of dev
?
The reasons I don't like to have a local copy of dev
are:
- It is almost always out of date, and you might accidentally use an outdated version of it.
- You don't have to waste time pulling to keep it up to date.
- You don't ever actually need it because you can always use
origin/dev
.
- If you don't keep local copies of shared branches, you never have to
git pull
.
The downside of using the no-pull strategy, is conceptually it takes a little time to get used to it.
Caveat:
If you don't use Pull/Merge Requests to merge into dev
, then I would add this step for merging into dev
:
git switch dev
git fetch
git reset --hard @{u}
git merge my-branch --no-ff
git push