Git

How to Remove Git Remotes

“Want to remove a wrongly set Git remote repository? Or perhaps you wish to change a remote in Git? Git offers a simple way to remove a remote using the git remote remove command, which removes a specified remote from the repository.

If this sounds new to you, or you want a quick recap about removing Git remotes, this guide got you covered. We’ll discuss git remotes and offer the fastest ways to remove Git remotes using examples, ensuring you get a grasp of things. Let’s dive in!”

What is a Git Remote?

Git is a decentralized version control system, implying you can work on a remote repository and push it to a remote server. The local and remote repositories use the same system. Therefore, setting a remote is the best option if two clients are working on a repository, especially if collaborating on a project.

A Git remote works as a pointer to a remote version of a repository. When you clone a Git repository and make changes, the main Git repository is not affected, only the local repository, until you push the changes to the remote repository. Meanwhile, you will have a remote Git branch created to work on, named origin by default.

You are not restricted to only using the origin remote, you can create others, and in such a case, that’s where you need to learn how to remove Git remotes when working with various remotes.

How to Remove a Git Remote

The syntax to remove a remote from Git is:

$ git remote rm <url>

or

$ git remote remove <url>

Note that any remote accidentally removed can only be added back manually. Besides, removing a remote using the git remote rm won’t remove the remote from the remote repository as the local and remote repositories are only updated by pushing the local repository to the remote. Therefore, all changes to the local remote don’t affect the remote repository.

The first step in removing a remote is to navigate into your repository directory containing your cloned project.

Next, list the available remotes using the command below. Their URLs will also get displayed.

$ git remote -v

Once you see which remotes you have, you can decide which to remove. In our example, we have the origin and new-remote remotes. Let’s remove the new-remote.

$ git remote remove new-remote

The command above silently removes the remote.

However, you can confirm if the remote got removed using the command below.

$ git remote -v

Note that now we only have the origin remote. The new-remote was removed. That’s one way to remove Git remotes. Your local repository no longer has the remote removed. However, your remote Git repository is unaltered.

Removing Git Remotes by Editing Their Configuration File

Note that even though this method works, it’s not the recommended way to remove Git remotes. Nonetheless, the .git/config displays the remotes associated with your project, and if you need to remove a given remote, you can delete its code from the configuration file.

Start by opening the configuration file using an editor of choice.

Once opened, you will see the available remotes; in our case, we want to remove the remote1.

We need to delete all the code pertaining to that remote, save the file, and exit.

That’s it! If we check the available remotes, we will note the remote is no longer available.

The two methods above offer solutions to removing Git remotes. However, if you need to remove a remote to change the URL, how about updating its remote URL instead? Git allows updating the URL of a remote and setting the new URL, which is an alternative to removing the remote and adding it again.

For instance, let’s create a new remote, remote2, with a given URL.

$ git remote add remote [remote-name] [remote-url]

If we need to change the URL to a new one, all we need is to use the syntax below.

$ git remote set-url [remote-name] [new-url]

List the available remotes and notice that the URL for our remote2 was successfully updated.

Conclusion

Git offers a simple way of working with remotes, including adding and removing them. When working with different local remotes, it’s good to have a clean way of removing them. This guide offered two methods of removing Git remotes. Furthermore, we saw how to update a remote’s URL instead of deleting and adding it back.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.