GIT WORKFLOW PATTERNS
Below are git workflows I have used and thought were helpful
enough to preserve and share. I'm still a git newb, so these
may not always be best practices. If that scares you, come
back tomorrow and I may have some better practices for you.
Rebase and cherry pick to avoid unintuitive merges
------------------------------------------------------------
I am working on PR for a new feature for some project. A
contributor merged the development branch `dev' into my PR
branch so as to have the PR contain new work done on
`dev'. In my mind, this operation should have been performed
as a rebase, since the PR branch will eventually be merged
into `dev', and no conflicts needed to be fixed. The same
contributor also added a commit the PR, which contained some
new code. This is what I did to fix the history of this PR,
while preserving the new commit.
I fetched the PR, `origin/new-feature'. Then I checked out
the branch: `git checkout origin/new-feature'. Next, I
reviewed the log to see which commit I needed to cherry
pick: `git log' (I did this visually--there may be a more
exacting method). I identified the commits and made their
hashes available in a scratch pad.
My local branch tracking the PR did not include the
contributor's work. This meant I could use it as a base for
the next set of procedures, reapplying the latest commits on
`dev' and integrating the contributor's work. So I switched
to a throwaway branch, `git switch -c _new-feature'. Then I
rebased `dev': `git rebase dev'. Next, I cherry pick the
contributor's commits: `git cherry-pick <hash>' and `git
cherry-pick --continue'. Just to be sure I got everything, I
compare this branch with the PR: `git diff _new-feature
origin/new-feature'. Everything looks good so I rebase the
throwaway branch onto the local PR branch and then force
push it upwards: `git rebase _new-feature new-feature', `git
push --force origin'.