Sometimes users may get errors while pulling the project files using the git pull command into the remote repository. In this situation, it is required to overwrite the local files.
In this blog, we will learn how to perform the Git pull operation forcefully to overwrite local files.
How to Force Git Pull to Overwrite Local Files?
To force Git pull to overwrite local files, first, move to the Git local repository and check the list of content. Then, update the existing file and add the changes to the repository using the “$ git add .” command. After that, fetch the remote repository’s latest version to the local repository by executing the “$ git fetch” command. Next, run the “$ git reset –hard HEAD” command to reset the HEAD pointer forcefully. Lastly, overwrite the local files using the “$ git pull origin –allow-unrelated-histories” command.
Now, let’s implement the above-given scenario!
Step 1: Move to Git Repository
At first, move to the local directory with the help of the “cd” command:
Step 2: List Repository Content
Execute the “ls” command to view the content of Git local repository:
As you can see, currently, we have one file in current repository:
Step 3: Update File
Next, open up the Git repository existing file using the “start” command:
Using the above command, the file will be opened with the editor. Add some text and save it:
Step 4: Track File
Now, add all the changes to the local Git repository using the provided command:
Step 5: Git Fetch
To fetch the latest version of the Git remote repository to the local Git repository, run the “git fetch” command:
According to the below output, it is stated that the Git local repository is updated with the remote repository:
Step 6: Overwrite Changes
Finally, run the “git reset” command with “–hard” option which will reset the HEAD pointer forcefully:
Step 7: Pull Request
Lastly, execute the “git pull” command to merge the fetched content with local repository and overwrite the changes:
As you can see, the added changes in local repository file are overwritten by the remote repository file:
We have illustrated the procedure to perform the Git pull operation forcefully to overwrite local files.
Conclusion
To force Git pull to overwrite local files, first, open up the Git local repository and check the list of content. Then, update the existing file and add the changes to the repository using the “$ git add .” command. Next, fetch the remote repository’s latest version. After that, run the “$ git reset –hard HEAD” command to reset the HEAD pointer forcefully, and lastly, overwrite the local files using the “$ git pull origin –allow-unrelated-histories” command. This blog discussed how to perform the Git pull operation forcefully to overwrite local files.