How do I ignore an error on 'git pull' about my local changes would be overwritten by merge?

1,152,227

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:

  1. Commit everything you don't want to overwrite and use the method above for the rest.

  2. Use git checkout path/to/file/to/revert for the changes you wish to overwrite. Make sure that file is not staged via git 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
Share:
1,152,227
mae
Author by

mae

Updated on July 08, 2022

Comments

  • mae
    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
    mae over 11 years
    Possibility #2 does not work. After executing the command nothing happens. On pull I still get the same error.
  • Daniel Hilgarth
    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
    mae over 11 years
    The trick was to use git checkout HEAD^ path/to/file/to/revert. Using HEAD^ made all the difference.
  • Daniel Hilgarth
    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
    cdcdcd over 10 years
    This worked for me. Could you expand on this answer, ie. what is this actually doing?
  • k3a
    k3a over 10 years
    It's dropping local changes, reverting to the HEAD reference which is probably the last commit in the master branch
  • Micah Walter
    Micah Walter over 9 years
    What 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
    Yura over 9 years
    why HEAD^ instead of HEAD?
  • DanielSank
    DanielSank over 9 years
    Annoyingly, 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
    jpmc26 about 9 years
    I don't have any unstashed local changes and I still got this error.
  • davidneedham
    davidneedham almost 9 years
    HEAD^ 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
    Jon about 8 years
    Point 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
    endolith about 8 years
    please explain what this does in the answer
  • Suneel Kumar
    Suneel Kumar over 7 years
    @Jon This solution is for Ubuntu, I have not found anything better then this.
  • Suneel Kumar
    Suneel Kumar over 7 years
    @pabloasc This is destroy your local changes in that file.
  • David
    David over 7 years
    Yes it does: "What if I want to overwrite them?"
  • Admin
    Admin about 7 years
    You can then use the Git diff - w + name of the file to confirm the code merge automatically
  • Suneel Kumar
    Suneel Kumar over 6 years
    Original question is not able this, this answer can give someone nightmare who can blindly copy paste the command.
  • Faito
    Faito over 6 years
    This 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
    Stephen O'Flynn over 6 years
    I 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
    JonBrave over 6 years
    After 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
    theRiley about 6 years
    this 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
    Dan Pisarski about 6 years
    When your stuck due to line endings, this method is a life saver
  • Benedict K.
    Benedict K. about 6 years
    SO needs to work on their ranking algo, cumbersome to find a working, highly rated answer so far down the thread.
  • Kittsil
    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
    Peter Mortensen about 6 years
    I had to leave out "save --keep-index".
  • Ender
    Ender almost 6 years
    Works just fine for me
  • Eugen Konkov
    Eugen Konkov almost 6 years
    Unfortunately --autostash option is available only with --rebase option (
  • HonoredMule
    HonoredMule almost 6 years
    One does not use axe to remove fly from friend's forehead.
  • IgorGanapolsky
    IgorGanapolsky over 5 years
    I still get an error: Please commit your changes or stash them before you merge. Aborting
  • dopexxx
    dopexxx over 5 years
    In case you staged a commit already, you first have to revert the commit via git reset HEAD~ and then do the git checkout
  • andromeda
    andromeda over 5 years
    DO NOT use this without the awareness that some files will be DELETED.
  • A__
    A__ over 5 years
    git checkout -- <file> fails with error: pathspec '<file>' did not match any file(s) known to git.
  • Philip Rego
    Philip Rego almost 5 years
    Make it just not overwrite the changes.
  • Philip Rego
    Philip Rego almost 5 years
    thats 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ś
    Dr_Zaszuś over 4 years
    The 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
    mike rodent about 4 years
    Why do you add save --keep-index? I went git 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
    Daniel Hilgarth about 4 years
    @mikerodent: I have taken your comment as an opportunity to update the answer. Please check.
  • Daniel Hilgarth
    Daniel Hilgarth about 4 years
    @mikerodent: Thanks for the hint. I missed push. Fixed.
  • mike rodent
    mike rodent about 4 years
    It 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
    PRMan about 4 years
    This isn't working for me. The files don't exist locally and I am still getting the error.
  • Eugen Konkov
    Eugen Konkov almost 4 years
    Hm... Dangerous advice. After stash and drop you will loose your local changes =(
  • user1568901
    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
    oraclesoon over 3 years
    I 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
    sf_jeff over 3 years
    The 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
    invinciblemuffi over 3 years
    Works perfect. Saved my day. Thank you.
  • mikey
    mikey over 3 years
    This is the easiest solution if you just want to start over and make your git pull work.
  • Ashok Ogirala
    Ashok Ogirala over 3 years
    After you ran git commit command doesn't it ask for git push?
  • Riley Hemphill
    Riley Hemphill over 3 years
    git reset --hard origin/master is the only thing that worked, out of all these solutions. Thank you.
  • Timo
    Timo over 3 years
    @MicahWalter did you find a solution, got the same issue.
  • DanielSank
    DanielSank over 3 years
    @Timo I do not remember, it was seven years ago.
  • Micah Walter
    Micah Walter over 3 years
    @Timo I don't remember one, I'm sorry :(
  • Timo
    Timo over 3 years
    Is 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
    learning2learn about 3 years
    Nicely done! Can you take out the "TLDR" you have at the top? Perhaps "This is a better answer - PLEASE read."
  • Eugen Konkov
    Eugen Konkov about 3 years
    @learning2learn: May you please describe why you dislike TLDR?
  • learning2learn
    learning2learn about 3 years
    Because 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
    andrew lorien about 3 years
    this even worked for a git merge master, when none of the other suggestions did
  • itay_alon
    itay_alon almost 3 years
    i do the same but it seems wierd to me that this common issue does not have a designated command
  • Lenka Čížková
    Lenka Čížková almost 3 years
    Thank 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
    jim smith almost 3 years
    imagine if you didn't read that to the end!
  • user1568901
    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
    Lukas Coorek over 2 years
    In my case it, didn't work. But: git checkout HEAD -- my-file.txt worked for me.
  • Agile Bean
    Agile Bean over 2 years
    For 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
    Fernando Torres over 2 years
    error: 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
    troy over 2 years
    Great answer, helped me out, I was completely lost.
  • jakebugz617
    jakebugz617 over 2 years
    I 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
    Saad over 2 years
    This answer is unique and very important
  • Clint Angelo 'Tec' Mercado
    Clint Angelo 'Tec' Mercado over 2 years
    What 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
    lcompare about 2 years
    This 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 be git pull.
  • cbishop
    cbishop almost 2 years
    True 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
    Caverman almost 2 years
    Fumbled 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.