How to Pull Repository in Git

Last updated on May 27 2022
Suyash Borole

Table of Contents

How to Pull Repository in Git

Git Pull / Pull Request

The term pull is used to receive data from GitHub. It fetches and merges changes from the remote server to your working directory. The git pull command is used to pull a repository.

devops 45
devops

Pull request is a process for a developer to notify team members that they have completed a feature. Once their feature branch is ready, the developer files a pull request via their remote server account. Pull request announces all the team members that they need to review the code and merge it into the master branch.
The below figure demonstrates how pull acts between different locations and how it is similar or dissimilar to other related commands.

devops 46
devops

The “git pull” command

The pull command is used to access the changes (commits)from a remote repository to the local repository. It updates the local branches with the remote-tracking branches. Remote tracking branches are branches that have been set up to push and pull from the remote repository. Generally, it is a collection of the fetch and merges command. First, it fetches the changes from remote and combined them with the local repository.
The syntax of the git pull command is given below:
Syntax:
1. $ git pull <option> [<repository URL><refspec>…]
In which:
<option>: Options are the commands; these commands are used as an additional option in a particular command. Options can be -q (quiet), -v (verbose), -e(edit) and more.
<repository URL>: Repository URL is your remote repository’s URL where you have stored your original repositories like GitHub or any other git service. This URL looks like:
1. https://github.com/ImDwivedi1/GitExample2.git
To access this URL, go to your account on GitHub and select the repository you want to clone. After that, click on the clone or download option from the repository menu. A new pop up window will open, select clone with https option from available options. See the below screenshot:

devops 47
devops

Copy the highlighted URL. This URL is used to Clone the repository.
<Refspec>: A ref is referred to commit, for example, head (branches), tags, and remote branches. You can check head, tags, and remote repository in .git/ref directory on your local repository. Refspec specifies and updates the refs.
How to use pull:
It is essential to understand how it works and how to use it. Let’s take an example to understand how it works and how to use it. Suppose I have added a new file say design2.css in my remote repository of project GitExample2.
To create the file first, go to create a file option given on repository sub-functions. After that, select the file name and edit the file as you want. Consider the below image.

devops 48
devops

Go to the bottom of the page, select a commit message and description of the file. Select whether you want to create a new branch or commit it directly in the master branch. Consider the below image:

devops 49
devops

Now, we have successfully committed the changes.
To pull these changes in your local repository, perform the git pull operation on your cloned repository. There are many specific options available for pull command. Let’s have a look at some of its usage.

Default git pull:

We can pull a remote repository by just using the git pull command. It’s a default option. Syntax of git pull is given below:
Syntax:
1. $ git pull
Output:

devops 50
devops

In the given output, the newly updated objects of the repository are fetched through the git pull command. It is the default version of the git pull command. It will update the newly created file design2.css file and related object in the local repository. See the below image.

devops 51
devops

As you can see in the above output, the design2.css file is added to the local repository. The git pull command is equivalent to git fetch origin head and git merge head. The head is referred to as the ref of the current branch.

Git Pull Remote Branch

Git allows fetching a particular branch. Fetching a remote branch is a similar process, as mentioned above, in git pull command. The only difference is we have to copy the URL of the particular branch we want to pull. To do so, we will select a specific branch. See the below image:

devops 52
devops

In the above screenshot, I have chosen my branch named edited to copy the URL of the edited branch. Now, I am going to pull the data from the edited branch. Below command is used to pull a remote branch:
Syntax:
1. $ git pull <remote branch URL>
Output:

devops 53
devops

In the above output, the remote branch edited has copied.

Git Force Pull

Git force pull allows for pulling your repository at any cost. Suppose the below scenario:
If you have updated any file locally and other team members updated it on the remote. So, when will you fetch the repository, it may create a conflict.
We can say force pull is used for overwriting the files. If we want to discard all the changes in the local repository, then we can overwrite it by influentially pulling it. Consider the below process to force pull a repository:
Step1: Use the git fetch command to download the latest updates from the remote without merging or rebasing.
1. $ git fetch -all
Step2: Use the git reset command to reset the master branch with updates that you fetched from remote. The hard option is used to forcefully change all the files in the local repository with a remote repository.
1. $ git reset -hard <remote>/<branch_name>
2. $ git reset-hard master
Consider the below output:

devops 54
devops

In the above output, I have updated my design2.css file and forcefully pull it into the repository.

Git Pull Origin Master

There is another way to pull the repository. We can pull the repository by using the git pull command. The syntax is given below:
1. $ git pull <options><remote>/<branchname>
2. $ git pull origin master
In the above syntax, the term origin stands for the repository location where the remote repository situated. Master is considered as the main branch of the project.
Consider the below output:

devops 55
devops

It will overwrite the existing data of the local repository with a remote repository.
You can check the remote location of your repository. To check the remote location of the repository, use the below command:
1. $ git remote -v
The given command will result in a remote location like this:
1. origin https://github.com/ImDwivedi1/GitExample2 (fetch)
2. origin https://github.com/ImDwivedi1/GitExample2 (push)
The output displays fetch and push both locations. Consider the below image:

devops 56
devops

Git Pull Request

Pull request allows you to announce a change made by you in the branch. Once a pull request is opened, you are allowed to converse and review the changes made by others. It allows reviewing commits before merging into the main branch.
Pull request is created when you committed a change in the GitHub project, and you want it to be reviewed by other members. You can commit the changes into a new branch or an existing branch.
Once you’ve created a pull request, you can push commits from your branch to add them to your existing pull request.
How to Create a Pull Request
To create a pull request, you need to create a file and commit it as a new branch. As we mentioned earlier in this topic, how to commit a file to use git pull. Select the option “create a new branch for this commit and start a pull request” from the bottom of the page. Give the name of the new branch. Select the option to propose a new file at the bottom of the page. Consider the below image.

devops 57
devops

In the above image, I have selected the required option and named the file as PullRequestDemo. Select the option to propose a new file. It will open a new page. Select the option create pull request. Consider the below image:

devops 58
devops

Now, the pull request is created by you. People can see this request. They can merge this request with the other branches by selecting a merged pull request.
So, this brings us to the end of blog. This Tecklearn ‘How to Pull Repository in Git’ blog helps you with commonly asked questions if you are looking out for a job in DevOps. If you wish to learn Git and GitHub and build a career in DevOps domain, then check out our interactive, Version control with Git and GitHub Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

Version Control with Git and GitHub

Version Control with Git and GitHub Training

About the Course

Tecklearn has specially designed this Git and GitHub Training Course to advance your skills for a successful career in this domain. The course will cover different components of Git and GitHub and how they are used in software development operations. The course consists of important concepts like: branching & merging, how to deal with conflicts, rebasing, merge strategies, Git workflows and so on. You will get an in-depth knowledge of these concepts and will be able to work on related demos. Upon completion of this online training, you will hold a solid understanding and hands-on experience with Git.

Why Should you take Git and GitHub Training?

• Average salary of Git and GitHub Professional is $85k – Indeed.com
• Amazon, Google, Facebook, Microsoft, Twitter, & many other MNC’s worldwide use Git across industries.
• According to Grand View Research, the DevOps market size is estimated to be worth $12.85 billion by 2025. DevOps professionals are highly paid and in-demand throughout industries including retail, eCommerce, finance, and technology.

What you will Learn in this Course?

Introduction to DevOps
• What is Software Development
• Software Development Life Cycle
• Why DevOps?
• What is DevOps?
• DevOps Lifecycle
• DevOps Tools
• Benefits of DevOps
• How DevOps is related to Agile Delivery
• DevOps Implementation
Git and GitHub
• What is version control
• Version Control System (VCS) Products
• Types of VCS
• What is Git
• Why Git for your organization
• Install Git
• Common commands in Git
• Working with Remote Repositories
• GitHub
• Git Installation
• Git Lifecycle
• GitHub (Push, Pull Request)
• GitHub Workflow

 

0 responses on "How to Pull Repository in Git"

Leave a Message

Your email address will not be published. Required fields are marked *