Git notes

Sync fork

To Sync your fork from the original repository is two steps:

  1. Setup the upstream remote (only have to do this one time)
    • check current config: git remote -v
    • add upstream remote: git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
  2. Sync fork
    • git fetch upstream
    • git checkout master
    • git merge upstream/master
    • You’re up-to-date locally, then update your fork, git push

Remote branches

It is a bit confusing that branches are only local by default. A branch that is connected to a remote repository (GitHub) is called a “tracking branch”. When you git clone, the master branch is automatically created as a “tracking branch” with the GitHub repository as the “upstream branch”. However, git knows about the other branches, but does not automatically create them locally. Furthermore, after you create a new branch locally, and you want to push it to GitHub, you will need to add a remote.

To get information about all your remotes, git remote show origin lists everything tracked locally and how it lines up with your remotes.

Use a remote “tracking branch” locally:

  • Clone the repo, git clone https://github.com/user/repository.git
  • Checking git branch will display only the local branches, i.e. *master if you just cloned it.
  • Check git branch -a to see all branches, including remotes, i.e. git knows about the other branches, it just didn’t create a local version of it yet. The remote branches will be named like remotes/origin/branch-name.
  • To create a local version of the remote branch, git checkout -b branch-name origin/branch-name. Now it is there, you can push and fetch/pull as normal. (note: there is a short cut version: git checkout --track origin/branch-name, or even shorter: if you simply git checkout branch-name when there is no local branch with that name and it exactly matches a remote branch git will automatically create a local “tracking branch”)

Create a branch locally, then add to remote:

  • Create and switch to a new branch, git checkout -b branch-name. (reminder: this is a short cut combo of two commands, git branch branch-name and git checkout branch-name)
  • To add the branch to the remote, git push origin branch-name will create the remote branch and set the “upstream” for the local version. (this is a shortcut to git branch --set-upstream-to origin branch-name plus git push)


Git in Classroom