Git Pull Force

Learn via video courses
Topics Covered

Overview

The git pull command is used to pull all the recent changes made on a remote repository onto your local repository. This operation in Git is only possible if all the locally committed changes are in sync with the changes on the remote. If there are some uncommitted code changes in the local repository, then the merge section of the git pull command will fail and the pull operation will be unsuccessful. However, for some situations, you may want to force overwrite all the local repository code changes on the remote repository. For that, git provides the --force option.

Pre-requisites

Before jumping into the concept of forcing a pull of committed code changes from the remote repository to your local repository, let's first understand how the git pull command works. This command is used to pull changes from a remote cloud-based server to your local computer. It works in two steps- first, it fetches the changes and then combines them from the remote server to the local git repository.

workflow-of-git-pull-command

As shown in the image above, the git pull command fetches the changes from the remote and then combined them with the changes of the local repository. This pulling concept is very useful to access the latest and most recent changes from the remote server to your local repo when multiple persons are working on the same project. The syntax of the git pull command is given below:

The command used to pull the changes from a new branch of a remote repo in Git is:

What is Git Pull --force?

The Git pull force technique allows you to update your local repository with the latest changes of the remote repo even if you have some leftover uncommitted commits local. This force-pulling concept is different from the normal Git pull process. Because in the usual git pull process, you can only pull the remote data into a clean (having all the committed code changes) local repository without any uncommitted changes. If untracked commits are found in the local repository during the git pull operation, then git will ask you to commit those changes before performing the merge

error: Your local changes to the following files would be overwritten by merge.

When working in a shared repository with a team, you might face this kind of error message when trying to perform a git pull operation in your local repository without committing the local changes. The reason for showing this error message is that you have some local changes that are still not committed and so git warns you that either you should commit those changes or they would be overwritten by the incoming new remote repo changes. This will happen in the merge part of the git pull operation.

The working of the git force command is also the same as the working of the git pull command, firstly, it fetches all the data from the remote server forcefully and does the merge operation. Here, the merge is not done forcefully (git pull --force = git fetch --force + git merge). We will see this in detail in the upcoming sections of this article.

Why Would You Want to Git Pull Force?

There may arise situations where you want to force overwrite all the code changes of the local repo to match the commit history on the remote repo.

For example,, suppose the commit history of the remote repo has changed very significantly in recent times as compared to the history of the local repo. So, in this case, simply performing a git pull operation can lead to conflicts and can cause a very confusing commit history, or both. On the other hand, if you found code changes of your local repository irrelevant then in that case you can simply force pull the content of the remote repo.

That means if you don't want your local changes to be there and you don't care about the commit history of your local repository then you should do a git pull force.

Git Pull Force Workflow

Let's see the complete workflow of the git pull force command:

1. Overwrite the Uncommitted Changes Forcefully from The Local Repository

This step will work on deleting any un-committed changes from your local repository, but if you want to save and store your un-committed changes, then please follow the second case of this git pull force workflow section.

  • Switch to the branch if you are not already there:
  • verify the current branch you are on:
  • Take the backup of your branch:
  • Fetch the refs and tags from the remote repo:
  • Reset the position of HEAD to the fetched reference:
  • Perform the delete operation on any untracked files or folders:
  • Perform the git pull operation:
  • Now delete the backup branch:

2. Stash or Save the Uncommitted Changes of The Local Repository Changes and Then Perform the Git Pull Force Operation

  • Switch to the branch if you are not already there:
  • Verify the current branch you are on:
  • Take the backup of your branch:
  • Stash the uncommitted changes in your local repository
  • Verify your stashed list
  • Fetch the refs and tags from the remote repo:
  • Reset the position of HEAD to the fetched reference:
  • Perform the delete operation on any untracked files or folders:
  • Perform the git pull operation:
  • Re-apply the stashed changes back:
  • Check the status:
  • Now delete the backup branch:

How Do You Git Pull Force in The Command Line?

Firstly, run a git fetch operation to download all the latest changes of the remote repo to your local machine:

git-fetch-command

Next, you'll need to find the commit ID of the latest commit in the remote repo that you want to pull changes from.

git-rev-parse-command)

Use of the git rev-parse command is done here because it is much faster than using the git log <remote-name> command or the git log <branch-name> command.

Then use the git reset --hard command is done to reset the master branch of the local repo to the remote branch you pulled the data from. You will need to run this command using the remote current commit id obtained in the step above. In this way, you can reset your local repo with the changes of the remote repo.

For higher visibility, you can also verify if both of your local and remote branches point to the same last commit with the following command below:

If both branches point to the same commit, then you'll get a clean output.

git-diff-branch-name-remote-name

If both the branches do not point to the same commit, then you'll get a clean output, then you'll see:

images/git-diff-branch-name-remote-name-don%27t-point-same-commit

How to Force Git Pull to Overwrite Local Files?

Before looking at how to force git pull to overwrite local files, you must first remotely download the latest code changes to your local machine without merging or rebasing. The command to fetch the remote changes is:

It will simply fetch all the latest code changes from all the branches of all remotes to your local branch of the repository. It will just download all the data to the local machine and the existing local code will not get overwritten as we have not performed the merge operation yet.

Then, use the git reset command to reset the local branch with the one you just pulled from the remote server. However, this command requires the --hard option to forcefully change all the files in the local working directory with those in the remote server.

Cleaning Up the Working Copy

Firstly, you need to make sure that your local working directory does not contain any uncommitted changes that can cause any code conflicts. There are two ways to clean up the Working Copy:

a) Stash the local uncommitted changes:

If you want to store your local changes and don't want to lose them, then you can safely store them in a stash and then re-apply them whenever needed from your local working directory. They will remain available to you if you want to use them later at any point in time. The command used to stash the changes is:

b) Permanently remove/discard the local uncommitted changes:

If you're sure that you don't need the local uncommitted changes anymore, then you can permanently remove them from your local repo using the below command:

If you also have some of the untracked files, then you should use the clean command of git to erase them as well.

You should be careful with the following commands as they will permanently remove the local changes and will erase the untracked files. But you should keep in mind, this operation is irreversible and we can't revert it back.

Pull Again

After cleaning up any local uncommitted code changes that may have been overwritten, the pull operation will now work properly. The command for pulling the changes is:

Conclusion

  • The git pull command is used to pull all the recent changes made on a remote repository onto your local repository.
  • If there are some left uncommitted code changes in the local repository, then the merge section of the git pull command after the fetch part will fail and the git pull operation will be aborted.
  • The git pull command fetches the changes from the remote and combined them with the changes of the local repository.
  • The Git pull force technique allows you to update your local repository with remote content even if you have some leftover uncommitted commits
  • If you don't care about the commit history of your local repo, then you should do a git pull force operation.
  • Git pull command first fetches the changes and then combines them from the remote server to the local working directory of git. (git pull = git fetch + git merge).
  • Git force pull command first fetches the data from the remote server forcefully and then merges them forcefully with the local changes (git pull --force = git fetch --force + git merge).
  • To clean up the Working directory, you can either stash the local uncommitted changes or permanently discard them.
  • After cleaning up any local uncommitted code changes that may have been overwritten, the pull operation finally works.
  • git fetch --all command is used to fetch all the latest code changes from all the branches of all remotes.
  • git reset --hard origin/master command will reset the master branch to the one you just pulled.
  • The git rev-parse command here because it is faster than running the git log <remote-name> command or the git log <branch-name> command.
  • If you want to store your local changes and don't want to lose them, then you can safely store them in a stash and then re-apply them whenever needed from your local working directory.
  • The git reset command to reset the local branch with the one you just pulled from the remote server