3. Git Remotes

3.1. About Remotes

  1. Default remote: origin (cloned from)

  2. Any repository can be remote

  3. Can fetch and push to any remote

  4. Pull Requests are branches too

  5. .git/config section [remote "origin"] can have multiple urls

3.2. Using Remotes

usage: git remote [-v | --verbose]
   or: git remote add [-t <branch>] [-m <master>] [-f] [--tags | --no-tags] [--mirror=<fetch|push>] <name> <url>
   or: git remote rename <old> <new>
   or: git remote remove <name>
   or: git remote set-head <name> (-a | --auto | -d | --delete | <branch>)
   or: git remote [-v | --verbose] show [-n] <name>
   or: git remote prune [-n | --dry-run] <name>
   or: git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...]
   or: git remote set-branches [--add] <name> <branch>...
   or: git remote get-url [--push] [--all] <name>
   or: git remote set-url [--push] <name> <newurl> [<oldurl>]
   or: git remote set-url --add <name> <newurl>
   or: git remote set-url --delete <name> <url>

    -v, --verbose         be verbose; must be placed before a subcommand
$ git remote

$ git remote -v
origin      https://github.com/AstroMatt/book-devops.git (fetch)
origin      https://github.com/AstroMatt/book-devops.git (push)

$ ls .git/refs/remotes

$ git ls-remote
From https://github.com/AstroMatt/book-devops.git
8192e9663597d1a58bb89d09b882372763395175    HEAD
8192e9663597d1a58bb89d09b882372763395175    refs/heads/master
328c3ea571acda37a2d0f4673ab8af44ee0371d8    refs/pull/1/head
535067e2c1b261e4ef1f5ae0d1c579fab7822bba    refs/pull/2/head
ad0d7c95d9a14cdc5eac36d3b48bfcd76fbe8fd9    refs/pull/3/head

3.3. Updating / Fetching

$ git remote update --prune
Fetching origin
$ git fetch
Fetching origin

3.4. Pull

$ git fetch
$ git merge


$ git fetch
$ git rebase

3.5. Push

3.5.1. Push to the default

$ git push

3.5.2. Push all branches

$ git push --all

3.5.3. Push to specific remote

$ git push origin
$ git push origin master

3.5.4. Push to other name in the remote

$ git push origin master:other

3.5.5. Delete branch in the remote

$ git push origin :other

3.5.6. Push Tags

  • Not pushed by default

$ git push --tags