Managing git remotes


Add remote

To track a local repository to a remote repository:

$ git remote add origin git@bitbucket.org:user/repo.git
$ 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  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:

$ 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 git@bitbucket.org:user/repo.git

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



If you enjoyed this post, please consider supporting this site!