Skip to content

Chapter 4 of 7

Collaborating with Remote Repositories

A remote repository is a version of a Git project hosted on a server such as GitHub, GitLab, or Bitbucket, enabling developers to share work across teams. Before interacting with one, the local repository must be linked to it using git remote add <name> <url>, where origin is the conventional name for the primary remote. To inspect configured remotes, git remote -v lists them along with their URLs, while git remote show <name> provides additional detail about tracked branches and the remote's state.

Sending work to and retrieving work from a remote are the two core collaborative actions. The command git push <remote> <branch> uploads local commits to the remote, with the first push of a new branch typically using git push -u origin <branch> to establish tracking. In the opposite direction, git pull fetches changes from a remote and immediately merges them into the current branch, behaving as a combination of git fetch followed by git merge. Because git fetch only downloads remote changes without modifying the working directory, it is safer when one wants to review incoming work before integrating it.

Several commands help maintain a tidy and accurate view of remote state. A tracking branch is a local branch configured to follow a remote counterpart, allowing git pull and git push to work without specifying arguments; such tracking can be configured with git branch --set-upstream-to. To refresh tracking branches across every configured remote at once, git fetch --all downloads their latest changes without touching the working directory. When a remote's name needs updating, git remote rename <old> <new> renames it while preserving its URL. Finally, git remote prune <remote> removes local tracking branches whose counterparts have been deleted on the remote, keeping the local repository clean and consistent.

All chapters
  1. 1Introduction to Git and Its Core Concepts
  2. 2Essential Daily Commands
  3. 3Branching, Merging, and History Shaping
  4. 4Collaborating with Remote Repositories
  5. 5Undoing, Reverting, and Recovering
  6. 6Tags, Submodules, and Productivity Tools
  7. 7Customizing and Maintaining Your Repository

Drill it

Reading is not remembering. These come from the Git Version Control deck:

Q

What is Git?

Git is a distributed version control system that tracks changes in source code during software development, allowing multiple developers to collaborate efficien...

Q

How do you initialize a new Git repository?

Use the command git init in the root directory of your project.This creates a hidden .git subdirectory containing all repository data.

Q

What does <code>git clone</code> do?

git clone &lt;url&gt; creates a copy of an existing remote repository on your local machine.It includes all branches, tags, and history.

Q

What is the purpose of <code>git add</code>?

git add &lt;file&gt; stages changes in the working directory for the next commit.You can stage all changes with git add . or specific files.