[Solved] git Error: error: Your local changes to the following files would be overwritten by merge

A git pull error, error: Your local changes to the following files would be overwritten by merge, occurs because changes to the local branch are not saved when git pulls.

There are two general solutions. There is also a special case, that is, the change of permissions such as file reading and writing.

Method 1: discard local changes

If the local modification is not important, you can directly discard the local modification:

# Discard all local uncommitted changes
git checkout .

Some local files are newly added but not Add, the status in git status is untrack , and they need to be deleted through git clean

# First check what files will be deleted
git clean -nxdf

# Make sure the files that will be deleted are correct, then execute the delete
git clean -xdf

# You can also delete files one by one, for example, delete file xxx
git clean -f xxx

Or

Direct execution:

git reset --hard
git pull

Note: discarding local files is a dangerous operation and must be considered before deleting.

Method 2: temporarily store to the stack area

If local modifications are important. If it needs to be used later, the current modification can be temporarily stored in the stack area:

# Staging to the stack area
git stash

# View stash contents
git stash list

To use local modification, apply the stash content to the local branch:

git stash pop

The contents in stash are popped up. If multiple temporary contents are saved, the pop-up order is first in and last out (stack).

If you do not want to pop up the content, but still apply the stash content to the local branch:

git stash apply

In this way, the contents in stash will not be ejected.

In addition, you can manually delete the content in stash

# Delete the contents of a specified stash, the name of which can be obtained from the git stash list
git stash drop xxx
# Delete all stash content
git stash clear

Generally:

 git stash
 git commit
 git stash pop

Git stash: back up the contents of the current workspace, read the relevant contents from the latest submission, and ensure that the workspace is consistent with the last submission. At the same time, save the current workspace content to the GIT stack
git stash Pop: read the last saved content from the GIT stack and recover the relevant content of the workspace. Since there may be multiple stash contents, it is managed by stack. Pop will read the contents from the latest stash and recover them
git stack list: displays all backups in the GIT stack. You can use this list to decide where to recover
git stack clear: clear the GIT stack. At this time, graphical tools such as gitg will be used to find out which nodes of the original stash have disappeared.

Note: merge after using git stash to temporarily store content, and then git stash pop, branch conflict may be reported. At this time, you can create a new branch locally and recover the stash content on the new branch.

exceptional case:

Git prompts that the file has been modified, but the actual content has not been modified. The reason is that all git libraries will ignore the change of FileMode.

Git diff prompts the change of FileMode, as follows:

git diff xxx
diff --git a/xxx b/xxx
old mode 100755
new mode 100644

Solution:

#Terminal Execution 
git config --add core.filemode false

Read More: