git pull command examples [5 Methods]


GIT

Related Searches: git pull remote branch, git pull branch, git pull command, what does git pull do, pull remote branch, how to pull, git pull remote branch to local branch, git pull remote, git pull example, git pull merge, git pull upstream, git pull remote branch example, git pull a remote branch, git pull remote branch to local, git pull local branch, how to use git pull, git pull new branch, git pull commit, pull branch from remote, how to pull git repository, git pull command line, git pull new branch from remote, how to pull remote branch, git pull syntax, git pull and merge

 

Introduction to git pull command

Git pull is the process of fetching and merging committed changes from a remote project to your local server. To pull in git implies accepting data for your local workstation from your Github account. Git employs the use of git pull command to get a remote repository to a local working environment.

When collaborating with other programmers, a pull request serves to inform the rest of the team about the completion of a particular feature. The notification enables the rest of the team to review the new codes and possibly merge them into the main branch once satisfied. Git allows you to make the pull request on a collaborated remote project.

In this tutorial, we are going to explain how to work with git pull command, git pull methods and how to make a pull request.

 

How does a git pull command function?

The git pull command is basically the sum of two other git commands, git fetch and git merge. The git pull command is used to pull the remote modifications to the local repository.

A pull command activates the pull fetch command directed to the active local branch with the head pointing at it. When the local branch accepts the remote content, a new merge commit is established causing the head to shift and point to the newly created commit.

You can read more about git pull at git pull vs git pull --rebase explained with examples

 

git pull syntax

Following is the syntax to use git pull:

$ git pull <option> [<repository URL><refspec>...]

Syntax breakdown:

  • <options>: these are alternatives that can be included in a pull command such as  --edit, -q(--quite), -v(--verbose) among others. Follow official documentation to get the complete list of supported options.
  • <repository URL>: it indicates your remote URL from git hub containing the original project. Example: https://github.com/git-testing/new-git-test.git
  • <refspec>: indicates the referenced commit such as tags, branches and remote repositories

 

Git pull workflow

Below is a diagrammatic presentation of the git pull function. Generally, the pull command originates from the remote repo to establish a connection with the local repository.

git pull command examples [5 Methods]

 

Lab Environment

To effectively practice how git pull works, it is vital to set up your local workstation. I will create a remote project git-trial and clone it to my local workstation windows 10 pro. We will use git-trial remote project to run different git pull methods.

$ git clone https://github.com/git-testing/git-trial.git

Sample Output:

git pull command examples [5 Methods]

Now that we have a local workstation ready, let’s move forward and practice using different methods of git pull.

 

Different methods to perform git pull

In this section, we are going to demonstrate how to work with the git pull command using examples.

 

Method-1: git pull remote branch (default)

It is possible to successfully run a git pull command without an option or repository specification provided in the command. How is that possible?  We will use an example to illustrate how that works out as follows:

First, we will create a branch trial-1 on our local workstation:

$ git checkout -b trial-1
Switched to a new branch 'trial-1'

Next, we will then push it upstream to create a remote copy of the same branch on the repository as follows;

$ git push --set-upstream origin trial-1

Sample output

git pull command examples [5 Methods]

We now have the trial-1 branch as a remote branch as shown in the output. Now any other user in the team who needs to work on the same branch can simply perform git pull (assuming they already have cloned the repo). We will add a file test-5.css and have the new changes downloaded into our local repository.

Sample output

$ git pull
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 2 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (2/2), 663 bytes | 34.00 KiB/s, done.
From https://github.com/git-testing/git-trial
   d335682..9a80fcd  trial-1    -> origin/trial-1
Updating d335682..9a80fcd
Fast-forward
 test-5.css | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 test-5.css

The result displayed shows that the git pull command was a success. You can see the branch trial-1 with all the latest changes downloaded. The head as expected will be pointing to the last commit.  Let’s run the git log --online command to see that in action.

$ git log --oneline
9a80fcd (HEAD -> trial-1, origin/trial-1) Create test-5.css
d335682 (master) Create README.md
4a574da Create test-4.txt

 

Method-2: git pull remote branch using URL

You can also raise a pull request using a project URL and specifying the branch name with following syntax:

git pull <REPO_URL> <BRANCH>

Get your project URL by clicking on the code button in green as displayed below:

git pull command examples [5 Methods]

Let’s create the new branch git-demo under our git-trial project which we had cloned earlier. We already have a branch with the same name on our git-trial project on the remote repository:

$ git checkout -b git-demo
Switched to a new branch 'git-demo'

Now assuming this branch is also used by other developers, we want to pull the latest changes. So we can use following command:

$ git pull https://github.com/git-testing/git-trial.git git-demo
From https://github.com/git-testing/git-trial
 * branch            git-demo       -> FETCH_HEAD
Updating d335682..28e109e
Fast-forward
 trials-1.txt | 1 +
 trials.css   | 1 +
 2 files changed, 2 insertions(+)
 create mode 100644 trials-1.txt
 create mode 100644 trials.css

So now we have pulled all the changes from git-demo branch of the git-trial repository.

 

Method-3: git force pull remote branch

Git force pull command downloads the remote content to the local server despite any conflicts in the procedure of fetching and merging.

Assume that you are working with another developer on a collaborated project. You make changes through your local repo and the other developer also runs some changes on the remote repository. During your time to fetch the repo, you encounter conflict as a result. We will use this scenario to demonstrate how git force pull command works.

In this example, Maureen and J-Mulombi are working as a team on the git-trial project. While working J-Mulombi added and committed a remote file 3.css as illustrated.

git pull command examples [5 Methods]

Without knowing the changes that her team member committed in the project, Maureen creates a file newfile.css in her local copy of git-demo branch but doesn’t commit it.

$ touch newfile.css

Next, Maureen decides to run the git pull command to update local repo before she can commit her changes but she encounters an error as illustrated:

$ git pull

Sample output

$ git pull
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 2 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (2/2), 609 bytes | 32.00 KiB/s, done.
From https://github.com/Maureen-Mulombi/git-trial
   43acc58..675156f  trial-1    -> origin/trial-1
error: Your local changes to the following files would be overwritten by merge:
  newfile.css

At this stage, Maureen decides to use git force pull command to forcefully get the latest changes downloaded to the local remote without raising a conflict.

The first step is to run git fetch all:

$ git fetch --all

Sample Output:

git pull command examples [5 Methods]

By applying the git fetch –all command, you can download all the recent remote content without merging. Meaning you skip the merging step required by basic git pull command.

Next, run the git reset –-hard git-demo command. git reset –-hard function will force the active local repository to adopt all content received from the remote repo.

$ git reset --hard git-demo
$ git reset --hard git-demo
HEAD is now at 218a0cb Merge branch 'git-demo' of 
https://github.com/Maureen-Mulombi/git-trial into git-demo

We now have the HEAD pointing to the latest commit and obviously in the git-demo branch. Lastly, you will run the git pull origin git-demo –-force to forcefully have all the remote changes updated to the local server as follows:

$ git pull origin git-demo --force

Sample Output:

git pull command examples [5 Methods]

From the output, you can see all the files found in the remote repo and local branch git-demo displayed.

 

Method-4: Use a pull request

We earlier explained what a pull request is and its importance while collaborating with others on a project. In this section, we will demonstrate how to run a pull request.

For you to initiate a pull request, first you will need to create a branch and then create a file under create new file option in your Github account as shown by the screenshot;

git pull command examples [5 Methods]

Next, name your file e.g. file.txt, scroll down the page where you have two options on how to commit as shown below.

git pull command examples [5 Methods]

Choose the second option which permits you to start a pull request.

In a git environment, you have the flexibility to send a pull request at any stage of development whether in a commit or branch.

It’s during a pull request that you effectively communicate with your team member. You may communicate details about your changes, assign new contributors and perform @mentions team members. This depends on your role and how you have scheduled responsibilities with everyone else in the team.

The commits you make during a pull request are usually displayed chronologically under the file changed option in the pull request interface. That allows for the rest of the collaborators to give feedback through comments and they can also add commits at that stage of discussions.

Once you feel satisfied with the contributions for your pull request from the team, you can finally merge them in the pull request.

 

Method-5: Define origin of remote branch with git pull

git pull <remote> <branch> command when performed, data history from the remote repository overwrites that of the local repository. Origin represents the location where the remote repository is stored. Master is the primary branch of a project both remote and local. Let's use an example to understand how git pull origin operates.

First switch to the branch where you plan to pull the remote repo changes. Since we intend to pull the changes from master branch, we will switch to master branch:

$ git checkout master

Using the git-trial project, we will perform a pull origin master command as follows:

$ git pull origin master
From https://github.com/Maureen-Mulombi/git-trial
 * branch            master     -> FETCH_HEAD
Updating 28e109e..ef2be15
Fast-forward
 now.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 now.txt

Run git remote -v in case you want to see the location of your remote project as follows;

$ git remote -v
origin  https://github.com/Maureen-Mulombi/git-trial.git (fetch)
origin  https://github.com/Maureen-Mulombi/git-trial.git (push)

 

Summary

We have been able to cover the following topics in this tutorial about the git pull command:

  • What is git pull
  • How does a git pull command function
  • Different git pull methods
  • How to make a pull request

 

Further reading

git-pull-git, pull-request

 

Deepak Prasad

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment