GitHub and Remote Repositories
Workplace Context
Your team has adopted GitHub to manage remote repositories and collaborate on projects with multiple contributors. As the team expands, developers will push their code to GitHub, allowing others to pull the latest changes and submit their updates seamlessly. Your task is to set up your project on GitHub and manage remote repositories, enabling collaboration across the team.
You will need to upskill on how to push your local commits to a GitHub repository, pull updates from your teammates, and ensure that the project stays synchronized across all contributors.
Learning Objectives
By the end of this lesson, you will be able to:
- Create a GitHub repository and link it to a local repository.
- Push changes to a remote repository on GitHub.
- Pull changes from a remote repository to your local machine.
- Explain how to work with remote branches.
Creating a GitHub Repository
A remote repository is a version of your project hosted on an external server, allowing team members to collaborate. GitHub is a popular platform for hosting these repositories.
Think about remote repositories like cloud drives, like Google Drive, OneDrive, iCloud, or DropBox, but for your code. You still keep a local copy of the files on your computer, but you can upload (push) or download (pull) files to and from the remote repository in order to make sure that multiple users have access to the latest versions of the files.
This is what these commands look like when working with remote repositories. Feel free to reference this diagram as you continue your training.
Source: Devguide
Step-by-Step: Creating a Repository on GitHub
-
Log in to GitHub:
- Open your web browser and go to GitHub .
- Log in with your credentials.
-
Create a New Repository:
- Click the green New button on the GitHub homepage to create a new repository.
- Name the repository (e.g.,
my-first-repo
). - Choose whether to make it public or private (a public repo is visible to anyone, while a private repo is restricted to you and collaborators).
- Do not initialize the repository with a README file since we will be linking our local project.
-
Link Your Local Repository to GitHub:
- After creating the repository, GitHub will provide you with a URL to connect it to your local Git repository.
- In your terminal, navigate to the project’s folder and add the remote repository:
$ git remote add origin https://github.com/your-username/my-first-repo.git
-
Verify the Remote:
- To verify that your local repository is linked to GitHub, you can run:
$ git remote -v
- To verify that your local repository is linked to GitHub, you can run:
Pushing Changes to GitHub
Now that your local repository is linked to GitHub, you can push your changes to the remote repository so others can access them.
Step-by-Step: Pushing Changes
-
Stage and Commit Your Changes:
- Make sure your changes are staged and committed locally:
$ git add . $ git commit -m "Initial commit"
- Make sure your changes are staged and committed locally:
-
Push to GitHub:
- Push your changes to the remote repository:
$ git push -u origin main
The
-u origin main
flags tell Git to push the changes to themain
branch on theorigin
remote. Once set up, future pushes can be done with justgit push
. - Push your changes to the remote repository:
Pulling Changes from GitHub
In collaborative environments, other developers might push changes to the shared repository. To keep your local copy up to date, you can use the git pull
command.
Step-by-Step: Pulling Changes
-
Pull Changes from the Remote Repository:
- To fetch changes from the remote repository and merge them into your local branch, use:
$ git pull origin main
- To fetch changes from the remote repository and merge them into your local branch, use:
-
Resolve Conflicts (if applicable):
- If there are conflicting changes, Git will prompt you to resolve the conflict manually. Open the conflicting file, resolve the differences, stage the resolved file, and commit the changes.
Working with Remote Branches
Branches allow multiple developers to work on different features without interfering with each other’s work. You can create and push branches to GitHub to keep your work separate until it is ready to be merged.
Step-by-Step: Creating and Pushing a Branch
-
Create a New Branch:
- If you want to work on a new feature, create a new branch locally:
$ git checkout -b feature/new-feature
- If you want to work on a new feature, create a new branch locally:
-
Push the Branch to GitHub:
- Push the branch to GitHub so others can view it:
$ git push origin feature/new-feature
- Push the branch to GitHub so others can view it:
-
Switch Back to Main Branch:
- When you are done, switch back to the main branch:
$ git checkout main
- When you are done, switch back to the main branch:
Interactive Idea: GitHub Pull Request Simulator
To reinforce learning, you could develop a GitHub Pull Request Simulator. This tool could simulate the process of submitting and reviewing pull requests, allowing learners to:
- Create a pull request for their feature branch.
- Add comments and suggestions.
- Approve or request changes on a peer’s pull request.
This simulation would help learners get familiar with the GitHub workflow in a hands-on way without needing a real project.
Examples from Industry
React Repository
With the additional context of how local developers work with remote repositories, take another look at the React repository on GitHub .
- What challenges might working with such a large repository and so many collaborators present?
- How often do you think you should sync changes from the remote repository to your local working copy, and vice versa?
- What might happen if multiple contributors are working on the same features simultaneously?
As we continue into the next set of trainings, we will address these issues and more.
Knowledge Check
Which command is used to push changes to a remote repository?
- Select an answer to view feedback.
What does the git pull
command do?
- Select an answer to view feedback.
Summary
In this lesson, you learned how to create and link a GitHub repository to your local project, push changes to a remote repository, and pull changes made by others. These skills are essential for collaboration and project management in real-world development environments. Next, we will explore how to handle branching and merging in Git to manage multiple features and contributions effectively.