Git Pull: How It Works With Detailed Examples - JohnHau/mis GitHub Wiki

If you’re working alone on a project, you can use Git locally just fine. Most of the time, however, you’ll be collaborating with other people. To sync with remote repositories, you’ll need to master Git network operations, including git pull.

This post will explain git pull in detail. You’ll learn what this command does and how to use it, in addition to learning useful fundamentals about network operations in Git. As is usual for these tutorials, we assume you have Git installed and are at least familiar with working with the command line.

Git Pull 101: Understanding Git Networking What is git pull? In a nutshell, it’s the command you use to update your repository with new changes.

To better understand git pull, you need to understand how network operations work in Git.

A Brief Overview of Git Commits Unlike in other VCSs, commits in Git aren’t deltas. Instead of storing the patch—that is, the actual changes introduced—they store a whole snapshot of the project in a specific instant.

Git commits are identified by a unique-ish identifier, which is calculated based on their data, including the name and email of the author, time stamp, commit message and the commit’s parents.

Git Is Decentralized Git is a decentralized VCS, which means there’s no central server. Instead, in Git you can have any number of remote repositories, or simply remotes.

In practice, however, most projects will have a “main” remote, which acts as the de facto central server.

You Collaborate in Git by Sending to and Getting From Remotes You collaborate in Git by exchanging information with your remotes. As we’ve mentioned, you can have any number of remotes. And, despite being common for projects to have a main remote playing the role of a “central server,” you can also have other remotes configured.

For instance, you can add your coworker’s repository as a remote so both of you can collaborate. As soon as you get your remotes configured, you can send and get commits from the branches in your remotes.

How does that work in practice? For starters, you have your local branches in which you do your work. Your remote repositories also have their branches, which are appropriately called “remote branches.” The last part of this tripod is what makes the whole thing possible: remote-tracking branches.

Remote-tracking branches are references that live in your local repositories. You can think of remote-tracking branches as “read-only” branches that mirror the state of the actual remote branches.

Git Pull: Going Deeper Time to understand git pull more in-depth.

What Is Git Pull, Really? How Does It Work? Git pull, in a nutshell, is a two-part process. First, your remote-tracking branch is synced with the “true” branch in the remote repository.

Then, your local branch is compared to the remote-tracking branch and receives the new commits so it can catch up to the current state of the remote branch.

What Is the Difference Between Pull and Fetch in Git? It’s common for beginners to mix up git pull and git fetch. Despite being related, they’re two different operations. Let’s see how they’re similar and how they’re not.

Git fetch, to put it simply, is the process of updating the remote-tracking branches. When you run this command, Git fetches the new commits from the remote. After fetching, you can merge the received commits into a local branch, using git merge.

And what about git pull? You can think of this command as a shortcut: it does all of the above in a single step. In other words: git pull = git fetch + git merge.

Git Pull vs. Git Push: What’s the Difference? Another common question beginners have when it comes to git pull is how it compares with git push.

Well, they’re similar in that they’re both very common commands in Git and both are related to network operations. However, when it comes to results, they couldn’t be more different—they’re opposite operations.

Pulling means getting commits from the remote. Pushing means sending things to the remote.

image

image

image

image

image

image

Git Pull vs. Pull Request: What’s the Relationship? Before wrapping up, let’s answer another common question: What’s the relationship between the pull operation in Git and the concept of a pull request?

Well, git pull is a native command from Git. A pull request, on the other hand, is a feature from GitHub. A pull request is the event in GitHub in which a contributor to a project sends a potential contribution, asking for it to be merged to the project’s code.

Why is it called a pull request, then, instead of a merge request?

To be fair, the name of the operation is indeed a little bit confusing. The explanation is simple, though: When you send a pull request, you’re asking for the project’s maintainer to pull commits from your branch and merge them into their repository.

Conclusion Git can be used 100% offline. However, if you want to collaborate with other people, you’ll need to understand the networking aspects of the tool. So, mastering commands such as git pull and git push is crucial if you want to collaborate while using Git.

It doesn’t stop there, though. You should also learn more about branches in general—for example, how to create them, how to switch from one branch to another, how to check out a remote branch and more.

You also might want to learn about references in Git and how they work, including learning about HEAD and understanding what a detached HEAD is and how to fix it.

This post was written by Carlos Schults. Carlos is a consultant and software engineer with experience in desktop, web, and mobile development. Though his primary language is C#, he has experience with a number of languages and platforms. His main interests include automated testing, version control and code quality.