Skip to content

Remote repositories

Adding a Remote Repository (git remote add)

Section titled “Adding a Remote Repository (git remote add)”

A remote repository is a version of your project that is hosted on a server, such as GitHub, GitLab, or Bitbucket. To collaborate with others or back up your work, you need to connect your local repository to a remote repository.

Steps:

  1. Obtain the Remote Repository URL:

    • This URL is provided by the hosting service and can be an HTTPS or SSH link.
  2. Add the Remote Repository: Use the git remote add command to add a remote repository. You need to provide a name (commonly origin for the main remote) and the URL.

Example:

Terminal window
git remote add origin https://github.com/username/repository.git
# or using SSH
git remote add origin git@github.com:username/repository.git

What It Does:

  • Adds a remote repository named origin with the provided URL.
  • You can verify the remote repository with git remote -v.

Fetching Changes from a Remote (git fetch)

Section titled “Fetching Changes from a Remote (git fetch)”

Fetching updates from a remote repository downloads changes to your local repository but does not merge them into your working branch. It updates your remote-tracking branches.

Usage:

Terminal window
git fetch origin

What It Does:

  • Retrieves new commits and branches from the remote repository.
  • Updates your local references to remote branches.

Example:

Terminal window
git fetch origin
# Fetches updates from the 'origin' remote repository.

What It Shows:

  • Updates are reflected in remote-tracking branches, such as origin/main.

Pulling changes from a remote repository combines fetching and merging. It retrieves new commits and automatically merges them into your current branch.

Usage:

Terminal window
git pull origin main

What It Does:

  • Fetches updates from the remote repository.
  • Merges these updates into your current branch.

Example:

Terminal window
git pull origin main
# Fetches and merges changes from 'origin/main' into your current branch.

What It Shows:

  • Displays the commits that were fetched and merged.

Pushing uploads your local commits to a remote repository. It updates the remote branch with your local changes.

Usage:

Terminal window
git push origin main

What It Does:

  • Sends your local commits to the specified branch on the remote repository.

Example:

Terminal window
git push origin main
# Pushes local commits on 'main' to the 'origin' remote repository.

Common Variations:

  • Push All Branches:
    Terminal window
    git push --all origin
  • Push Tags:
    Terminal window
    git push --tags

Forking a repository creates a personal copy of someone else’s repository on your own GitHub, GitLab, or Bitbucket account. This allows you to make changes without affecting the original repository.

Steps:

  1. Navigate to the Repository: Go to the repository page on GitHub, GitLab, or Bitbucket.

  2. Click on Fork: Click the “Fork” button usually found at the top-right of the repository page.

  3. Clone Your Fork: Clone the forked repository to your local machine.

Example:

Terminal window
git clone https://github.com/your-username/repository.git

What It Does:

  • Creates a copy of the repository under your own account.
  • Allows you to make changes and propose them to the original repository via pull requests.

Pull Requests (PRs) allow you to propose changes from your fork or a branch to another repository’s branch. They are used for code review and collaboration.

Steps:

  1. Make Changes: Commit changes to a branch in your forked repository.

  2. Push Changes: Push your branch to your remote fork.

Example:

Terminal window
git push origin feature-branch
  1. Create a Pull Request:
    • Go to the repository page on GitHub, GitLab, or Bitbucket.
    • Navigate to the “Pull Requests” section.
    • Click “New Pull Request” or similar.
    • Select your branch and the target branch of the original repository.
    • Add a title and description, then create the pull request.

Managing Pull Requests:

  • Review: Collaborators review the pull request, leave comments, and suggest changes.
  • Merge: Once approved, the pull request can be merged into the target branch.
  • Close: If the pull request is no longer needed, it can be closed without merging.

Code Review is an essential part of collaborative development. It ensures code quality and allows team members to provide feedback.

Common Workflows:

  1. Feature Branch Workflow:

    • Create a branch for each feature or fix.
    • Push changes to the branch.
    • Open a pull request to merge the branch into the main branch.
    • Review, discuss, and merge the pull request.
  2. Fork and Pull Workflow:

    • Fork the repository.
    • Clone the fork and create a feature branch.
    • Push changes to the fork.
    • Open a pull request from your fork to the original repository.
  3. Gitflow Workflow:

    • Use predefined branches like main, develop, feature, release, and hotfix.
    • Follow specific rules for merging and branching to manage releases and hotfixes.

Code Review Practices:

  • Request Reviews: Ask team members to review your pull request.
  • Provide Feedback: Leave comments and suggestions on the code.
  • Test: Ensure that the changes are tested before merging.
  • Resolve Conflicts: Address any conflicts or issues raised during the review.

These practices and commands help streamline collaborative development, making it easier to manage changes, review code, and maintain a high-quality codebase.