How to overwrite local files while doing "git pull"?

In this tutorial we are going to explain how to force a git pull when there are untracked files that will be overwritten.

The error that you get when you have untracked files and will be overwritten by the pull is:

error: Untracked working tree file 'public/images/icon.gif' would be
overwritten by merge.

How to force the overwrite of local files while doing "git pull"?

Important: Using the --hard option, will delete any local commits that haven't been pushed and all local changes will also be lost.

If you have any files that are not tracked by Git , these files will not be affected.

First fetch all branches or just git fetch origin/branch_name:

git fetch --all 

git fetch downloads the latest from remote without trying to merge or rebase anything.

Then, you have several options:

git reset --hard origin/master 

If you are on some other branch:

git reset --hard origin/ 

Then the git reset resets the current branch to what you just fetched. The --hard option changes all the files in your working tree to match the files in origin/master.

git stash --include-untracked

Git stash will save all untracked files into the stash. Then you can use the command git stash list to delete specific stashed changes or just do git stash drop.