Browse topics

git remote

Version
2.51
Review
Draft

Summary#

git remote manages the names and URLs of other repositories. Renaming a remote also updates its configuration and remote-tracking branches. A remote can use a push URL that differs from its fetch URL.

Syntax#

sh
git remote -v
git remote add upstream URL
git remote rename origin upstream
git remote get-url --push origin
git remote set-url origin URL
git remote set-url --push origin URL
git remote remove upstream

Example#

sh
(
  set -eu
  scratch=$(mktemp -d "${TMPDIR:-/tmp}/devlookup-git.XXXXXX")
  trap 'rm -rf -- "$scratch"' EXIT
  unset GIT_CONFIG_GLOBAL $(git rev-parse --local-env-vars)
  export GIT_CONFIG_NOSYSTEM=1 GIT_TEMPLATE_DIR= LC_ALL=C
  export HOME="$scratch" XDG_CONFIG_HOME="$scratch" GIT_ATTR_NOSYSTEM=1
  export GIT_PAGER=cat GIT_EDITOR=true
  git init -q -b trunk "$scratch/work"
  cd "$scratch/work"

  # Editing remotes is local work, so these paths do not need to exist.
  git remote add origin "$scratch/old.git"
  git remote rename origin upstream
  git remote set-url upstream "$scratch/new.git"
  git remote add origin "$scratch/fork.git"
  git remote
  basename "$(git remote get-url upstream)"
  git config --get remote.upstream.fetch

  git remote set-url --push upstream "$scratch/new.git"
  git remote set-url upstream "$scratch/moved.git"
  echo "fetch: $(basename "$(git remote get-url upstream)")"
  echo "push: $(basename "$(git remote get-url --push upstream)")"
  git remote remove origin
  git remote
)

Expected output#

Text
origin
upstream
new.git
+refs/heads/*:refs/remotes/upstream/*
fetch: moved.git
push: new.git
upstream

Common mistakes#

If a push URL is configured, set-url without --push changes only the fetch URL and pushes keep going to the old address. Check both with git remote -v. Both URLs should refer to the same repository; use two remotes for separate places. git remote add does not contact the URL unless you pass -f.

Version applicability#

Git 2.7 added remote get-url. Git 1.7.12.2 added the remove spelling as an alias for rm.

The complete example requires Git 2.28 or later for git init -b.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0