The Git Pull command fetches changes from a remote repository and adds(or merges) them with our local branches. It is a combination of two commands - Git Fetch and Git Merge. However, sometimes Git may block a pull and return an error.
error: Your local changes to the following files would be overwritten by merge: ...
error: Untracked working tree file 'file' would be overwritten by merge
The reason for these errors is that the new changes fetched by Git Pull will overwrite our existing local changes. Git will not overwrite our local changes. So, it blocks the Pull operation. What if we want to overwrite the local files? Let's learn how to force Git Pull to overwrite local files.
The first step is to back up our committed and uncommitted work. Use the Git Stash command to store your uncommitted changes. We will also use the --include-untracked option to stash untracked changes. We can retrieve the stashed changes by running the Git Stash Pop command.
git stash --include-untracked
git stash pop
We might have some newer commits on our local branch. Create a backup branch to make sure these commits are safe. Use the Git Branch command to create a new branch.
git branch <backup-branch-name>
For example, if we are on the master branch and trying to overwrite changes, create a backup-master branch.
git branch backup-master
A -- B -- C -- D <- master <- HEAD
↑
backup-master
If you are don't care about your local changes, then use the Git Reset command and Git Clean command. These commands will discard all the local uncommitted and untracked changes.
git reset --hard HEAD
git clean -fd
Next, we need to fetch the changes from the remote repository. We will use the Git Fetch command. The Git Fetch command downloads the new commits and adds them to the remote-tracking branches. It will not merge these changes into our local branch.
We need to pass the remote name(origin, in most cases) and the branch name to this command. We can also use the --all option to download changes for all the branches.
git fetch <remote-name> <branch-name>
git fetch --all # to fetch all the remotes and branches
For example, when we run the git fetch origin master, it will update the origin/master branch. The origin/master branch is a remote-tracking branch and tracks changes of the remote master branch.
The final step is to reset our local branch to match the remote branch. The Git Reset command is perfect for this purpose. We will use the --hard option with Git Reset. The --hard option will reset any local uncommitted changes. Therefore, it is essential to stash them before running this command. We need to pass the remote-tracking branch name to the command.
git reset --hard <remote-tracking-branch-name>
For example, if we are trying to reset the master branch, we will first fetch the changes to the remote-tracking branch origin/master(discussed in step 2). Then, we will run the Git Reset command from the local master branch.
git reset --hard origin/master # run from the master branch
Git provides a safety feature that prevents us from overwriting any local changes. Git returns an error whenever a Pull operation tries to overwrite any local files.
We can force Git Pull to overwrite the local files. However, one must be careful as we may lose some uncommitted or untracked changes. Always stash changes and create a backup branch. If you wish to discard local changes, then run the Git Reset and Git Clean commands.
We can first download all the remote changes by using the Git Fetch command. Next, we can reset our local branch to match the corresponding remote branch. This process will overwrite any local files.
Note that the git pull --force command may sound tempting but cannot be used for this purpose.
Another alternate approach could be to clean all the local changes(stashing or resetting) and then re-run the Git Pull command.
git reset --hard HEAD # remove all uncommitted changes
git clean -fd # remove untracked files
git pull
# ----OR----
git stash --include-untracked # stash uncommitted and untracked changes
git pull