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 isrepo
, the remote repository isbitbucket.org
, and the remote name isorigin
.
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
git remote rename origin bitbucket
Here, we renamed
origin
tobitbucket
.
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
git remote set-url origin git@bitbucket.org:user/repo.git
Here, we’re setting the URL of
origin
back to Bitbucket.