Git - git remote

git

What is the purpose of the 'git remote add' command?

It define remote repository shorthand so that you do not have to remember the full URL.

git remote add bob /home/bob/myrepo

The above command create an alias 'bob' that point to the remote repository '/home/bob/myrepo'. This alias can be use wherever we normally use a URL:

git fetch bob

Unlike the longhand form, when Alice fetches from Bob using a remote repository shorthand set up with git remote, what was fetched is stored in a remote-tracking branch, in this case bob/master. So after this:

git log -p master..bob/master

shows a list of all the changes that Bob made since he branched from Alice’s master branch. After examining those changes, Alice could merge the changes into her master branch:

git merge bob/master

This merge can also be done by pulling from her own remote-tracking branch, like this:

git pull . remotes/bob/master

Note that git pull always merges into the current branch, regardless of what else is given on the command line.

What is the purpose of the 'git remote show remote' command?

The command git remote lists all the remotes repositories and git remote show remote shows the branches in a remote repository.

How can we determine the information about the remote repository that we cloned from?

As mentioned earlier, for cloned repositories, Git will automatically give the name “origin” to the server from which you cloned and the name “master” to your local branch. By running the git remote command, you can view the short names, such as “origin,” of all specified remote handles. By using git remote -v, you can view all the remote URLs next to their corresponding short names.

We can work with multiple remote repositories, like we can push our changes to any remote repositories, and we can pull changes from any remote repositories. Our local repositories can contain changes from multiple remote repositories depending on our push and pull histories.

How can we add a remote repository with a shortname?

To add a new remote Git repository with a short name, use the following format:

git remote add shortname url

How can we change or assign a shortname for a remote repository?

To rename a remote’s short name, use the git remote rename command.

git remote rename js jane

How can we remove a remote?

To remove a remote for whatever reason (e.g., a contributor has left the team, the server has moved), simply use the git remote rm command:

git remote rm jane
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License