Managing Git remotes


This post goes over how to manage Git remotes using Bitbucket as an example.

Add remote

To track a local repository to a remote repository:

git remote add origin git@bitbucket.org:user/repo.git

Here, the username is user, the repository name is repo, the remote repository is bitbucket.org, and the remote name is origin.

Set master as the upstream branch:

git push -u origin master

Show remote

To show the remote name and URL:

git remote -v
origin  git@bitbucket.org:user/repo.git (fetch)
origin  git@bitbucket.org:user/repo.git (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@github.com:user/repo.git
git push -u

Now when you show remote:

git remote -v
bitbucket git@bitbucket.org:user/repo.git (fetch)
bitbucket git@bitbucket.org:user/repo.git (push)
origin  git@github.com:user/repo.git (fetch)
origin  git@github.com:user/repo.git (push)

You see there’s bitbucket and origin.

Fetch, pull, push

When you fetch, pull, and push, you’re doing it against the default, which is origin:

git fetch # git fetch origin
git pull # git pull origin
git push # 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 git@bitbucket.org:user/repo.git

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



Please support this site and join our Discord!