Overwriting Local Files in Git (Tutorial with Examples)

Git Overwrite Local Files

This is a tutorial on how you can force the git to fetch the remote files & overwrite the local files using the different commands. (git fetch, pull, etc.)

Overwrite Local Files with different Git Commands

There are cases when you might be trying to use the git pull command to pull everything from the remote git to your local machine but the for some reason the git is not allowing you to overwrite the local files. In such a case, there a number of different options available that you can use along with the git pull command to overwrite the local files anyway.

Important Note. Make sure that if you are using the git pull command to overwrite the local files, all of the local changes will be lost. So, make sure either you take a backup or whatsoever else accordingly in case you want to persevere the local changes as well.

Preserving the Local Changes

You may push the local changes first to a safe place or some other branch before actually using any git command to hard overwrite the local changes. At the same time, if in the local git folder, you also have the files that are not tracked by the git, they will remain unaffected. Untracked files will neither be overwritten nor their changes will be lost.

To preserve the local changes, you can simply create a new branch and then store the current git files in it and then can move back to the master or any other desired branch wherever you want the local file changes to be updated as per the remote repository.

git checkout master
git branch new-branch-to-save-local-changes

Note. The above-mentioned command will only move the committed changes to the newly created branch. All of your uncommitted changes will be lost (Even if they are staged). So, make sure you either commit all the required changes or do whatsoever appropriate you want to preserve those local changes as well.

The uncommitted changes can be stashed using the following command.

git stash

You can use the following command to re-apply the uncommitted changes according to your need.

git stash pop

Fetching Remote Git Files and Updating Local Files

Given below are the commands to try to pull everything from the remote git and to overwrite the local files of the git if any exists.

All you need to do is to run two commands one after another. First, run the following command.

git fetch --all

The above command will fetch all the latest available data from the Remote Git but it will not merge or restructure anything yet. You will need to make use of the git reset command with the --hard option to reset your current branch to whatever you just fetched using the git fetch command. The --hard option plays the real game here and makes all the changes in the local files to match your local repository to match exactly like the remote repository.

Then run any of the following two commands as per your requirement, the first one if you’re fetching everything from the master branch and the second one if you’re fetching the remote git files of a particular branch.

git reset --hard origin/master

Replace <branch_name> with the actual branch name in the following command.

git reset --hard origin/<branch_name>


The following example illustrates the entire process of fetching the remote git files to the local repository so as to also update the changes in the local files. We’ll also make sure that we move the locally committed changes to some other branch before actually fetching the latest remote repo changes to the master branch.

I’ve created a repository on GitHub just for the tutorial purposes. We’ll use the same for this tutorial as well. Let me first clone this remote git into a local folder on my PC named demo using the git clone command.

Cloning Remote Git Using Git Clone Command

This repo contains a file named git-test.txt. The remote fetched contents of this file are shown below in the screenshot.

Git Test.txt File

Now, I’ll make some changes in this file to represent the Local Changes.

Making Changes In Local Files

As you can see we’ve changed the original file text to some other text. Now, this represents our changes in the local file. Now, before using the commands to fetch the remote git files to update the local files, we’ll create another branch to preserve the local changes.

But before that, I also need to commit the changes or stash the untracked changes. I’ll prefer to commit the changes using the git commit command. You might also be needed to initialize the local repo using the git init command.

If the git is initialized locally, you can make required files trackable using the git add command and then can commit the changes using the git commit command as shown in the following screenshot.

Git Committing Changes

Then simply I’ve used the git branch command to move the local changes to a new branch.

Git Saving Local Changes To New Branch

Now, I am using the specified commands in the above section to fetch all the latest changes from the remote repo so as to also update the local changes in the master branch.

Forcing Git To Overwrite Local Changes

That’s it, the latest remote repository files are now fetched to the local repository and it also overwrote the local changes as shown in the following screenshot.

Local File Overwritten

I hope you found this guide useful. If so, do share it with others who are willing to learn about git and other technologies. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *