Master Git Remotes: How to Manage, Update, and Sync Your Repositories

When working with Git, understanding how to manage remotes is crucial for maintaining a smooth and efficient workflow. Whether you’re collaborating on a large project or just syncing your local code with a repository, mastering remote operations can save you time and prevent common issues.

This guide covers everything you need to know about working with Git remotes, from removing outdated branches to setting up new repositories. By the end, you’ll be well-equipped to handle any remote operation with confidence, keeping your codebase clean and your projects on track. Let’s dive in!


How to Remove a Remote Branch in Git

To delete a remote branch in Git:

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

or

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

Updating Your 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 URL

Verify new remote URL

git remote -v
# origin https://github.com/username/repo2.git (fetch)
# origin https://github.com/username/repo2.git (push)

Viewing Your Current Git Remotes

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

Cleaning Up 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] --prune

To prune deleted branches from all remotes:

git fetch --all --prune

Syncing with an Upstream Repository

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

Using git ls-remote to List Remote References

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 the 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 returns https://server.com/user/repo, and you have set git config url.ssh://git@server.com:. instead of https://server.com/:

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

New Remote Repository in Git

git remote add upstream git-repository-url

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


Setting Upstream Branches for New Git Branches

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.


Push to the Remote Branch

Syntax for pushing to a remote branch

git push <remote_name> <branch_name>

Example

git push origin master

How to Rename a Git Remote

To rename the remote, use the command git remote rename

The git remote rename the command takes two arguments:

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

Get the existing remote name

git remote
# origin

Check 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)

Displaying Detailed Information About a Specific Git Remote

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

Setting the URL for a Specific Git Remote

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

git remote set-url remote-name url

Retrieving the URL of a Specific Git 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

How to Change Your Git 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.git

Conclusion

So this is all about the Remote Repository in Git. In an upcoming article, we will deep dive into Git Staging.

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply

    Your email address will not be published. Required fields are marked *