Download Remote Repository and Merge to Local Repository – Git Pull
Pull is used when you want to download the latest Remote Repository information and merge it with the existing branch in the Local Repository. git pull
is the command used to execute the pull action. The git pull
command is often explained as a shortcut command for git fetch
and git merge
. As the git pull
command shortcuts the manual checking process of the Remote Repository information, it may create a conflict. For a simple operation, you can use the pull command. However, generally, it is safer to use the git fetch
command and check the Remote Repository status first before executing the git merge
command.
For a better understanding, please go through the following practice section.
Practice
Objective:
Check how the pull operation works from both the repository owner and collaborator's points of view
In this practice, we'll use the following two users: Developer A and Developer B.
1. Download the project files for the first time: $ git clone
This is the same step we explained in Chapter 3. As we are using a different repository, you need to go through the same process again.
Action by Developer A
Before Developer B accesses the Remote Repository, Developer A needs to grant access to it.
Go to Settings of the repository and select Manage access. Press the green button to add a collaborator.
Find a collaborator. In this demo, we invite sky-blue2022 (Developer B).
Next, we'll be explaining the steps from Developer B's point of view.
Action by Developer B
After Developer A sends an invitation, Developer B receives an email with the invitation like shown below.
Click View invitation and press the Access invitation button like in the image below.
Now you as Developer B have access to the Remote Repository. To clone the project file, click the Code button and copy the URL for HTTPS.
Go to the command line and run the git clone
command.
Open the main project directory
When you run the git clone
command., make sure that the current directory is where you want to create the project directory. In this project, you need to set the project's main directory for Developer B (e.g., Dev_B_skyblue) as the current working directory.
A quick way to open the directory with VS Code is by using drag & drop.
Open a new terminal. You can see that the project's main directory is shown in the EXPLORER section on the left and the directory is shown as the current working directory in the terminal.
You can also use the command line to move the current directory to the main project directory.
cd ~/Dev_B_skyblue
Once you set the current working directory properly, run the command below.
git clone git@github.com:bloovee/git_remote_practice.git
or
git clone https://github.com/bloovee/git_remote_practice.git
If the command successfully goes through as shown below, you'll see that the git_remote_practice directory is generated under the directory in which you executed the clone command.
Cloning into 'git_remote_practice'...
:
Resolving deltas: 100% (2/2), done.
2. Run the git pull command
At this stage, the HEAD of the master branch in each repository is the following.
- Developer A's Local Repository: M1LA
- Remote Repository: M2RA
- Developer B's Local Repository: M2RA
From Developer B's point of view, at this stage, the statuses of the Remote Repository and the Local Repository on the master branch are the same; the HEAD is commit M2RA. This means that there is nothing you can pull on the master branch. However, Developer A's master branch lags behind.
To practice the pull command, we'll do the following actions:
- Pull the master branch on Developer A's local computer
- Further edit the master branch on Developer A's computer and push it to the Remote Repository
- Pull the updated master branch on Developer B's local computer
Action by Developer A
Run the pull command specifying the Remote Repository name (origin) and the master branch.
git pull origin master
You can see that the master branch was updated by the Fast-forward merge.
bloovee@MBP git_remote_practice % git pull origin master
remote: Enumerating objects: 5, done.
:
Fast-forward
git_remote_practice.html | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
To confirm the commit status, check the log.
git log --oneline
You can see that the HEAD of the master branch in Developer A's Local Repository is the same as the Remote Repository's.
4e1a7d9 (HEAD -> master, origin/master) M2RA
4b30a1c M1LA
Next, edit the html file on the master branch.
<!-- Master Branch-->
<h1>M1LA</h1>
<h1>M2RA</h1>
<h1>M3LA</h1>
<!-- /Master Branch-->
Commit the change with the commit message of "M3LA" and check the log to see the latest status.
M3LA indicates the following :
- M – Master Branch
- 3 – 3rd commit on the branch
- L – Committed in the Local Repository
- A – Committed by Developer A
git commit -am "M3LA"
git log --oneline
You can see that a new commit was successfully created as shown below.
4e7dbf9 (HEAD -> master) M3LA
4e1a7d9 (origin/master) M2RA
4b30a1c M1LA
Finally, push the master branch to the Remote Repository.
git push origin master
You can see that the latest commit was successfully pushed like shown below.
Enumerating objects: 5, done.
:
To https://github.com/bloovee/git_remote_practice.git
4e1a7d9..4e7dbf9 master -> master
Next, we'll be explaining the steps from Developer B's point of view.
Action by Developer B
Before running the pull command, let's check the commit status.
cd git_remote_practice
git log --oneline
You can see only two commits at this stage.
4e1a7d9 (HEAD -> master, origin/master, origin/HEAD) M2RA
4b30a1c M1LA
To bring the latest commit made by Developer A to Developer B's Local Repository, run the pull command.
git pull origin master
You can see that Fast-forward merge was executed.
remote: Enumerating objects: 5, done.
:
From github.com:bloovee/git_remote_practice
* branch master -> FETCH_HEAD
4e1a7d9..4e7dbf9 master -> origin/master
Updating 4e1a7d9..4e7dbf9
Fast-forward
git_remote_practice.html | 1 +
1 file changed, 1 insertion(+)
Check the commit status again.
git log --oneline
You can see that commit M3LA made by Developer A was merged with Developer B's master branch.
4e7dbf9 (HEAD -> master, origin/master, origin/HEAD) M3LA
4e1a7d9 M2RA
4b30a1c M1LA
3. Recap of this practice page
To review the summary of what we have done, you can check the illustration below.