Git: Force Pull
Git prevents you from pulling files to your local machine if any unsaved or untracked changes would be
overwritten by the merge operation
. You can use the force pull method to force Git to pull the changes you want to receive on your local computer.
In this guide, we discuss, with reference to an example, how to force pull the contents of a Git repository. We’ll talk about the git pull and fetch commands, and how to use the reset option to force a pull.
What is Pulling?
Git pull operation
downloads all the metadata and files from a remote repository and updates the local repository based on the data that has been retrieved.
Pulling is the method used to bring your local files in line with the remote version of a repository. You’ll have to pull a repository if you want to download the changes someone else has made to a remote repository. Pulling does not happen automatically.
Pulling cannot occur when you have
that would be overwritten. This means you have a file on your local branch that has not been added to the Git repository and would be replaced if the pull occured.
You are unable to pull a repository if you have unsaved changes on your local machine that would be overwritten by the pull.
Git: Force Pull
We’re working on a repository called ck-git, which contains the code for a web project. A collaborator has just updated a file called README.md. We have updated that file on our machine and have not pushed our changes to the repository.
Let’s see what happens when we try to pull our changes:
This command returns an error:
error: Your local changes to the following files would be overwritten by merge: README.md Please, commit your changes or stash them before you can merge.
We have three options. We can commit our changes, stash them, or force Git to pull the changes anyway. These will overwrite our files.
We can force Git to pull the changes by fetching any changes that have been made and then resetting our repository to show those changes. Let’s start by fetching the changes using the
git fetch command
git fetch --all
This command retrieves all of the metadata for the changes made to our remote repository. Next, we’re going to back up our current branch. We do this to make sure we do not lose our work once our code is overwritten.
We can back up our branch using the
git branch command
git branch backup
This command creates a new branch called “backup”. Now that we have our backup branch in place, we can reset our repository to show the contents of our remote repository.
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot
We’re going to use the
git reset command
to update our repository:
git reset --hard origin/master
The –hard option downloads all the files from the remote repository and adds them to your local working copy of the project. This will overwrite any changes you have made.
The origin/master statement refers to the branch we are retrieving. We are downloading the contents of the “master” branch from our “origin” remote repository.
If you are working with a repository that has a different branch or remote name, you can use this formula to guide how you use the reset command:
git reset --hard <remote>/<branch>
The git reset command resets our repository to the most recent commit that we fetched.
Saving Uncommitted Changes
You can save the uncommitted changes that you have made using the git stash command. This command lets you “stash” your code away from later. Let’s create a stash of our code:
All of the changes we have made to our repository will be saved in the stash. When we are ready to work with those changes again, we can “pop” our stash:
git stash pop
You can read more about saving with git stash in our git stash tutorial.
You can force a Git repository to pull contents from a remote repository. To do this, you need to fetch the contents of the repository. Once you have fetched the repository, you can reset your changes to the branch on your remote repository that you want your codebase to use.
Now you have the knowledge you need to force a Git pull operation like a pro!