In the last tutorial, we got familiar with the Git fetch and Git merge command. Both of being highly prevalent in Git, they are used very frequently. Git fetch, and Git merge are used together for merging the changes and accepting them. The problem is that if the user is using Git fetch ten times in a day and all of the changes have to merge, the git merge also gets used ten times. Is there something that can combine both of these processes? Yes, there sure is. Git fetch and Git merge commands are so commonly used that Git has a special command that combines both of these commands into one command called Git Pull command. This tutorial is all about:
- What Is Git Pull?
- How To Use Git Pull?
- When To Use Git Pull?
- What different options are available In Git Pull?
What is Git Pull?
Git pull is a magical way to perform a combined operation of git-fetch & git-merge with a single command. "Pull", which is self-explanatory, depicts that the user is trying to fetch something from the repository. In a way, "fetch" is not the right word because we already discussed git fetch command. If you think that fetching is all that Git Pull does, then why aren't we satisfied by Git Fetch?
Here's the magic that I was talking about at the start. Git Pull will perform Git Fetch without telling the user and merge those changes automatically without asking from the user. Hard to digest? Let's see a bit more.
Executing a git pull command will merge the changes without notifying the user or displaying what changes are merging.
The user is just notified about the result of the command, whether the operation was successful or failed, including any warnings, etc. It might sound risky, but in the industry, git pull gets used very commonly. "Risky" in a way that git pull will merge even those changes that are not required or those which you do not want to merge. Remember that Git Fetch used to bring you the changes that happen on the repository? Well, Git Pull assumes that any change that has occurred in the repository requires merging.
The side-effect or precautionary arrangement that this command brings with itself is "merge-conflict". Git pull raises a warning of "merge-conflict" in Git sometimes. When does this happen, and how to resolve it?. We will see in brief in the later section.
How to Use Git Pull Command?
We can use the Git pull command by typing the following command in the Git Bash.
git pull
Notice the 2 sections I have marked in the image above.
The first section has the same output as the git fetch command (Refer Git Fetch Command), whereas the second section has the same output as the git merge command. It proves git pull is an amalgam of git fetch and git merge command, and one should use it carefully.
When To Use Git Pull?
A user might wonder, when should they use Git fetch and when they should go for Git pull command. Git fetch often considered a safer version of Git Pull, and one should use it if the user is new to Git. If the new user is confident enough, they can use git pull command only on a clean working directory (no committed changes).
For experienced users, using git fetch is often the case when someone else is also working with you on the same branch. The scenario might look like your friend will ask you to look at some changes they have done on the branch and merge if you like it. Now since you are not sure enough about the merging of the changes, you will first fetch these changes, review them, and then merge. We use Git pull when one is working alone on the branch. Since there is no point in reviewing your changes again, you can directly pull them to your repository.
Using Git pull command is no different than using Git merge command. Just keep in mind that git pull is a short cut to git fetch and git merge. It means we don't need to execute git fetch and git merge, and the changes will be incorporated directly.
Different options in Git Pull
Like any other command in Git, the pull command also boasts some quick options which help in the natural and efficient use of the command.
No-Commit option
The no-commit option will pull the changes, but the merge will not be an explicit commit, i.e., the merge commit won't be listed.
The command for the same is:
git pull --no-commit
Rebase Option
The rebase option creates a linear history of commits after merging one branch into another. In addition to this, the Git rebase option helps in a transparent workflow. Moreover, even though it involves multiple branches, it looks like a single branch with a linear workflow.
Git Branches Before Rebase[/caption]
Git Branches After Rebase[/caption]
Using the Git merge creates a messy graph if you look at all the commits at once. To achieve a clear workflow, we go ahead with Git rebase, and a project's line of development looks clean.
The command can execute as:
git pull --rebase
The drawback of using Git rebase command is that it makes the developers and testers hard to recognize the small commits and changes done in the repository as the commit history becomes linear. Open source projects often do not use 'rebase' command due to this reason, but as always, it comes to personal choice.
It concludes the tutorial. Till now, we have covered all the major commands and their usage in Git. I hope you are practicing them regularly. In the next section, we will talk about branches in Git and visualize them to understand why they are the most important aspect of Git.
Common Questions On Git Pull
Why do we commonly write git pull command as git pull origin master?
'git pull origin master' will fetch and update only a specific branch called master and origin in the remote repository. Often, the default branch in Git is a master branch, and it keeps updating frequently. A user can use any branch name to pull that branch from the remote.
Does git pull fetch all the branches?
Yes, if the command used is just 'git pull' the Git will fetch all the updated references to local branches that are tracking the remote branches.
Can I undo git pull?
Yes, we can revert the changes done by Git Pull by the git reset --hard
command. Git reset hard resets the branch to the data user just fetched while the hard option changes the file in the working tree to match the files in the branch.