Skip to content

Instantly share code, notes, and snippets.

@vernko
Last active November 6, 2023 18:57
Show Gist options
  • Save vernko/aa74dfa288384db6d6fdaac92144dbdb to your computer and use it in GitHub Desktop.
Save vernko/aa74dfa288384db6d6fdaac92144dbdb to your computer and use it in GitHub Desktop.
Bring your feature branch up to date with the master branch

The point of this gist is to give you helpful info on some ways that you can update a feature branch you are working on to the current master/main, if you get behind master/main branch. The idea behind this is that when you try to merge your branch you have (hopefully) have less merge conflicts. The two ways I'll outline here are done via the following:

  • Fast Forward Merging
  • Rebasing

Using Fast Forward Merging

This is the way most devs learn how to merge the current master/main branch into your working branch.

First you need to make sure your local master/main branch is up to date. Go to your local project and check out the branch you want to merge into (your local master/main branch)

$ git checkout master

Fetch the remote, doing this will bring the current master/main branch and their commits from the remote repository. You can use the -p, --prune option to delete any remote-tracking references that no longer exist in the remote. Commits to master will be stored in a local branch, remotes/origin/master

$ git pull

This will do a git fetch and a git merge. Merge the changes from origin/master into your local master branch. This brings your master branch in sync with the remote repository without losing your local changes. If your local branch didn't have any unique commits, Git will instead perform a "fast-forward".

$ git checkout <feature-branch>

Merge your (now updated) master branch into your feature branch to update it with the latest changes from your team.

$ git merge master

Depending on your git configuration this may open vim. Enter a commit message, save, and quit vim:

Press a or i to enter insert mode and append the text following the current cursor position.
Press the esckey to exit insert mode and enter command mode.
Type :wq or :x to write the file to disk and quit.
This only updates your local feature branch. To update it on GitHub, push your changes.

$ git push origin <feature-branch>.

Using Rebasing (Coming Soon)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment