Lesson 7
Git Remotes
In this lesson, we will explore the concept of remote repositories and how to interact with them using Git commands. You will learn how to fetch, pull, and push changes between your local repository and remote repositories.
Objectives
- Understand remote repositories
- Learn how to add and remove remotes
- Learn how to fetch and pull from remotes
- Learn how to push to remotes
Understanding remote repositories
A remote repository is a version of your project that is hosted on a remote server, such as GitHub. Remote repositories allow you to collaborate with others, share your work, and keep your local repository in sync with the latest changes.
Adding and removing remotes
To add a remote repository, use the git remote add
command followed by a name for the remote and the remote repository URL. To remove a remote, use the git remote remove
command followed by the name of the remote.
Fetching and pulling from remotes
To fetch changes from a remote repository, use the git fetch
command followed by the name of the remote. This will download the changes but not merge them into your local branch. To fetch and merge changes, use the git pull
command followed by the name of the remote and the branch you want to pull from.
Pushing to remotes
To push your local changes to a remote repository, use the git push
command followed by the name of the remote and the branch you want to push to. This will update the remote repository with your latest changes.
Exercises
Add a remote repository
- Navigate to github.com/eriknewland/gitty
- Fork the
eriknewland/gitty
repository on GitHub - Then, clone your forked repository to your local machine
- Add the original
eriknewland/gitty
repository as a remote named 'upstream' using thegit remote add
command.
Create a pull request
- Create a new branch in your local repository and make some changes to the code
- Commit your changes and push the new branch to your forked repository on GitHub
- Then, create a pull request in your own repository to merge the changes from the new branch into the 'main' branch
- Review and approve the pull request.