Managing git remotes

Add remote

To track a local repository to a remote repository:

$ git remote add origin
$ git push -u origin master

Here, the username is user, the repository name is repo, the remote repository is Bitbucket, and the remote name is origin. We’re setting master as the upstream branch.

Show remote

To show the remote name and URL:

$ git remote -v
origin (fetch)
origin (push)

What if you want to add a new remote and replace it as origin? You can rename the current remote and add a new remote.

Rename remote

To rename the remote:

$ git remote rename origin bitbucket

Here, we renamed origin to bitbucket.

Then add remote origin again and point it to GitHub:

$ git remote add origin
$ git push -u

Now when you show remote:

$ git remote -v
bitbucket (fetch)
bitbucket (push)
origin (fetch)
origin (push)

You see there’s bitbucket and origin.

Fetch, pull, push

When you fetch, pull, and push:

$ git fetch
$ git pull
$ git push

You’re doing it against the default, which is origin:

$ git fetch origin
$ git pull origin
$ git push origin

To fetch, pull, and push to Bitbucket, you can specify the bitbucket remote:

$ git fetch bitbucket
$ git pull bitbucket
$ git push bitbucket

Remove remote

To remove the bitbucket remote:

$ git remote rm bitbucket

Change remote URL

To change the remote’s URL:

$ git remote set-url origin

Here, we’re setting the URL of origin back to Bitbucket.

Please support this site and join our Discord!