Git

How to Force Git Pull to Overwrite Local Files

Git offers multiple commands to perform different operations on Git local repositories, such as creating new directories, branches, tags, making updates in projects folders/files, pull files/folders, and many more. The “$ git pull” command is utilized for downloading files and folders from the Git remote repository. It also helps to modify the local repository to check out the contents of the remote repository.

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:

$ cd "C:\Users\nazma\Git\Pull_force"

Step 2: List Repository Content

Execute the “ls” command to view the content of Git local repository:

$ ls

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:

$ start file9.txt

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:

$ git add .

Step 5: Git Fetch

To fetch the latest version of the Git remote repository to the local Git repository, run the “git fetch” command:

$ git fetch

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:

$ git reset --hard HEAD

Step 7: Pull Request

Lastly, execute the “git pull” command to merge the fetched content with local repository and overwrite the changes:

$ git pull origin master --allow-unrelated-histories

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.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.