问题
How do I ignore the following error message on Git pull?
Your local changes to the following files would be overwritten by merge
What if I want to overwrite them?
I\'ve tried things like git pull -f
, but nothing works.
To be clear, I only want to overwrite specific changes, not everything.
回答1:
If you want remove all local changes from your working copy, simply stash them:
git stash save --keep-index
If you don't need them anymore, you now can drop that stash:
git stash drop
If you want to overwrite only specific parts of your local changes, there are two possibilities:
Commit everything you don't want to overwrite and use the method above for the rest.
Use
git checkout path/to/file/to/revert
for the changes you wish to overwrite. Make sure that file is not staged viagit reset HEAD path/to/file/to/revert
.
回答2:
Alright with the help of the other two answers I've come up with a direct solution:
git checkout HEAD^ file/to/overwrite
git pull
回答3:
This works for me to override all local changes and does not require an identity:
git reset --hard
git pull
回答4:
Here is a solution that throws away staged changes:
git reset file/to/overwrite
git checkout file/to/overwrite
回答5:
You can either commit your changes before you do the merge, or you stash them:
git stash save
git merge origin/master
git stash pop
回答6:
If you want to discard your local changes on one file you can do the following:
git checkout -- <file>
Then you could overwrite the file[s] with the latest version just doing:
git pull
回答7:
If your repository contains a few files which are removed from master
:
git checkout master
git fetch origin
git reset --hard origin/master
git checkout -b newbranch
回答8:
Sometimes, none of these work. Annoyingly, due to the LF thing I think, what will work is deleting the files, then pulling. Not that I recommend this solution, but if the file doesn't exist, git won't uselessly inform you that your changes (which may not even be changes) will get overridden, and will let you continue.
Use at your own risk.
回答9:
git stash save --keep-index
did not worked for me.
below command worked as expected.
git reset --hard
git pull
It override all local changes if you don't need them.
回答10:
In the recent Git, you can add -r
/--rebase
on pull
command to rebase your current branch on top of the upstream branch after fetching. The warning should disappear, but there is a risk that you'll get some conflicts which you'll need to solve.
Alternatively you can checkout different branch with force, then go back to master
again, e.g.:
git checkout origin/master -f
git checkout master -f
Then pull it again as usual:
git pull origin master
Using this method can save you time from stashing (git stash
) and potential permission issues, reseting files (git reset HEAD --hard
), removing files (git clean -fd
), etc. Also the above it's easier to remember.
回答11:
This problem is because you have made changes locally to file/s and the same file/s exists with changes in the Git repository, so before pull/push you will need stash local changes:
To overwrite local changes of a single file:
git reset file/to/overwrite
git checkout file/to/overwrite
To overwrite all the local changes (changes in all files):
git stash
git pull
git stash pop
Also this problem may be because of you are on a branch which is not merged with the master branch.
回答12:
You can use this for overwrite file
git checkout file_to_overwrite
回答13:
The best way to solve this problem is:
git checkout -- <path/file_name>
After that you can overwrite the file by:
git pull origin master
回答14:
This worked for me to discard changes on the live remote server and pull from the source control GitHub:
git reset --hard
git pull origin master
回答15:
git reset --hard && git clean -df
Caution: This will reset and delete any untracked files.
回答16:
If you want to overwrite specific changes, you need some way of telling it which ones you want to forget.
You could try selectively stashing the changes you want to abandon using git stash --patch
and then dropping that stash with git stash drop
. You can then pull in the remote changes and merge them as normal.
回答17:
Here is my strategy to solve the problem.
Problem Statement
We need to make changes in more than 10 files. We tried PULL (git pull origin master)
, but Git shouted:
error: Your local changes to the following files would be overwritten by merge: Please, commit your changes or stash them before you can merge.
We tried to execute commit
and then pull
, but they didn't work either.
Solution
We were in the dirty stage actually, because the files were in the "Staging Area" a.k.a "Index Area" and some were in the "Head Area" a.k.a "local Git directory". And we wanted to pull the changes from the server.
Check this link for information about different stages of Git in a clear manner: GIT Stages
We followed the following steps
git stash
(this made our working directory clean. Your changes are stored on the stack by Git).git pull origin master
(Pull the changes from the server)git stash apply
(Applied all the changes from stack)git commit -m 'message'
(Committed the changes)git push origin master
(Pushed the changes to the server)git stash drop
(Drop the stack)
Let's understand when and why you need stashing
If you are in the dirty state, means you are making changes in your files and then you are compelled, due to any reason, to pull or switch to another branch for some very urgent work, so at this point you can't pull or switch until you commit your change. The stash
command is here as a helping hand.
From the book ProGIT, 2nd Edition:
Often, when you’ve been working on part of your project, things are in a messy state and you want to switch branches for a bit to work on something else. The problem is, you don’t want to do a commit of half-done work just so you can get back to this point later. The answer to this issue is the git stash command. Stashing takes the dirty state of your working directory – that is, your modified tracked files and staged changes – and saves it on a stack of unfinished changes that you can reapply at any time.
回答18:
I had a special case of this: I had a file with --assume-unchanged on it. It was hard to locate, as the git status
command was not showing any changes
回答19:
If you want to keep production changes on the server, just merge into a new configuration item. The processing method is as follows:
git stash
git pull
git stash pop
Maybe you don't execute all operations. You can know what you can do next.
回答20:
I was ignoring a file in my repo and when I did git pull upstream master
I got the following error:
error: Your local changes to the following files would be overwritten by merge: myfile.js Please, commit your changes or stash them before you can merge. Aborting
To resolve it I did the following
git update-index --no-assume-unchanged myfile.js
I then did git status
and got this message
On branch master Your branch is behind 'origin/master' by 4 commits, and can be fast-forwarded. (use "git pull" to update your local branch)
Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory)
modified: myfile.js
no changes added to commit (use "git add" and/or "git commit -a")
Then I did git checkout myfile.js
followed by git pull upstream master
. This time the git pull operation was successful.
回答21:
I encountered this when pulling from the master.
The way I handled it, using Visual Studio;
- First, I performed Undo commit on my solution.
- Then I did the Git pull process.
Hope this helps!
回答22:
Error "Your local changes to the following files would be overwritten by merge" comes because you have some changes in the local repo that have NOT been commited yet, so before pulling from remote repo just commit the changes in local repo.
Lets say your remote repo has some branch xyz and you want that remote repo xyz branch to be merged into (copied to) local repo xyz branch then,
{
git checkout xyz //check out to the respective branch in local repo
git commit -m "commiting message" //commit changes if any, in local repo branch xyz
git pull //it pulls remote xyz branch into local xyz branch
}
回答23:
If this error is because of line endings,
git add
git checkout mybranch
will work. I'm not really sure why it works.
回答24:
For Pycharm, you can do Git-->Revert and then pull.
回答25:
This message can also happen if git-lfs
is used and a file pointer was overwritten by a real file.
then you use:
git stash
git lfs migrate import
git pull
full output from my case
λ git stash
Saved working directory and index state WIP on master: 5d4ad47 Merge branch 'feature/...' into 'master'
Encountered 1 file(s) that should have been pointers, but weren't:
public/apple-touch-icon.png
λ git pull
Updating 5a4ad44..b25f79d
error: Your local changes to the following files would be overwritten by merge:
public/apple-touch-icon.png
Please commit your changes or stash them before you merge.
Aborting
λ git lfs migrate import
migrate: Fetching remote refs: ..., done
migrate: Sorting commits: ..., done
migrate: Rewriting commits: 100% (0/0), done
migrate: Updating refs: ..., done
migrate: checkout: ..., done
λ git pull
Updating 5d4ad47..a25c79a
Fast-forward
public/apple-touch-icon.png | Bin 2092 -> 130 bytes
public/favicon.ico | Bin 6518 -> 1150 bytes
2 files changed, 0 insertions(+), 0 deletions(-)
see https://github.com/git-lfs/git-lfs/issues/2839
回答26:
I have tried and it successfully, before pulling, let commit all file that you have not committed, then you will not receive that messages from AS.
回答27:
I got the same error-message executed the
:PluginUpdate command from the vim editors command-line
"Please commit your changes or stash them before you merge"
1. just physically removed the folder contained the plugins form the
rm -rf ~/.vim/bundle/plugin-folder/
2. and reinstalled it form the vim commandline,
:PluginInstall!
because my ~/.vimrc contained the instructions to build the plugin to that destination:
this created the proper folder,
and got new package into that folder ~./.vim/bundle/reinstalled-plugins-folder
The "!" signs (due to the unsuccessful PluginUpdate command)
were changed
to "+" signs, and after that worked the PluginUpdate command to.
回答28:
The simplest solution is:
git reset --hard && git pull
回答29:
I'm new in git and not sure if my solution is a good idea.
I've tested ALL of answers and none of them worked for me!
But I found another solution:
1. Backup both of local and repository versions of the file.
2. Delete the file from repository.
3. git add .
4. git commit
5. git push
Hope this helps.
来源:https://stackoverflow.com/questions/14318234/how-do-i-ignore-an-error-on-git-pull-about-my-local-changes-would-be-overwritt