Git Pull
Git Pull
If you are working on a project with multiple people, the chances are that the codebase will change very often. So you would need to have a way to get the latest changes from the GitHub repository to your local machine.
You already know that you can use the git push
command to push your latest commits, so to do the opposite and pull the latest commits from GitHub to your local project, you need to use the git pull
command.
To test this, let’s go ahead and make a change directly on GitHub directly. Once you are there, click on the README.md
file and then click on the pencil icon to edit the file:
Make a minor change to the file, add a descriptive commit message and click on the Commit Changes
button:
With that, you’ve now made a commit directly on GitHub, so your local repository will be behind the remote GitHub repository.
If you were to try and push a change now to that same branch, it would fail with the following error:
As stated in the output, the remote repository is ahead of your local one, so you need to run the git pull
command to get the latest changes:
The output that you will get will look like this:
We can see that the README.md
file was changed and that there were 2 new lines added and 1 line deleted.
Now, if you were to run git log
, you will see the commit that you’ve made on GitHub available locally.
Of course, this is a simplified scenario. In the real world, you would not make any changes directly to GitHub, but you would most likely work with other people on the same project, and you would have to pull their latest changes regularly.
You need to make sure that you pull the latest changes every time before you try to push your changes.
Now that you know the basic Git commands let’s go ahead and learn what Git Branches are.