Commander Cody Commander Cody - 1 year ago 121
Git Question

Git shortcut for checkout, pull, checkout, merge/rebase

Let's say I've got 2 branches, master and codyDev. Before I begin making changes on codyDev, I do the following in order to make sure I'm starting from the latest master commit on codyDev:

git checkout master

git pull

git checkout codyDev

if changes,
git merge master

Is there a shortcut for the first 3 steps, so I don't have to leave my codyDev branch? I tend to find out that the master has not been updated and the checkout/pull/checkout was 3 unnecessary commands.

Note that I did Google to try to find an answer to my question, but there seems to be a wide range of potential and somewhat complicated solutions. The one that seemed most appealing was something to the affect of
git fetch origin master:master
, but the explanations I read were not super clear because the examples were a bit more complicated. I also found myself wondering "why a fetch instead of a pull" (i.e.
git pull origin master:master

Answer Source

You can update your codyDev branch with the latest changes from master without actually changing branches:

git checkout codyDev      # ignore if already on codyDev
git fetch origin          # updates tracking branches, including origin/master
git merge origin/master   # merge latest master into codyDev

The trick here is that to merge the latest master into codyDev you don't actually need to update your local master branch. Instead, you can just update the remote tracking branch origin/master (via git fetch) and then merge it instead.

You can also directly update the local master branch using git fetch without actually checking out the local master branch:

# from branch codyDev
git fetch origin master:master

However, this will only work if the merge is a fast-forward of the local master branch. If the merge be not a fast-forward, then this will not work, because a working directory is required in case merge conflicts arise.

Read here for more information.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download