Tutorial Level: Medium
This git tutorial is intended for readers who already have experience with a version control system are looking for best practices to administer branching and merging on a team project.  It assumes the user has understanding of git commands from previous tutorial.

For official documentation, please reference:

Git Remote Workflows


Lesson 1: Branching Basics

  1. Local branches TRACK remote branches.
  2. Branches each have a HEAD, which is a reference to the version.
  3. FETCH will update the remote-tracking so it points to the ‘current’ Head.
  4. MERGE will write a source (local or remote) to a destination (branch that is currently checked out).  This can fail and create “merge conflicts” which must be resolved.
  5. PULL updates your local code to the current remote.  This essentially is a Fetch then Merge.
  6. Use CHECKOUT to switch between local branches and create local copies of remote branches.

Lesson 2: Strategy

Track multiple remote branches: master (remote), develop (remote), feature/<feature>(remote).

Complete work on local feature branches.  Ideally, teammates all work on separate feature branches to reduce merge conflicts.

When ready to push feature to develop:

  1. Commit any unsaved changes.
  2. Pull all updates.
  3. Checkout Develop.
  4. Merge Feature (to Develop).
  5. Resolve any conflicts locally, then commit, push to Develop (remote).

This strategy is useful for isolating conflicts locally and eliminating blockers.  Team lead should be responsible for merging specific hotfix branches and Develop branch into Master.

Pro Tip 1: When utilizing multiple feature branches and especially with a rapid development team, it is strongly recommended that you update your local feature branch with Develop branch regularly.  This will help you minimize snowballing merge conflicts. To do this, a git pull won’t suffice because it only pulls changes from the remote-branch that you are tracking, aka the Feature (remote) branch.  Therefore, to update your local feature branch with Develop:

  1. Commit any unsaved changes.
  2. Pull all updates.
  3. Checkout Feature.
  4. Merge Develop (to Feature).
  5. Resolve any conflicts locally, then commit, push to Feature (remote).

Pro Tip 2:  Just because you created a mess, doesn’t mean you need to fix it.  If you find yourself struggling to resolve merge conflicts and would like to completely start over, you can by doing a git reset hard.

Lesson 3: Commands

  1. View

    List local branches

    git branch

    List remote branches

    git branch -r

    List all branches

    git branch -a
  2. Fetch

    Fetch all branches (Updates remote-tracking)

    git fetch -a
  3. Checkout

    Checkout new local copy of remote branch

    git checkout -b <localBranchName> <remote/branchName>

    Checkout local branch

    git checkout <myLocalBranch>
  4. Track

    Update which remote a branch is tracking

    git branch <branchName> --set-upstream-to <remote/branchName>
  5. Pull

    Update all  (Warning:  May result in merge conflict)

    git pull -a
  6. Merge

    Merge remote branch to local

    git merge <remote/branchName>

    Merge local to local

    git merge <branchName>
  7. Reset

    Lose all unsaved changes and go back to last HEAD

    git reset --hard HEAD

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>