overwrite local changes git pull

How to Force Git Pull to Overwrite Local Changes

Sometimes you may want to force git pull to overwrite local changes. By default, git pull does not overwrite local files and will show you a warning. In this article, we will learn how to force overwrite local changes when you run git pull. Please note, in this case, you will lose all unsaved local changes on your system. Even local commits that have not been pushed will also be lost. So please be very careful before you run the following commands. Do it only if you absolutely need to.


How to Force Git Pull to Overwrite Local Changes

Here are the steps to force git pull to overwrite local changes.


1. Run git fetch command

Run git fetch command to update all branches to latest

$ git fetch --all

The above command will only download the latest commits of all branches to your local system but not merge them.


2. Backup current branch

Backup current branch.

$ git branch backup-master


3. Reset Branches

Next, do a hard reset on your master/branch.

$ git reset --hard origin/master

The above command will reset master. If you want to reset a specific branch replace <branch_name> as per your requirement.

$ git reset --hard origin/<branch_name>

The above reset command will reset all master/branch to what you just fetched. The –hard option will change all files in your working tree to match files in origin/master or origin/<branch_name>.

In this case, all uncommitted changes will be lost. If you want to retain them, then it is advisable to stash all changes before you pull.

$ git stash

Once you have pulled, you can reapply stashed changes with the following command.

$ git stash pop

In this short article, we have learnt how to overwrite local changes in git pull.

Also read:

How to Check Remote SSL Certificate in Linux
How to Enable SSL for MySQL in Windows
How to Append One File to Another in Linux
How to Rename Git Tag
How to Clone Large Git Repository

Leave a Reply

Your email address will not be published.