0

Is there a simple way to sync current branch with remote master? I use next commands to do that (from local branch develop):

  1. git checkout master
  2. git pull
  3. git checkout develop
  4. git merge master
aynber
  • 22,380
  • 8
  • 50
  • 63
  • 1
    Possible duplicate of [A single command to git pull a branch?](https://stackoverflow.com/questions/41733132/a-single-command-to-git-pull-a-branch) – phd Aug 17 '18 at 13:04
  • 1
    https://stackoverflow.com/a/17722977/7976758 – phd Aug 17 '18 at 13:05
  • @phd Ouch, didn't see this one before answering. Thank you for the link. – Romain Valeri Aug 17 '18 at 13:24
  • Not a problem. SO loves duplicates. :-) – phd Aug 17 '18 at 13:25
  • Possible duplicate of [Merge, update, and pull Git branches without using checkouts](https://stackoverflow.com/questions/3216360/merge-update-and-pull-git-branches-without-using-checkouts) – LarsH Aug 17 '18 at 13:37
  • What are you trying to achieve? Based on the commands you used, it seems you want to merge latest `master` branch into `develop` branch. If so, it's the right way. Or you can merge `origin/master` branch into `develop` directly: `git fetch`, `git checkout develop` and `git merge origin/master`. – Marina Liu Aug 18 '18 at 03:57

2 Answers2

0

Your process does the job and doesn't seem that long to type, but you could always speed up the process by setting an alias, like

git config --global alias.whatever '!git checkout master && git pull && git checkout develop && git merge master'

# then to use it just type
git whatever
Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
0

You don't actually need to checkout a branch to sync it*, so instead of the first checkout and pull you can just run git fetch origin master:master.

So now you just have a fetch and a merge, but wait if you read the first part of the git pull documentation you learn that a pull is just a fetch, then a merge.

So in reality all you have to do is a special git pull: git pull origin master:master.

It'll get changes from origin master and merge them into your current branch. master:master means to pull the changes from master down to your current branch, but to also update your own copy of master (the first master is what you are pulling, the second is to update your own master at the same time)

Main benefits of this way, other than being 1 command, is you avoid doing 2 checkouts which are slow. Downside is it will not work if there is any conflicts between between your master and origin's.

*If it is a fast forward update, that is you've done no work on it.

Ripp_
  • 2,298
  • 1
  • 9
  • 12
  • I admit this is a very good solution if you can ensure no conflicts will arise. Depending on the context, so, I guess. – Romain Valeri Aug 17 '18 at 14:20
  • In most git flow based work flows you can don't do work to your local msater and dev branches, so you are fine – Ripp_ Aug 17 '18 at 16:22
  • I grant you that. But let's also note that in a lot (most?) workplaces, things don't happen exactly like they should ;-) – Romain Valeri Aug 17 '18 at 18:59
  • 1
    If the pull isn't fast forward then it'll refuse to run so it's not as dangerous as it could be. Also the people incharge of the repos in those placed need large inflatable hammers. – Ripp_ Aug 18 '18 at 19:23