mae
mae

Reputation: 15664

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

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.

Upvotes: 914

Views: 1632376

Answers (30)

If you don't need your local changes and want to overwrite them with the remote version, this worked for me

git reset --hard
git pull

Upvotes: -1

Gabriel Fucci
Gabriel Fucci

Reputation: 15

In my case, I had configured a few months before not to track the file locally with

git update-index --skip-worktree <absolute path to file>

and that file changed in remote repo and confused git so i used

git stash
git update-index --no-skip-worktree <absolute path to file>
git pull origin master
git stash pop

and it worked

Upvotes: 1

Dmitry
Dmitry

Reputation: 4373

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
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)

        modified:   myfile.js ** 
        ^^^^ THIS IS KEY ^^^^^ - FILE SHOULD APPEAR MODIFIED!!

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.

Upvotes: 3

Rustam Khusainov
Rustam Khusainov

Reputation: 43

If you want to push all files, then try this git push --force-with-lease origin master

Upvotes: -4

TaQuangTu
TaQuangTu

Reputation: 2343

Before pulling, you have to commit all files that are not committed yet, then you will not receive that message from AS.

Upvotes: 1

Amol
Amol

Reputation: 115

Example

[root@localhost www]# git pull
Username for 'http://159.65.151.11': amol
Password for 'http://[email protected]':
remote: Counting objects: 34, done.
remote: Compressing objects: 100% (34/34), done.
remote: Total 34 (delta 13), reused 0 (delta 0)
Unpacking objects: 100% (34/34), done.
From http://159.65.151.11/OpenCC
   f793c8e..b8fe60c  v1.18.0_24Feb2022 -> origin/_v1.18.0_24Feb2022
error: Your local changes to the following files would be overwritten by merge:
        cc/routes/web.php
Please, commit your changes or stash them before you can merge.
Aborting

Solution(ignore already commit change in local file)

git checkout HEAD^ cc/routes/web.php
git pull

Upvotes: 3

NikeCon
NikeCon

Reputation: 87

Due to your branch is behind 'origin/dev' by xx commits, and can be fast-forwarded. Try this command:

git checkout .
git pullenter code here

Hope that fix your issue.

Upvotes: 5

Amar Kumar
Amar Kumar

Reputation: 2646

For me this worked:-

  1. First I cleaned all the untracked files, run-> git clean -f.
  2. git pull

Upvotes: -2

Eugen Konkov
Eugen Konkov

Reputation: 25282

git pull --rebase --autostash
  -r, --rebase[=false|true|merges|preserve|interactive]
      When true, rebase the current branch on top of the 
      upstream branch after fetching. If there is a 
      remote-tracking branch corresponding to the upstream
  --autostash, --no-autostash
      Before starting rebase, stash local modifications away if
      needed, and apply the stash entry when done

I do not know why this is not answered yet, but solution, as you can see is simple. All answers here suggest same: to delete/save your local changes and apply upstream, then (if you save) apply your local changes on top.

What git pull --rebase --autostash does step-by-step:

1. your local changes saved by `--autostash`
2. your local commits saved by `--rebase`
3. commits from upstream applied to your branch
4. your local commits are restored on top of upstream
5. your local changes are restored to working directory

My case (probably yours too):

I have local changes (changes at working directory):

enter image description here

When I try to pull remote changes I get error:

enter image description here

This changes do not intersect with local changes:

enter image description here

So when I pull --rebase --autostash local changes saved and applied without any problem automatically

enter image description here

Now my local changes are little lower: enter image description here

Upvotes: 65

Kasyful Anwar
Kasyful Anwar

Reputation: 402

Try this

git fetch --all 

git reset --hard origin/master

git pull origin master

It's work for me to force pull

Upvotes: 23

Brian Knoblauch
Brian Knoblauch

Reputation: 21419

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

Upvotes: 141

Divyesh Kalbhor
Divyesh Kalbhor

Reputation: 445

Check the file which you are okay to overwrite and lose local changes and then

git checkout --ours ${filePath}
git merge upstream/master

Upvotes: 0

UltraSibz
UltraSibz

Reputation: 15

Simply commit the local changes and merge commit -a

Upvotes: -3

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174477

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.

Upvotes: 676

Ali Kleit
Ali Kleit

Reputation: 3659

My solution was delete the files from outside the IDE that are going to be overwritten, then pull.

(you can always backup and manually merge untracked data)

Upvotes: 2

ASR
ASR

Reputation: 319

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
}

Upvotes: 4

Milad Safaei
Milad Safaei

Reputation: 490

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.

Upvotes: 2

Yamen Ashraf
Yamen Ashraf

Reputation: 2960

git reset --hard && git clean -df

Caution: This will reset and delete any untracked files.

Upvotes: 10

Jackkobec
Jackkobec

Reputation: 6765

The simplest solution is:

git reset --hard && git pull

Upvotes: 5

pabloasc
pabloasc

Reputation: 783

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

Upvotes: 62

Suneel Kumar
Suneel Kumar

Reputation: 5831

You can either commit your changes before you do the merge, or you stash them:

  1. git stash save
  2. git merge origin/master
  3. git stash pop

Upvotes: 85

Khemraj Sharma
Khemraj Sharma

Reputation: 59004

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.

Upvotes: 15

c33s
c33s

Reputation: 2662

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

Upvotes: 0

AJ Macapaz
AJ Macapaz

Reputation: 21

I encountered this when pulling from the master.

The way I handled it, using Visual Studio;

  1. First, I performed Undo commit on my solution.
  2. Then I did the Git pull process.

Hope this helps!

Upvotes: 2

BSB
BSB

Reputation: 2468

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.

Upvotes: 12

YanQing
YanQing

Reputation: 61

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.

Upvotes: 6

STK
STK

Reputation: 167

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.

Upvotes: 10

Gajen Sunthara
Gajen Sunthara

Reputation: 4816

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

Upvotes: 7

Nikhil K R
Nikhil K R

Reputation: 707

If your repository contains a few files which are removed from master:

  1. git checkout master
  2. git fetch origin
  3. git reset --hard origin/master
  4. git checkout -b newbranch

Upvotes: 31

Munichong
Munichong

Reputation: 4041

For Pycharm, you can do Git-->Revert and then pull.

Upvotes: 0

Related Questions