24/7/365 Support

Receiving updates with Git pull

In the last recipe, we learned how to set up a remote repository and send local changes to a remote using the git push command. The story is not complete yet. When the repository is shared by multiple people, everyone will push their own changes. The central repository will keep on updating. When you want to synchronize or push your changes to the central repo, you need to download any updates made by other users and then push your modifications on top of that. A git pull command will be used to pull down any updates to the remote central repository to your local repository.

This recipe covers the git pull command. We will use this command to resolve a rejected push, but it is generally used simply to update your local copy.

Getting ready

You will need one central remote repository; it may be hosted on GitHub or anywhere else.

Secondly, you will need two local copies of the central repo. Use the git clone command to create a local replica of the remote repository. These two copies are used for demonstration purposes; in the real world, you will already have multiple copies with different users of your repository:

$ git clone https://github.com/sawantuday/mynewproject.git local_copy_1

$ git clone https://github.com/sawantuday/mynewproject.git local_copy_2

Now enter local_copy_1, create a new file with random content and then commit and push the changes back to the remote repository:

$ cd local_copy_1

$ echo "// Modifications by user 1" >> index.php

$ git add .

$ git commit -m "Index.php created with comments"

$ git push origin master

Your push command should complete without any errors or warnings.

Next, enter local_copy_2 and create a new file with random contents:

$ cd local_copy_2

$ echo "\\ Modifications by user 2" >> main.php

How to do it…

Suppose you are user two working on a copy, local_copy_2. You cloned the repository and started working with the code base. In the meantime, user one completed his work and pushed his changes back to the central repo. Now, after you have completed your work, you are ready to send updates to the remote repo:

Commit your modifications to the local repository:

$ git add .

$ git commit -m "main.php created with comments"

Try to push your commit to the central repo:

$ git push origin master

This time, your push should fail, saying someone else had already updated the remote repository. Git will give you details of a rejected push, as follows:

Now you need to pull remote changes; first, with git pull, merge any potential conflicts, and then try to push again:

$ git pull origin master

You will be asked to enter a merge message in nano or a similar editor. Simply accept the pre-filled message and save the file by pressing Ctrl + O, then press Enter to save, and then Ctrl + X to exit.

Now try to push again. This time it should complete successfully:

$ git push origin master

How it works…

As we saw in the previous example, git pull is used to pull the remote modifications to the local repository. It is a good idea to use git pull before starting your work on the local copy. This way you can be sure that you have all remote updates in your local repository, thus reducing the chances of a rejected push.

The git pull command can be used any time, even to simply update your local codebase with the remote copy. I have used it in a commit and push flow just to demonstrate the rejected push and merge scenario.

The example demonstrates the simple automated merge. It may happen that both user one and user two are working on the same file and incidentally modify the same part of the code. Git will report a Merge conflict, as follows:

Now, in this case, Git may not be able to automatically merge both updates. It will combine both updates in single file and mark them in a special format, as follows:

In this case, you need to decide what to keep and what to remove. Once you are done with solving conflicts, remove the special tags added by Git and commit the conflicting file. After that, you can push your updates along with the new commit for merging.

See also

You can read more by following these links:

Git pull: https://git-scm.com/docs/git-pull

Git merge: https://git-scm.com/docs/git-merge

Git fetch: https://git-scm.com/docs/git-fetch

Help Category:

What Our Clients Say