Git Working with Remotes

From WikiOD

Syntax[edit | edit source]

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

Deleting a Remote Branch[edit | edit source]

To delete a remote branch in Git:

git push [remote-name] --delete [branch-name]


git push [remote-name] :[branch-name]

Changing Git Remote URL[edit | edit source]

Check existing remote

git remote -v 
# origin (fetch)
# origin (push)

Changing repository URL

git remote set-url origin
# Change the 'origin' remote's URL

Verify new remote URL

git remote -v
# origin (fetch)
# origin (push)

List Existing Remotes[edit | edit source]

List all the existing remotes associated with this repository:

git remote

List all the existing remotes associated with this repository in detail including the fetch and push URLs:

git remote --verbose

or simply

git remote -v

Updating from Upstream Repository[edit | edit source]

Assuming you set the upstream (as in the "setting an upstream repository")

git fetch remote-name
git merge remote-name/branch-name

The pull command combines a fetch and a merge.

git pull

The pull with --rebase flag command combines a fetch and a rebase instead of merge.

git pull --rebase remote-name branch-name

ls-remote[edit | edit source]

git ls-remote is one unique command allowing you to query a remote repo without having to clone/fetch it first.

It will list refs/heads and refs/tags of said remote repo.

You will see sometimes refs/tags/v0.1.6 and refs/tags/v0.1.6^{}: the ^{} to list the dereferenced annotated tag (ie the commit that tag is pointing to)

Since git 2.8 (March 2016), you can avoid that double entry for a tag, and list directly those dereferenced tags with:

git ls-remote --ref

It can also help resolve the actual url used by a remote repo when you have "url.<base>.insteadOf" config setting.

If git remote --get-url <aremotename> returns, and you have set git config url.ssh://

git ls-remote --get-url <aremotename>

Removing Local Copies of Deleted Remote Branches[edit | edit source]

If a remote branch has been deleted, your local repository has to be told to prune the reference to it.

To prune deleted branches from a specific remote:

git fetch [remote-name] --prune

To prune deleted branches from all remotes:

git fetch --all --prune

Set Upstream on a New Branch[edit | edit source]

You can create a new branch and switch to it using

git checkout -b AP-57

After you use git checkout to create a new branch, you will need to set that upstream origin to push to using

git push --set-upstream origin AP-57

After that, you can use git push while you are on that branch.

Getting Started[edit | edit source]

Syntax for pushing to a remote branch[edit | edit source]

git push <remote_name> <branch_name>

Example[edit | edit source]

git push origin master

Adding a New Remote Repository[edit | edit source]

git remote add upstream git-repository-url

Adds remote git repository represented by git-repository-url as new remote named upstream to the git repository

Changing a Remote Repository[edit | edit source]

To change the URL of the repository you want your remote to point to, you can use the set-url option, like so:

git remote set-url <remote_name> <remote_repository_url>


git remote set-url heroku

Show information about a Specific Remote[edit | edit source]

Output some information about a known remote: origin

git remote show origin

Print just the remote's URL:

git config --get remote.origin.url

With 2.7+, it is also possible to do, which is arguably better than the above one that uses the config command.

git remote get-url origin

Renaming a Remote[edit | edit source]

To rename remote, use command git remote rename

The git remote rename command takes two arguments:

  • An existing remote name, for example : origin
  • A new name for the remote, for example : destination

Get existing remote name

git remote
# origin

Check existing remote with URL

git remote -v 
# origin (fetch)
# origin (push)

Rename remote

 git remote rename origin destination
 # Change remote name from 'origin' to 'destination'

Verify new name

git remote -v 
# destination (fetch)
# destination (push)

=== Posible Errors ===

Could not rename config section 'remote.[old name]' to 'remote.[new name]'

This error means that the remote you tried the old remote name (origin) doesn't exist.

Remote [new name] already exists.

Error message is self explanatory.

Set the URL for a Specific Remote[edit | edit source]

You can change the url of an existing remote by the command

git remote set-url remote-name url

Get the URL for a Specific Remote[edit | edit source]

You can obtain the url for an existing remote by using the command

git remote get-url <name>

By default, this will be

git remote get-url origin