Skip to content

Instantly share code, notes, and snippets.

@sinewalker
Last active July 6, 2018 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sinewalker/3b07efab5791bf39c05b27f103b2d391 to your computer and use it in GitHub Desktop.
Save sinewalker/3b07efab5791bf39c05b27f103b2d391 to your computer and use it in GitHub Desktop.
Merge remote origin/master into a new local repository

This little dance is needed to merge in the remote origin/master to a fresh local (one created without cloning the remote, such as by a tool like pass). I don't know if there is a better way?

You need to do this when

  • you have another application which can use git for storing data, and you need to fill it with history from a common remote, but the application doesn't support git clone, only git init type operations
  • you have a local repository with local changes that you want to keep, but still merge in the origin (although you could go the other way, I think?)

Use commands like these to merge in a remote to your local master, as the new origin:

git remote add origin git@github.com:user/repo
git fetch origin
git checkout origin/master
git checkout -b temp
git checkout -B master temp
git branch -d temp
git branch --set-upstream-to=origin/master master

An explanation of what these commands do:

  • Add the remote, call it origin
  • fetch the data
  • switch to the origin/master branch just fetched
  • create a new branch temp from the current branch (origin/master) - merges all the data to temp
  • reset master from the temp branch - merges all the data to the local master branch
  • delete the temp branch
  • set upstream tracking for master as origin/master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment