Working with Remotes
Deleting a Remote Branch
Section titled “Deleting a Remote Branch”To delete a remote branch in Git:
git push [remote-name] --delete [branch-name]or
git push [remote-name] :[branch-name]Changing Git Remote URL
Section titled “Changing Git Remote URL”Check existing remote
git remote -v# origin https://github.com/username/repo.git (fetch)# origin https://github.com/usernam/repo.git (push)Changing repository URL
git remote set-url origin https://github.com/username/repo2.git# Change the 'origin' remote's URLVerify new remote URL
git remote -v# origin https://github.com/username/repo2.git (fetch)# origin https://github.com/username/repo2.git (push)Updating from Upstream Repository
Section titled “Updating from Upstream Repository”Assuming you set the upstream (as in the “setting an upstream repository”)
git fetch remote-namegit merge remote-name/branch-nameThe pull command combines a fetch and a merge.
git pullThe pull with --rebase flag command combines a fetch and a rebase instead of merge.
git pull --rebase remote-name branch-namels-remote
Section titled “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:
git ls-remote --refIt 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 url.ssh://git@server.com:.insteadOf https://server.com/:
git ls-remote --get-url <aremotename>ssh://git@server.com:user/repoRemoving Local Copies of Deleted Remote Branches
Section titled “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:
git fetch [remote-name] --pruneTo prune deleted branches from all remotes:
git fetch --all --pruneList Existing Remotes
Section titled “List Existing Remotes”List all the existing remotes associated with this repository:
git remoteList all the existing remotes associated with this repository in detail including the fetch and push URLs:
git remote --verboseor simply
git remote -vAdding a New Remote Repository
Section titled “Adding a New Remote Repository”git remote add upstream git-repository-urlAdds remote git repository represented by git-repository-url as new remote named upstream to the git repository
Getting Started
Section titled “Getting Started”Syntax for pushing to a remote branch
Section titled “Syntax for pushing to a remote branch”git push <remote_name> <branch_name>
Example
Section titled “Example”git push origin master
Set Upstream on a New Branch
Section titled “Set Upstream on a New Branch”You can create a new branch and switch to it using
git checkout -b AP-57After 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-57After that, you can use git push while you are on that branch.
Show information about a Specific Remote
Section titled “Show information about a Specific Remote”Output some information about a known remote: origin
git remote show originPrint just the remote’s URL:
git config --get remote.origin.urlWith 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 originChanging a Remote Repository
Section titled “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:
git remote set-url <remote_name> <remote_repository_url>Example:
git remote set-url heroku https://git.heroku.com/fictional-remote-repository.gitRenaming a Remote
Section titled “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
git remote# originCheck existing remote with URL
git remote -v# origin https://github.com/username/repo.git (fetch)# origin https://github.com/usernam/repo.git (push)Rename remote
git remote rename origin destination # Change remote name from 'origin' to 'destination'Verify new name
git remote -v# destination https://github.com/username/repo.git (fetch)# destination https://github.com/usernam/repo.git (push)=== Posible Errors ===
Set the URL for a Specific Remote
Section titled “Set the URL for a Specific Remote”You can change the url of an existing remote by the command
git remote set-url remote-name urlGet the URL for a Specific Remote
Section titled “Get the URL for a Specific Remote”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
Syntax
Section titled “Syntax”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>)…]