When collaborating on a project with others using GitHub, pulling changes ensures that your local repository stays updated with the latest modifications made by other contributors. This chapter will guide you step-by-step on how to pull changes from GitHub, starting with the basics and progressing to more advanced scenarios.
Before pulling changes from GitHub, ensure the following:
Git is installed on your computer.
You have a GitHub account and the repository URL.
You have cloned the repository to your local machine.
If you haven’t cloned the repository yet, use the following command:
git clone
This will create a local copy of the repository.
Before you can pull changes, you need to have a local copy (clone) of the repository from GitHub. Here’s how you can set that up:
Note : You can also use SSH or HTTPs both will work
git clone https://github.com/diginodeship/diginodeship.git
This will create a local copy of the repository on your computer, and now you’re ready to pull changes
Note : There is one extra change on github , and local repo is not up to date
To pull the latest changes from GitHub into your local repository:
Step 1: Make sure you’re in your local repository directory. Use cd
to navigate into your project folder:
cd repository-name
git pull origin main
origin
is the default name for your remote repository (i.e., GitHub).main
is the name of the branch you want to pull changes from. Most projects use main
or master
as the main branch. If your project uses a different name for the main branch, replace main
with that name.Note💡: You will see that there is one insertion from github to your local repo.
The git pull
command does two things:
This is an easy way to sync your local copy of the project with the remote repository.
Sometimes, when you pull changes from GitHub, Git may encounter conflicts if you’ve made changes to the same part of the code that someone else has changed. Git will pause the pull process and ask you to resolve the conflicts.
Step 1: Git will mark the conflicting files with conflict markers:
<<<<<<< HEAD
Your changes
=======
Changes from GitHub
>>>>>>> branch-name
Step 2: You need to manually resolve these conflicts by editing the file. Choose which changes to keep (your changes, the changes from GitHub, or a combination of both), then remove the conflict markers.
Step 3: Once you’ve resolved the conflicts, stage the changes:
git add filename
Step 4: Finally, commit the merge
git commit
Sometimes, you may have local changes that are not ready to be committed but want to pull the latest changes from GitHub. In this case, you can stash your changes temporarily.
Step 1: Stash your local changes:
git stash
If you need specific changes, use git fetch
followed by git cherry-pick
:
1.Fetch the changes:
git fetch origin
2.Cherry-pick the commit:
git cherry-pick
Replace <commit-hash>
with the actual commit ID.
You can use git pull --rebase
to avoid creating merge commits:
git pull --rebase origin main
This applies the fetched changes on top of your local commits, creating a cleaner history.
If you encounter authentication issues, ensure your credentials are correct:
Use SSH keys for secure access.
Update your stored credentials if they’ve changed.
If Git cannot perform a fast-forward merge, manually resolve the situation by merging or rebasing.
Pulling changes from GitHub is a crucial skill for any developer working on collaborative projects. Whether you use the basic git pull command or advanced options like rebasing and cherry-picking, staying in sync with the remote repository ensures your workflow remains efficient and conflict-free. Practice these steps regularly to master the process. Happy Coding! ❤️