Search code examples
gitversion-controlgit-mergegit-workflowfast-forward

git workflow for merging using fast-forward commits


I use git on several projects with many developers. My usual work flow is to branch locally for a specific feature/request, merge my changes to a local branch tracked upstream as a fast forward merge, and push changes. I always branch for even a single commit. I don't ever commit locally on a branch I share with others so I'm free to rebase/muck about locally until I push my changes upstream. As a matter of preference I like my merges to be fast forward commits whenever possible. As an example if all for version 1 of a project is pushed by all developers on the origin/v1 branch I'd:

git checkout v1
git checkout -b feature-A
#Do work on feature-A branch.  Test, rebase --interactive to clean up

Eventually I'm at the point where I want to merge my changes to v1 locally and push to origin/v1. I'll do a git fetch origin with feature-A checked out. If changes are made I'll checkout v1 and merge

git fetch origin
#If New changes are present checkout v1 and merge
git checkout v1
git merge origin/v1 #I could pull but I prefer to fetch, log origin/v1, and then merge

Now to achieve the fast forward merge for feature-A I checkout feature-A, rebase to v1, checkout v1, merge feature-A, and push v1 back to origin.

git checkout feature-A
git rebase v1
git checkout v1
git merge --ff-only feature-A
git push origin v1

There is nothing wrong with non-fast forward commits and nothing wrong with a merge commit. Again, it's just a matter of preference. I'm wondering if there is a better workflow to accomplish the same thing without going through all the checkouts of branches. There might be a git command I'm unaware of that would work after I rebase feature-A on top of the updated v1 branch.

https://stackoverflow.com/a/4157106/620304 looks like it could help. A possible workflow would be to update v1 with the latest changes from origin and update v1 to the HEAD of feature-A locally after I rebase:

#Starting from feature-A currently checked out
git fetch origin
#New changes are present
git checkout v1
git merge origin/v1
git checkout feature-A
#In I didn't fetch anything from origin's v1 branch I can skip the commands between the comments
git rebase v1
git branch -f v1 feature-A
git push origin v1

I'm sure that there might be an even better way. Any help/input is much appreciated.


Solution

  • A workflow that uses only fast-forward merges is essentially a rebase workflow. The general rebase workflow works very similar to the example you gave, except for git rebase origin/v1 instead of merging.

    The most referenced article I've seen is Randy Fay's blog post: A Rebase Workflow for Git.

    Be sure to read the follow-up article, linked at the start of this one, for some short-cuts to make it easy to use.