'shortcut for git merging master into current branch
So sometimes I'm working on a branch and I want to pull in the changes made on origin/master since branch created. Just doing git merge master is not usually adequate because local master may not have the changes from remote master, so I find myself having to do this:
# save any uncommitted changes (if there are any)
git stash
# update master first:
git checkout master
git pull
# back to where we were:
git checkout <previous branch>
git stash pop # omit if git stash not done
# and finally the actual merge:
git merge master
Surely there is a shorter way, just one or two git commands?
Solution 1:[1]
I face your scenario constantly and rapidly got tired of doing it more or less like you described, so I created an alias to fast-forward master without switching to it.
I have created an alias in the [alias] section of my ~/.gitconfig to update master (or any branch) to origin/master without checking it out.
Here is a simple (but unsafe) version of this alias:
ff = !sh -c 'git update-ref refs/heads/$1 origin/$1' -
You call it by typing git ff some_branch, and it uses update-ref to set that branch to origin/some_branch.
Once this alias is defined, your operation becomes:
git fetch
git ff master
git merge master
However, this is unsafe because it will do the update even if is not equivalent to a fast-forward merge, and therefore it might discard some commits. Here is the second version of this alias, with a safeguard so that it only does fast-forwarding, and refuses to do anything in other situations:
# Do a fast-forward merge on the branch specified as argument, as long
# as it's safe: $1 is not checked out, and a fast-forward merge is possible.
ff = !bash -c '\
if [[ `git symbolic-ref HEAD` = refs/heads/$1 ]] ";" then \
echo $1 is checked out, use pull or merge instead. ";" \
else \
git update-ref refs/heads/$1 origin/$1 `git merge-base origin/$1 $1` ";" \
fi' -
I often find the unsafe alias useful too, in particular when I do want to discard some work on my local copy of the branch, so I also kept it as ff-force:
# Not safe - reset the specified branch to its state on origin, even if it's not a fast-forward merge and potentially throws away some commits
ff-force = !sh -c 'git update-ref refs/heads/$1 origin/$1' -
Known limitation for both my aliases: the remote has to be called origin.
Solution 2:[2]
I do
git fetch origin develop- get latest changes but don't merge them to the localdevelop-git merge origin/develop- merge
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | tymtam |
