When I am working on a feature branch that take couple of weeks to finish, I don’t want to diverge too much from the upstream. To keep my branch up to date with the upstream, I do a rebase with the upstream once every few days. Following is my workflow.
- Fetch upstream
git fetch upstream
- Rebase feature branch with upstream
git rebase upstream/master
While working on feature branch, I would have made several small commits into the feature branch. Before creating a pull request to merge the changes into upstream, it is a good practice to squash all the small commits into one commit. Following is my workflow to squash the commits.
- Find the common parent between feature branch and upstream branch
git merge-base HEAD upstream/master
- Squash commits using interactive rebase with the commit hash in step 1.
git rebase -i <commit hash from step 1>
- When editor is opened to select which commit to keep and which to squash, change all but first commit from pick to squash (or s for shortcut). Save and quit the editor.
- Editor is opened again for the squashed commit. Either keep it the messages as it is (not a good practice) OR delete existing commit messages, add a new meaningful commit message. Save and quit the editor.