Selected Reading

Git Working with Remote



Repositories must be managed remotely in order to collaborate on projects using Git. Essentially, these are networked or online replicas of your project.

  • They can be many, each with different access restrictions.

  • Adding and removing these distant locations, managing the branches inside of them, and configuring particular branches to automatically follow changes are all examples of collaborative tasks.

  • To share your work with others by pushing and pulling data efficiently, you must become proficient in these skills.

In Git, a repository is considered remote even if it resides on the same host. It doesn't always indicate that it's on a different network or the Internet.

Pushing, pulling, and fetching are standard activities when working with a remote repository of this kind, just like with any other.

Showing Your Remotes

View your Git remote connections: To see a list of specified remote server nicknames, use the git remote command.

D:\Git-test\WBC>git remote

The output message is as follows:

origin

Origin is the default name for the server you cloned from, and it appears when you clone a repository.

D:\Git-test>git clone https://github.com/tony/WBC.git

The output message is as follows:

Cloning into 'WBC'...
remote: Enumerating objects: 971, done.
remote: Counting objects: 100% (971/971), done.
remote: Compressing objects: 100% (746/746), done.
remote: Total 971 (delta 179), reused 964 (delta 174), pack-reused 0
iB/s
Receiving objects: 100% (971/971), 1.48 MiB | 2.53 MiB/s, done.
Resolving deltas: 100% (179/179), done.

The git remote command displays the shortnames of all specified remote handles, and allows you to inspect configured remote servers.

D:\Git-test\WBC>git remote -v

The output message is as follows:

origin  https://github.com/tony/WBC.git (fetch)
origin  https://github.com/tony/WBC.git (push)

The name origin, which is Git's default name for the server you cloned from, should appear if your repository has been cloned.

D:\Git-test\WBC>git remote -v

The output message is as follows:

origin  https://github.com/tony/WBC.git (fetch)
origin  https://github.com/tony/WBC.git (push)
vs      https://github.com/tony/WBC.git (fetch)
vs      https://github.com/tony/WBC.git (push)

Adding Remote Repositories

Since we covered how git clone creates an origin remote by default, let's explore adding remotes manually.

git remote add  <url>

To do that, use the command git remote add <shortname> <url>. Here, <shortname> is a nickname you choose for easy reference, and <url> is the web address of the remote repository.

D:\Git-test\WBC>git remote add vs https://github.com/tony/WBC.git

D:\Git-test\WBC>git remote -v

The output message is as follows:

origin  https://github.com/tony/WBC.git (fetch)
origin  https://github.com/tony/WBC.git (push)
vs      https://github.com/tony/WBC.git (fetch)
vs      https://github.com/tony/WBC.git (push)

Fetching and Pulling from Your Remotes

For your Git repository, the git fetch command functions as a downloader. Without changing your ongoing work, it fetches fresh data (commits, branches, and tags) from a remote repository.

You can track new branches and determine whether to merge them into your local work at any time by using this to get an updated view of the remote project.

$ git fetch <remote>

Git establishes a remote called origin that is connected to the source server when you clone a project. Then, any updates (new commits, branches) are fetched from origin using the git fetch origin command; however, they are not automatically integrated.

This ensures the safety of your local work. Using different commands, you may later determine when to merge these changes into your project.

D:\Git-test\WBC>git remote

The output message is as follows:

origin
vs
D:\Git-test\WBC>git fetch vs

The output message is as follows:

From https://github.com/tony/WBC
  * [new branch]      main       -> vs/main

You can simplify your workflow by using git pull to automatically fetch and merge the remote branch if your current branch is set up to track it.

Git automatically configures your local master branch to follow the remote master branch when you clone a repository. Thus, when you execute git pull, data is retrieved from the original server and integrated into your current code.

Pushing to Your Remotes

The command git push <remote> <branch> must be used to push your project upstream when you're ready to share it.

For example, you can use this command to send your commits back to the server if you want to push your master branch to the origin server (which is usually configured automatically when cloning).

$ git push origin master

If you cloned from a server to which you have write access and no one else has pushed the modifications in the meantime, then this command works. Your push will be denied if several users clone at the same time and one pushes modifications before you do. Before pushing, you must first fetch their work and combine it into yours.

D:\Git-test\WBC>git push origin main

The output message is as follows:

Everything up-to-date

Inspecting a Remote

To obtain further information on a particular remote, use the command git remote show <remote>. When this command is run with a specified shortname, such as origin, the following output is produced:

$ git remote show <remote>
  • The command shows the tracking branch data and the remote repository's URL.

  • It offers helpful advice, saying that when you perform git pull while working on the master branch, the remote branch's master branch gets immediately merged into the local branch after a retrieve.

  • It also includes a list of all the pulled remote references.

$ git remote show origin

The output message is as follows:

* remote origin
Fetch URL: https://github.com/tony/WBC.git
Push  URL: https://github.com/tony/WBC.git
Head branch: main
Remote branch:
  main tracked
Local branch configured for 'git pull':
  main merges with remote main
Local branch configured for 'git push':
  main merges with remote main(up to date)

This command shows the push destination that git push by default uses while working on particular branches.

In addition, it shows removed remote branches, remote branches that are not yet locally available, and local branches that can automatically merge with the relevant remote-tracking branches when performing a git pull.

Renaming and Removing Remotes

Use the git remote rename command to change a remote's shortname. To convert vs to vtp,for instance, use git remote rename.

$ git remote rename
D:\Git-test\WBC>git remote rename vs vtp

The output message is as follows:

Renaming remote references: 100% (1/1), done.
D:\Git-test\WBC>git remote

The output message is as follows:

origin
vtp

Additionally, all remote-tracking branch names are updated appropriately by this step. For example, after renaming the remote, vs becomes vtp.

Use git remote remove or git remote rm to remove a remote in case it needs to be removed (for example, because of a server relocation or stopped being used).

D:\Git-test\WBC>git remote

The output message is as follows:

origin
vtp
D:\Git-test\WBC>git remote remove vtp

D:\Git-test\WBC>git remote

The output message is as follows:

origin

Upon executing git remote remove or git remote rm to remove a remote reference, all related remote-tracking branches and configuration settings are also deleted.

Advertisements