Decoding git remove remote [3 Different Ways]


GIT

Author: Steve Alila
Reviewer: Deepak Prasad

Quick cheat sheet to perform git remove remote

Knowing how to git remove remote lets you customize your repo's upstream. The upstream, often shortened as u, connects the local repo and its remote. There are three most familiar ways to git remove remote.

If you are sure a remote exists and know its name use

git remote remove <remote-name>

or the shorter form:

git remote rm <remote-name>

Since the above method assumes the name of the remote is the origin, you can replace the remote-name as follows,

git remote remove origin

or shorten it as:

git remote rm origin

The second route to git remove remote is to navigate to the directory containing the repo and clear the entire repo as follows:

rm -rf .git

Lastly, you can git remove remote by renaming it. Running the following command

git remote rename <old-name> <new-name>

overrides the existing remote, thereby deleting it by creating another independent remote.

This tutorial makes it easier to understand git remove remote by explaining what lies in git remote followed by a step-by-step implementation of the three typical ways to git remove remote.

It would be best to get your hands dirty when following this guide by doing the exact steps illustrated. That will enable you to learn the concepts twice faster. What is more? Find out below.

 

What is git remove remote?

Git remove remote is a way to delete the link between the local repo and its remote. Typically, a local repo has its root on your development folder lying on your computer, whereas a remote (repository) exists on a website such as GitHub, GitLab, or Bitbucket.

An upstream simplifies pushing code from the local repo to the remote. Similarly, you can easily pull down changes from the remote to the local repo.

When setting the upstream, all you do is replace the remote URL with an easy-to-remember name like origin or upstream. So, origin, upstream, and a remote repo URL mostly refer to the same thing.

However, introducing a third repo in the equation can differentiate between origin and upstream, for instance when you fork a repo. Then, origin refers to the immediate repo you push changes to, whereas upstream means the parent repo where you forked origin repo's code.

You can learn more about the origin and upstream here.

Now that you know what we are about to work with, let us apply the knowledge in practical usage of git remove remote in the following sections of this tutorial.

 

Lab setup to explore git remove remote

I am creating a remote repo known as git_remove_remote on GitHub.

Decoding git remove remote [3 Different Ways]

Then copy the repo URL

Decoding git remove remote [3 Different Ways]

and move to the terminal or command line. Clone the repo, as shown here:

Decoding git remove remote [3 Different Ways]

Let us build the empty repo to remove the remote repo.

Navigate to the new repo.

cd git_remove_remote

Create a file.

touch first.txt

Stage and commit the file.

git add first.txt

git commit -m "track the first file"

Repeat the procedure for a second commit history.

touch second.txt

Stage the changes.

git add .

Commit them.

git commit -m "track the second file"

Let us view the commit history

git log

in readiness for git remove remote.

Decoding git remove remote [3 Different Ways]

We will modify the setup according to the needs of a target example: removing and adding the remote, creating files and tracking files before pushing the changes.

 

Option-1: The recommended way to perform git remove remote

Removing the upstream is the most typical and recommended way to git remove remote.

Let us move one directory backward and create an environment, exploring the challenging part of removing a remote repo like the cloned repo's remote we are about to remove.

cd ..

mkdir non_cloned_repo

Now, create two commits in the history as we did in the setup section above.

Create the first text file.

touch first.txt

and stage it.

git add .

What did you notice?

We ran into an error, fatal: not a git repository (or any of the parent directories): .git. Here, the command line tells us the current directory has no permissions to track files. We can make the repo track changes by initializing it.

git init

Now that the repo is instantiated, let us continue building the commit history before the push.

git add .

git commit -m "track the first file"

touch second.txt

git add .

git commit -m "track the second file"

Running the command

git push

returns the error, fatal: No configured push destination.

Decoding git remove remote [3 Different Ways]

Let us try using the same command on the cloned repo.

cd ..

cd git_remove_remote

git push

We don't encounter an error this time because a cloned repo comes with a remote name. We can check the name using the -v flag, as shown below:

git remote -v

The cloned repo has a name, origin.

Decoding git remove remote [3 Different Ways]

On the other hand, the non_cloned_repo lacks a remote name, making it impossible to push its changes to a remote repo. Neither can you remove its remote.

Let us assign the repo, non_cloned_repo a push destination by adding the name and URL, as follows:

git remote add <name> <URL>

Although the conventional name is the origin, you can set it to any word you like. For instance, I am setting it to upstream.

git remote add upstream https://github.com/Stevealila/git_remove_remote.git

After adding the remote, we can push our changes through it.

git push upstream

and confirm the effect on the remote repo on GitHub.

Decoding git remove remote [3 Different Ways]

Decoding git remove remote [3 Different Ways]

We can now remove the remote using any of these commands

git remote remove upstream

or

git remote rm upstream

Checking the remote

git remote -v

confirms the remote is gone!

Decoding git remove remote [3 Different Ways]

Here are more ways to remove the remote repo.

 

The two indirect ways to git remove remote are clearing the .git subdirectory or overriding its name with a new one.

 

Option-2: Remove the .git subdirectory

Let us reintroduce the name of the repo, non_cloned_repo, we deleted earlier.

git remote add upstream https://github.com/Stevealila/git_remove_remote.git

Check the repo.

ls -la

We can see the .git subdirectory. It holds all the information concerning the repo, such as the commit history, commit objects, and remotes.

We can view the remotes folder by moving further into the .git subdirectory, as follows.

cd .git/refs

Decoding git remove remote [3 Different Ways]

Since the .git subdirectory hosts the remote information, deleting it is one of the ways to git remove the remote. Let us do that right away.

Navigate to the folder containing the .git subdirectory and run this command:

rm -rf .git

Using the -r flag, we tell our command line to remove everything in the .git subdirectory recursively.

Recheck the remote.

git remote -v

We successfully removed the remote called upstream.

Decoding git remove remote [3 Different Ways]

 

Option-3: Override the git remote with a new name

Let us return to the git_remove_remote repo.

cd git_remove_remote

Confirm the remote.

git remote -v

Override the remote name by changing it to new.

git remote rename origin new

Recheck the remotes

git remote -v

Our initial remote origin no longer exists! Instead, we have new in its place. We can rename it to origin and delete the remote as follows.

git remote rename new origin

git remote remove origin

 

Conclusion

You have seen the ways to git remove remote. The primary method is git remote remove <remote-name>. Alternatively, you can remove the entire .git subdirectorygit rm -rf .git or rename it using the command git remote rename <old-name> <new-name>. Go ahead and apply what suits your git remove remote needs.

 

Steve Alila

Steve Alila

He specializes in web design, WordPress development, and data analysis, with proficiency in Python, JavaScript, and data extraction tools. Additionally, he excels in web API development, AI integration, and data presentation using Matplotlib and Plotly. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment