Git - Get up to date with Git Pull

This article covers how to use the command 'Git Pull'

Rarely is software written by a single developer. When there is more than one developer, you may find yourself in the situation where you've tried to push, only to get the following response:

Pushing to me@github.com:my-project/my-repo.git
To me@github.com:my-project/my-repo.git
  ! [rejected]        main -> main (non-fast-forward)
error: failed to push some refs to 'git@github.com:my-project/my-repo.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

All this means is that one or more commits have been pushed to the same branch, since you last checked it out.

If I make some changes and push a commit on Friday, and another (enthusiastic) developer pushes some changes over the weekend, my local version of the branch will be out of date when I resume work on Monday.

And to fix this, all I need to do is pull the latest changes:

git pull

This will merge all of the changes in the remote (on that branch), with your local state.

You can also pull down a specific branch from a specific remote, for instance:

git pull <remote-name> <branch-name>

When pulling the branch, git will also be performing a git fetch (to get the latest repository updates), and a git merge (to combine your local and remote branches) behind the scenes.

You can reduce the amount of information that you get back from these steps, by using the --quiet parameter:

git pull --quiet <remote-name> <branch-name>
# Or simply
git pull -q <remote-name> <branch-name>

If you have changes locally that aren't in the remote branch, and you attempt to pull, you may find yourself in the middle of a merge conflict.

In this instance, it will be easier to store your changes first, perform the pull, and then reapply them to deal with the conflict separately.

To do so, simply stash them:

# Stash my local changes
git stash save "My optional message"

# Get the latest remote changes
git pull

# Put my local changes back, and delete the stash
git stash pop