How do I ignore an error on 'git pull' about my local changes would be overwritten by merge?
Solution 1
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
Solution 2
If you want remove all local changes - including files that are untracked by git - from your working copy, simply stash them:
git stash push --include-untracked
If you don't need them anymore, you now can drop that stash:
git stash drop
If you don't want to stash changes that you already staged - e.g. with git add
- then add the option --keep-index
. Note however, that this will still prevent merging if those staged changes collide with the ones from upstream.
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
.
Solution 3
This works for me to override all local changes and does not require an identity:
git reset --hard
git pull
Solution 4
So many answers here that I hate to add yet another, but all of the above are clunkier than they need to be. I have to do this all the time as Git seems to become confused and says I have modified files that have not changed (can't revert because they didn't change, but I can't pull because they supposedly have changed) Simplest and fastest I've found so far is:
git stash
git stash drop
git pull
NOTICE: local changes will be lost
Solution 5
Here is a solution that throws away staged changes:
git reset file/to/overwrite
git checkout file/to/overwrite
mae
Updated on July 08, 2022Comments
-
mae almost 2 years
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.
-
mae over 11 yearsPossibility #2 does not work. After executing the command nothing happens. On pull I still get the same error.
-
Daniel Hilgarth over 11 years@user1132363: It works for me. Please test it first with a single file. Also, you have make sure that the file you want to overwrite is not staged.
-
mae over 11 yearsThe trick was to use
git checkout HEAD^ path/to/file/to/revert
. Using HEAD^ made all the difference. -
Daniel Hilgarth over 11 years@user1132363: That checks out the previous version and not the one currently checked in. I don't believe that this is the correct approach.
-
cdcdcd over 10 yearsThis worked for me. Could you expand on this answer, ie. what is this actually doing?
-
k3a over 10 yearsIt's dropping local changes, reverting to the HEAD reference which is probably the last commit in the master branch
-
Micah Walter over 9 yearsWhat if I am not able to do a stash because I have no changed files in my working copy? I get the error message despite having no local changes.
-
Yura over 9 yearswhy HEAD^ instead of HEAD?
-
DanielSank over 9 yearsAnnoyingly, if the perceived difference comes from the fact that the file had its newlines changed when it was checked out, this will not fix the problem.
-
jpmc26 about 9 yearsI don't have any unstashed local changes and I still got this error.
-
davidneedham almost 9 yearsHEAD^ is short for HEAD^1, which essentially means the one commit before HEAD. You could also do HEAD^2 for the commit before that one. For more information see git-scm.com/book/en/v2/… and stackoverflow.com/questions/1955985/….
-
Jon about 8 yearsPoint is, you shouldn't have to do this. Just take the current HEAD material and ..... merge it in! It's really simple, Git, all other VCS's do it... but nope. Linus had to make it annoying to use.
-
endolith about 8 yearsplease explain what this does in the answer
-
Suneel Kumar over 7 years@Jon This solution is for Ubuntu, I have not found anything better then this.
-
Suneel Kumar over 7 years@pabloasc This is destroy your local changes in that file.
-
David over 7 yearsYes it does: "What if I want to overwrite them?"
-
Admin about 7 yearsYou can then use the Git diff - w + name of the file to confirm the code merge automatically
-
Suneel Kumar over 6 yearsOriginal question is not able this, this answer can give someone nightmare who can blindly copy paste the command.
-
Faito over 6 yearsThis solution was only one working for me when i had a file with --assume-unchanged that was never shown in the changes to be commited so i could not stash it. Really good solution!.
-
Stephen O'Flynn over 6 yearsI had the problem mentioned because I'd updated the index to assume the files were unchanged. It still wouldn't let me do the pull. I used
git checkout -- path/*
just once, and it allowed me to execute the pull after. -
JonBrave over 6 yearsAfter doing this, it said "You are in 'detached HEAD' state." and I found myself in state where
git pull
now says "You are not currently on a branch. Please specify which branch you want to merge with.". I gave up and restored from backup where I did not have the unwanted changes. :( It really should not be this difficult. -
theRiley about 6 yearsthis is the best answer, imo, because it does not disrupt any staged items, but addresses the problem of the file preventing the pull
-
Dan Pisarski about 6 yearsWhen your stuck due to line endings, this method is a life saver
-
Benedict K. about 6 yearsSO needs to work on their ranking algo, cumbersome to find a working, highly rated answer so far down the thread.
-
Kittsil about 6 years@BenedictK. I believe that the ranking system appropriately reflects "what the most people find the most helpful." They're ranked by votes. More people prefer the other solutions. This is a good solution, but more people find the other answers more helpful.
-
Peter Mortensen about 6 yearsI had to leave out "
save --keep-index
". -
Ender almost 6 yearsWorks just fine for me
-
Eugen Konkov almost 6 yearsUnfortunately
--autostash
option is available only with--rebase
option ( -
HonoredMule almost 6 yearsOne does not use axe to remove fly from friend's forehead.
-
IgorGanapolsky over 5 yearsI still get an error: Please commit your changes or stash them before you merge. Aborting
-
dopexxx over 5 yearsIn case you staged a commit already, you first have to revert the commit via
git reset HEAD~
and then do thegit checkout
-
andromeda over 5 yearsDO NOT use this without the awareness that some files will be DELETED.
-
A__ over 5 years
git checkout -- <file>
fails witherror: pathspec '<file>' did not match any file(s) known to git.
-
Philip Rego almost 5 yearsMake it just not overwrite the changes.
-
Philip Rego almost 5 yearsthats going to introduce so many problems its not worth it. Takes 5 mins at least to load also. Also introduces "Unlink of file" errors. downvote
-
Dr_Zaszuś over 4 yearsThe manual suggests that
save
option is deprecated:This option is deprecated in favour of git stash push. It differs from "stash push" in that it cannot take pathspecs, and any non-option arguments form the message.
-
mike rodent about 4 yearsWhy do you add
save --keep-index
? I wentgit stash --help
to try and understand but I am none the wiser, particularly as the stash is due to be dropped immediately afterwards. I'd really like to understand what that's about! -
Daniel Hilgarth about 4 years@mikerodent: I have taken your comment as an opportunity to update the answer. Please check.
-
Daniel Hilgarth about 4 years@mikerodent: Thanks for the hint. I missed
push
. Fixed. -
mike rodent about 4 yearsIt wasn't a hint, I'm a very low level git person! I just looked at
git stash --help
. From that, it appears that the option--keep-index
belongs with the "sub-command" (if that's what it is)git stash save
. But this is beyond my pay grade! -
PRMan about 4 yearsThis isn't working for me. The files don't exist locally and I am still getting the error.
-
Eugen Konkov almost 4 yearsHm... Dangerous advice. After
stash and drop
you will loose your local changes =( -
user1568901 almost 4 years@EugenKonkov That's the whole point... Git sees things as local changes that aren't really changes we want to be kept and we've made no changes that need to be kept. This way you can quickly toss all local changes and then move on!
-
oraclesoon over 3 yearsI do the same. This is the simplest. Execute rm to remove the target file or mv to backup the target file to a new filename, then execute git pull.
-
sf_jeff over 3 yearsThe original question states changes should be overwritten, not committed. If this is the answer you want you need to include instructions for getting rid of the bogus commit.
-
invinciblemuffi over 3 yearsWorks perfect. Saved my day. Thank you.
-
mikey over 3 yearsThis is the easiest solution if you just want to start over and make your
git pull
work. -
Ashok Ogirala over 3 yearsAfter you ran git commit command doesn't it ask for git push?
-
Riley Hemphill over 3 yearsgit reset --hard origin/master is the only thing that worked, out of all these solutions. Thank you.
-
Timo over 3 years@MicahWalter did you find a solution, got the same issue.
-
DanielSank over 3 years@Timo I do not remember, it was seven years ago.
-
Micah Walter over 3 years@Timo I don't remember one, I'm sorry :(
-
Timo over 3 yearsIs save option similar to git stash without save? git stash pop means take the last stash from the stack, so apply it to the index.
-
learning2learn about 3 yearsNicely done! Can you take out the "TLDR" you have at the top? Perhaps "This is a better answer - PLEASE read."
-
Eugen Konkov about 3 years@learning2learn: May you please describe why you dislike TLDR?
-
learning2learn about 3 yearsBecause I think it's worth reading and to me "TL;DR" (too long; didn't read), implies it's too verbose and perhaps not worth reading. But for me of all the different answers on this question, it most closely reflected my issue at the moment, and did exactly what I was looking for. (I'd used stash before, but somehow didn't know of autostash cli option). I think your answer should get more upvotes.
-
andrew lorien about 3 yearsthis even worked for a git merge master, when none of the other suggestions did
-
itay_alon almost 3 yearsi do the same but it seems wierd to me that this common issue does not have a designated command
-
Lenka Čížková almost 3 yearsThank you very much! I've tried all the answers above but none of them worked. I haven't changed anything and still got this error for file A, and when I tried stash, I got this error with 10 other files. Repeating endlessly...
git reset --hard origin/<branch>
did help! -
jim smith almost 3 yearsimagine if you didn't read that to the end!
-
user1568901 almost 3 years@jimsmith Since tossing local changes is the whole point of this, I still struggle with that last part really being necessary at all!
-
Lukas Coorek over 2 yearsIn my case it, didn't work. But: git checkout HEAD -- my-file.txt worked for me.
-
Agile Bean over 2 yearsFor beginners, it should be added that "clean" means removal of files... If you don't want to delete the untracked files, do not do this
-
Fernando Torres over 2 yearserror: Pulling is not possible because you have unmerged files. hint: Fix them up in the work tree, and then use 'git add/rm <file>' hint: as appropriate to mark resolution and make a commit.
-
troy over 2 yearsGreat answer, helped me out, I was completely lost.
-
jakebugz617 over 2 yearsI recommend dealing with individual files. What I do in this case is first
git diff foobar.js
and then once confirmed nothing precious will be lost:git restore foobar.js
-
Saad over 2 yearsThis answer is unique and very important
-
Clint Angelo 'Tec' Mercado over 2 yearsWhat happened in me is that, I deleted the file on my local repo, and when I pull, the "about my local changes would be overwritten by merge" came up.
-
lcompare about 2 yearsThis was the only one that worked for me. Thank you!
git checkout -b newbranch
is out of scope. Question was to pull, so it would begit pull
. -
cbishop almost 2 yearsTrue for me too. Tried git checkout HEAD^ and git checkout HEAD -- and git checkout --. I'm working on my own files and never touched the files with conflicts. I didn't want to stash and then overwrite everything, just the conflicts...so I ended up deleting the file(s) and repulling.
-
Caverman almost 2 yearsFumbled around for a little bit to find an answer. Opened up in GitBash to the branch I was wanting to update and then ran these two commands. Bingo....worked like a charm.