Skip to main content

Collaborating with Remote Repositories

Once you have committed changes locally, you often need to share your work or get updates from a remote repository such as GitHub.


1. Add a Remote Repository

git remote add origin <repository-url>

Description: Links your local repository to a remote repository.
This only needs to be done once per repository.

Example:

git remote add origin 'https://github.com/user/repo.git'
  • You can verify the remote using:
git remote -v

2. Push Changes to Remote

git push -u origin <branch>

Description: Sends your local commits to the remote repository.

  • -u sets the upstream branch for future pushes.
  • Replace <branch> with your branch name, e.g., main or master.

Example:

git push -u origin main
  • For subsequent pushes, you can simply run:
git push

3. Pull Changes from Remote

git pull origin <branch>

Description: Fetches and merges changes from the remote repository into your local branch.

  • Always pull before pushing to avoid conflicts.

Example:

git pull origin main
  • This ensures your local repository is up-to-date.

4. Fetch Changes Without Merging

git fetch origin

Description: Downloads updates from the remote without merging them.

  • Useful if you want to inspect changes before merging.

  • After fetching, you can merge manually if needed.


Summary of Remote Collaboration Commands

CommandPurpose
git remote add origin <url>Link local repository to a remote
git remote -vVerify remote repositories
git push -u origin <branch>Push commits to remote and set upstream branch
git pushPush commits to remote (after initial setup)
git pull origin <branch>Fetch and merge remote changes
git fetch originFetch remote changes without merging

Next, you’ll learn how to work with branches in the repository.