Remote Repositories
🎯 Start Here
So far, your Git repository lives only on your computer. But what if your hard drive fails? What if you want to collaborate with others? Remote repositories solve both problems—they're copies of your project hosted online that you can push to and pull from.
- Have you ever lost work because it was only saved on one device?
- How do teams currently share code with each other?
Let's learn how to connect your local Git repository to the cloud using GitHub or GitLab.
Learning Objectives
By the end of this lesson, you'll be able to:
- ✓ Understand Understand what remote repositories are and why they are useful
- ✓ Set up an account on GitHub or GitLab
- ✓ Connect a local repository to a remote repository
- ✓ Push and pull changes between local and remote repositories
Why This Matters:
Remote repositories are how real teams collaborate. Understanding push, pull, and remote connections is essential for contributing to any project.
Before You Start:
You should be familiar with:
- Be familiar with basic Git commands
- Have a Git repository ready to practice with
- Create an account on GitHub or GitLab
What is a Remote Repository?
A remote repository is a version of your project hosted on the internet or another network. It allows you to:
- Collaborate with others by sharing your code
- Back up your project to prevent data loss
- Access your project from multiple devices
Popular platforms for hosting remote repositories include GitHub, GitLab, and Bitbucket. In this tutorial, we'll focus on GitHub and GitLab.
Step 1: Set Up an Account
Before you can use a remote repository, you need to create an account on a hosting platform. Follow these steps:
GitHub
Visit https://github.com and sign up for a free account. Once you've created your account, you can create repositories and collaborate with others.
GitLab
Visit https://gitlab.com and sign up for a free account. GitLab offers similar features to GitHub and is a great alternative.
Step 2: Create a Remote Repository
After setting up your account, create a new repository on your chosen platform. Follow these steps:
- Log in to your account on GitHub or GitLab
- Click the "New Repository" button
- Enter a name for your repository (e.g.,
my-project) - Choose whether the repository should be public or private
- Click "Create Repository"
Step 3: Connect Your Local Repository
To connect your local repository to the remote repository, use the following command:
Replace <repository-url> with the URL of your remote repository. For example:
This command links your local repository to the remote repository and names it origin.
Step 4: Push Changes to the Remote Repository
To upload your local changes to the remote repository, use the following command:
This pushes the changes in your local main branch to the remote repository. The -u flag sets the upstream branch, so you can use git push in the future without specifying the branch.
Step 5: Pull Changes from the Remote Repository
To download changes from the remote repository to your local repository, use the following command:
This fetches and merges changes from the remote main branch into your local branch.
Try It Yourself
Use the interactive terminal below to practice connecting a local repository to a remote repository and pushing changes:
Setting Up Git in VS Code
Visual Studio Code has built-in Git support, making it easy to manage your repositories directly from the editor. Follow these steps to set up Git in VS Code:
Step 1: Open the Source Control Panel
Click on the Source Control icon in the Activity Bar on the left side of the editor. If you have a Git repository initialized in your project folder, it will automatically appear here.
Step 2: Configure Git in VS Code
If you haven't already configured Git globally, VS Code will prompt you to set your name and email. You can also configure it manually by running the following commands in the integrated terminal:
Step 3: Commit Changes
To commit changes, stage your files by clicking the icon next to the file name in the Source Control panel. Then, enter a commit message in the text box and click the checkmark icon to commit.
Step 4: Push and Pull Changes
Use the Source Control panel to push your changes to a remote repository or pull changes from it. You can also use the integrated terminal to run git push and git pull commands.
Step 5: View Git History
Install the Git History extension to view your commit history in a visual format. This can be helpful for tracking changes and understanding your project's timeline.
🏁 Lesson Complete: Remote Repositories
Key Takeaways:
- Remote repositories are copies of your project hosted online (GitHub, GitLab, Bitbucket)
- git remote add origin connects your local repo to a remote one
- git push uploads your commits to the remote repository
- git pull fetches and merges remote changes into your local branch
- VS Code has built-in support for pushing, pulling, and managing remotes
Learning Objectives Review:
Look back at what you set out to learn. Can you now:
- ✅ Understand what remote repositories are and why they are useful Check!
- ✅ Set up an account on GitHub or GitLab Got it!
- ✅ Connect a local repository to a remote repository Can explain it!
- ✅ Push and pull changes between local and remote repositories Could teach this!
If you can confidently answer "yes" to most of these, you're ready to move on!
Think & Reflect:
💭 💭 Reflection Questions
- What are the advantages of having your code hosted on a remote platform?
- When should you push your changes to a remote repository?
- How does pulling changes help when working on a team?
🎯 Looking Ahead:
Now that you can work with remote repositories, you're ready to learn the complete Git workflow that professional teams use every day.
Recommended Next Steps
Related Topics
Explore these related tutorials to expand your knowledge:
Practice Projects
Apply what you've learned with these hands-on projects:
Additional Resources
Deepen your understanding with these helpful resources:
- GitHub Docs: Remote Repositories - Learn about working with remotes in Git.
Progress tracking is disabled. Enable it in to track your completed tutorials.
Cookie Settings