Skybinary

View Categories

5. Remote Repositories (GitHub, GitLab, Bitbucket)

2 min read

What is a Remote Repository? #

A remote repository is a version of your project that’s hosted on the internet or a network. It allows multiple developers to collaborate, share code, and synchronize changes between their local machines and a central location.

Platforms like GitHub, GitLab, and Bitbucket are popular services for hosting remote Git repositories.


Setting Up a Remote Repository #

1. git remote add origin <url> — Connect Local Repo to Remote #

This command links your local Git repository to a remote one (for example, on GitHub).

Example:

git remote add origin https://github.com/username/project.git

Here:

  • origin is the name of the remote (a common convention).
  • The URL is the remote repository address.

2. git remote -v — View Connected Remotes #

Displays the list of remote repositories linked to your project.

Example:

git remote -v

You’ll see:

origin  https://github.com/username/project.git (fetch)
origin  https://github.com/username/project.git (push)

Pushing and Pulling #

1. git push — Upload Local Changes to Remote #

Push your commits from your local repository to the remote repository so others can access them.

Example:

git push origin main

This sends your local main branch changes to the remote repository named origin.

If it’s the first push, you can set the upstream branch:

git push -u origin main

The -u flag remembers the remote branch for future pushes.


2. git pull — Fetch and Merge Changes from Remote #

This command retrieves the latest changes from the remote repository and merges them into your current branch.

Example:

git pull origin main

It’s a combination of two actions:

  1. Fetch: Downloads new commits.
  2. Merge: Merges them into your local branch.

3. git fetch — Download Changes Without Merging #

Unlike git pull, git fetch only downloads commits from the remote repository but does not merge them into your local branch.

Example:

git fetch origin

You can then view what changed using:

git log origin/main

And manually merge if needed:

git merge origin/main

Working with GitHub #

1. Creating a Repository on GitHub #

  1. Go to https://github.com
  2. Click New Repository
  3. Add a name, description, and select visibility (public/private)
  4. Click Create Repository
  5. Connect it to your local Git project using: git remote add origin https://github.com/username/repo.git git branch -M main git push -u origin main

2. Forking and Cloning #

  • Forking: Creates a personal copy of someone else’s repository on your GitHub account.
  • Cloning: Downloads the repository (your own or someone else’s) to your computer.

Clone Example:

git clone https://github.com/username/repo.git

3. Pull Requests (PRs) #

A Pull Request is a way to propose changes to a repository. You create a PR to ask maintainers to review and merge your code into the main branch.

Pull Request Workflow:

  1. Fork or clone the repository
  2. Create a new branch for your changes
  3. Commit and push your changes
  4. Open a Pull Request on GitHub
  5. Discuss and review changes
  6. Merge after approval

4. Merging Pull Requests #

Once a PR is approved, it can be merged into the main branch directly from GitHub using the Merge pull request button.

You can also merge locally using Git commands if needed:

git merge feature-branch
git push origin main

5. Issues and Discussions #

  • Issues: Used to track bugs, improvements, or tasks.
  • Discussions: Used for open-ended conversations, feedback, or planning.

These tools improve collaboration and project organization.


6. GitHub Actions (Introduction) #

GitHub Actions allows you to automate workflows such as testing, building, and deploying your code directly from your repository.

Example: Automatically deploy your website when you push changes to main.

Workflow file location:

.github/workflows/deploy.yml

Summary #

CommandDescription
git remote add origin <url>Link local repo to remote
git remote -vView remote connections
git pushUpload local commits to remote
git pullFetch and merge changes from remote
git fetchDownload changes without merging
git clone <url>Copy remote repo locally

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top