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.

  1. Fetch upstream
    git fetch upstream
    
  2. 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.

  1. Find the common parent between feature branch and upstream branch
    git merge-base HEAD upstream/master
    
  2. Squash commits using interactive rebase with the commit hash in step 1.
    git rebase -i <commit hash from step 1>
    
  3. 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.
  4. 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.
Advertisements