Remote Repositories with GitHub
Authors
Setup
Create a GitHub Account
- Go to GitHub Sign Up.
- Fill in the required details.
- Verify your email and complete the account setup.
In this unit, we will learn how to connect a local Git repository to GitHub, clone an existing repository to work locally, and fork repositories to contribute to open-source projects or collaborate with others. GitHub provides a platform for storing repositories online, enabling version control, collaboration, and backup of your work. This unit covers essential commands for working with remote repositories.
Section 1: Pushing a Local Repository to GitHub
Once you have a local repository, you can push it to GitHub to store it online and collaborate with others. This section covers adding a remote repository, pushing changes, and setting an upstream branch for future updates.
| Code | Description | VS Code GUI |
|---|---|---|
git remote add origin <URL> |
Link the local repository to a remote GitHub repository | Click Source Control (Ctrl + Shift + G) → Click Publish Repository |
git push -u origin main |
Push local commits to the remote repository and set upstream | Click Source Control → Click … → Click Push |
Exercises
-
Adding a remote and pushing the initial commit
- Open a local git repository on VS Code.
- Create a new public repository on GitHub (without initializing it with README. It should be completely empty) and note down the URL (Refer above figure to find it).
- In your local repository, add the remote URL using
git remote add origin <repo-url> - Push the local repository to GitHub using
git push -u origin main. - Verify that the repository is now visible on GitHub.
-
Pushing new file to GitHub
- Add a new text file with some content and commit the changes in local repository.
- Push the new commit to GitHub using
git push -u origin main. - Check GitHub to confirm that the new file is seen.
-
Pushing changes to a file to GitHub
- Add new lines to the text file and commit the changes in local repository.
- Push the new commit to GitHub
- Check GitHub to confirm that the changes are reflected in the file.
Section 2: Cloning a GitHub Repository to a Local Machine
If a repository already exists on GitHub, you can clone it to your local machine to work on it. This section covers cloning repositories and pulling the latest changes from GitHub.
| Code | Description | VS Code GUI |
|---|---|---|
git clone <repo-url> |
Clone a GitHub repository to a local machine | Click Source Control (Ctrl + Shift + G) → Click Clone Repository |
git pull origin main |
Pull the latest changes from GitHub | Click Source Control → Click … → Click Pull |
Exercises
Start these exercises on a new VS Code window opened to a folder that is not a git repository
-
Cloning an existing, public repository from Github
- Note the url of the workshop github repo (Refer image above).
- In the VS Code terminal, run
git clone <repo-url>or use VS Code GUI to clone the repository. - Open the cloned folder in VS Code and check that all the files are present on your local machine.
-
Cloning your own repository from GitHub
- Create a new public repository on GitHub, this time initializing it with a README and note the URL.
- In the terminal, run
git clone <repo-url>or use VS Code GUI to clone the repository. - Open the cloned folder in VS Code and check that the README file exists.
-
Pulling updates from GitHub
- Make a change directly on GitHub (edit the README file and commit).
- In the terminal, run
git pull origin mainor use VS Code GUI. - Verify that the changes are now in your local copy.
Section 3: Forking a Repository on GitHub
Forking allows you to create a personal copy of someone else’s repository. This is useful for contributing to open-source projects or working on repositories you don’t have direct write access to. This section covers forking a repository, cloning the fork, and keeping it updated with the original repository.
| Code | Description | VS Code GUI |
|---|---|---|
| (GitHub UI) | Fork a repository on GitHub | Click Fork on the GitHub repository page |
git clone <your-fork-url> |
Clone your forked repository locally | Click Source Control (Ctrl + Shift + G) → Click Clone Repository |
git remote add upstream <original-repo-url> |
Link the original repository as an upstream remote | NA |
git push origin main |
Push the merged changes to your forked repository on GitHub | Click Source Control → Click … → Click Push |
Exercises
Start with a new VS Code window
-
Forking and cloning this repository
- Fork this repository on GitHub.
- Clone your forked repository using
git clone <your-fork-url>. - Make a change to the README file (or another relevant file).
- Commit and push the changes to your forked repository using
git push origin main.
-
(Optional) Forking and Cloning Each Other’s Repositories
- Each participant creates a new public repository with a README file containing their name.
- Fork each other’s repositories on GitHub.
- Clone the forked repository locally using
git clone <fork-url>. - Add a new file (e.g.,
hello_your_name.txt) and commit the changes. - Push the changes to the forked repository.
-
(Optional) Creating Pull Requests and Merging Changes
- Each participant creates a Pull Request (PR) to the original repository from their fork.
- The original repository owner reviews the PR.
- The repository owner merges the PR.