Skip to main content
Chapter 3 of 61 · 14 sections · Page 26

Git: Working with Remotes

Add, inspect, rename, update, and prune remote repositories and upstream branch tracking.

Original chapter reference: Chapter 3: Working with Remotes. The command options and documentation recipes below are fully transcribed from Git Notes for Professionals.

Section 3.1: Deleting a Remote Branch

To delete a remote branch in Git:

Source example · PDF page 26
git push [remote-name] --delete [branch-name]

or

Source example · PDF page 26
git push [remote-name] :[branch-name]

Section 3.2: Changing Git Remote URL

Check existing remote

Source example · PDF page 26
git remote -v
# origin https://github.com/username/repo.git (fetch)
# origin https://github.com/usernam/repo.git (push)

Changing repository URL

Source example · PDF page 26
git remote set-url origin https://github.com/username/repo2.git
# Change the 'origin' remote's URL

Verify new remote URL

Source example · PDF page 26
git remote -v
# origin  https://github.com/username/repo2.git (fetch)
# origin  https://github.com/username/repo2.git (push)

Section 3.3: List Existing Remotes

List all the existing remotes associated with this repository:

Source example · PDF page 26
git remote

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

Source example · PDF page 26
git remote --verbose

or simply

Source example · PDF page 26
git remote -v

Section 3.4: Removing Local Copies of Deleted Remote Branches

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:

Source example · PDF page 27
git fetch [remote-name] --prune

To prune deleted branches from all remotes:

Source example · PDF page 27
git fetch --all --prune

Section 3.5: Updating from Upstream Repository

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

Source example · PDF page 27
git fetch remote-name
git merge remote-name/branch-name

The pull command combines a fetch and a merge.

Source example · PDF page 27
git pull

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

Source example · PDF page 27
git pull --rebase remote-name branch-name

Section 3.6: ls-remote

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:

Source example · PDF page 27
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 https://server.com/user/repo, and you have set git config

Source example · PDF page 27
url.ssh://git@server.com:.insteadOf https://server.com/:

git ls-remote --get-url <aremotename>
ssh://git@server.com:user/repo

Section 3.7: Adding a New Remote Repository

Source example · PDF page 27
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

Section 3.8: Set Upstream on a New Branch

You can create a new branch and switch to it using

Source example · PDF page 27
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

Source example · PDF page 28
git push --set-upstream origin AP-57

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

Section 3.9: Getting Started

Syntax for pushing to a remote branch

Source example · PDF page 28
git push <remote_name> <branch_name>

Example

Source example · PDF page 28
git push origin master

Section 3.10: Renaming a Remote

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

Source example · PDF page 28
git remote
# origin

Check existing remote with URL

Source example · PDF page 28
git remote -v
# origin https://github.com/username/repo.git (fetch)
# origin https://github.com/usernam/repo.git (push)

Rename remote

Source example · PDF page 28
 git remote rename origin destination
 # Change remote name from 'origin' to 'destination'

Verify new name

Source example · PDF page 28
git remote -v
# destination https://github.com/username/repo.git (fetch)
# destination https://github.com/usernam/repo.git (push)

=== Posible Errors ===

1. 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.

2. Remote [new name] already exists.

Error message is self explanatory.

Section 3.11: Show information about a Specific Remote

Output some information about a known remote: origin

Source example · PDF page 29
git remote show origin

Print just the remote's URL:

Source example · PDF page 29
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.

Source example · PDF page 29
git remote get-url origin

Section 3.12: Set the URL for a Specific Remote

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

Source example · PDF page 29
git remote set-url remote-name url

Section 3.13: Get the URL for a Specific Remote

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

Source example · PDF page 29
git remote get-url <name>

By default, this will be

Source example · PDF page 29
git remote get-url origin

Section 3.14: Changing a Remote Repository

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

Source example · PDF page 29
git remote set-url <remote_name> <remote_repository_url>

Example:

Source example · PDF page 29
git remote set-url heroku https://git.heroku.com/fictional-remote-repository.git
Buy me a coffee
Daily Practice
0/0