The git pull force command, used to overwrite and replaces the changes in the local branch with the changes of the online branch, the refs should be added to the git tree by the commit
Table Of Contents
Git is a repository and a remote control system for tracking all project source code updates and changes. It helps project managers to store and track all changes with commits so they can go back to any change that already happened before.
Actually, when executing the git pull command, there are two git commands happening behind the scene
- Git Fetch
- Git Merge
But there is no forcing in git pull here, so how to clean our local branch to be overwritten without any problem.
Git Pull Problem Overview
In the next image, you will see a team has four developers, who are working together on one project, and they are using the git remote control system. The last modifications were added by the local branch number #3 into the main online branch.
The developer who works on the local branch number #4 already completed his task without downloading the last modification of developer number #3. And knew there is new modifications already added before.
But when he uses the
git pull command it shows him the following error.
error: Your local changes to the following files would be overwritten by merge:
error: Untracked working tree file 'pc-number-4' would be overwritten by merge
That means the git remote control system already fetched the new modifications of user number #3. But it didn’t merge it to the local branch of user number #4 and it shows him this error message.
The reason for that error, is there are already new updates on the track of local branch number #3 has to overwrite
To solve this problem we have more than a way to git force pull on the track.
Solution 1: Git Pull Force to Overwrite The Local Branch
In this solution, you have to commit the local branch on the track. And then run the git pull command again.
It will show you the following required inputs. Which is to specify the “commit” of why you will merge the modification into the local branch.
The next step closing it by writing “:wq” and hitting enter.
It will show you the following message
In this way, there are two ways to solve this problem,
First, you have to store and save the changes of the local branch on the stash. and that can be done by using the following command which allows you back to it any time in the future.
git stash --include-untracked
Discard the latest changes and clean up completely then write the pull command again using the following
git reset --hard git clean -fd
And Then use git pull again