92

I've tried both

git update-index --assume-unchanged config/myconfig

and

editing .git/info/exclude and adding config/myconfig

however when I do git pull I always get:

Updating 0156abc..1cfd6a5 error: Your local changes to the following files would be overwritten by merge: config/myconfig Please, commit your changes or stash them before you can merge. Aborting

What am I missing?

toniedzwiedz
  • 17,895
  • 9
  • 86
  • 131
user2395365
  • 1,991
  • 4
  • 18
  • 34
  • possible duplicate of [Force Git to overwrite local files on pull](http://stackoverflow.com/questions/1125968/force-git-to-overwrite-local-files-on-pull) – bperson Jul 27 '14 at 17:51

6 Answers6

133

git pull wants you to either remove or save your current work so that the merge it triggers doesn't cause conflicts with your uncommitted work. Note that you should only need to remove/save untracked files if the changes you're pulling create files in the same locations as your local uncommitted files.

Remove your uncommitted changes

Tracked files

git checkout -f

Untracked files

git clean -fd

Save your changes for later

Tracked files

git stash

Tracked files and untracked files

git stash -u

Reapply your latest stash after git pull:

git stash pop
Nick McCurdy
  • 17,658
  • 5
  • 50
  • 82
  • 4
    That last command should be either `git stash apply` or `git stash pop`. (`pop` is just `apply`-then-`drop`. I prefer to separate the steps in case of problems, but then again, I prefer to create temporary commits rather than stashing in the first place...) – torek Jul 27 '14 at 18:30
  • 1
    Oops, that last one was a mistake, I change it to `git stash pop`. Thanks. – Nick McCurdy Jul 27 '14 at 18:37
49

You most likely had the files staged.

git add src/file/to/ignore

To undo the staged files,

git reset HEAD

This will unstage the files allowing for the following git command to execute successfully.

git update-index --assume-unchanged src/file/to/ignore
Foxan Ng
  • 6,883
  • 4
  • 34
  • 41
Eric
  • 699
  • 5
  • 15
  • 3
    Hi I did `git update-index --assume-unchanged src/file/to/ignore`, but now I want to track changes on the /file/to/ignore, how can I undo this ? – user1735921 Jun 20 '19 at 08:42
  • 4
    git update-index --no-assume-unchanged src/file/to/ignore – Eric Jun 20 '19 at 10:16
  • 2
    I think skip-worktree is better than unchanged index – user1735921 Jun 24 '19 at 10:24
  • 1
    Gits documentation states the solution I provided. Not just based on working experience but, the recommendation by git on the correct course of action. – Eric Jun 24 '19 at 10:28
2

You probably need to do a git stash before you git pull, this is because it is reading your old config file. So do:

git stash
git pull
git commit -am <"say first commit">
git push

Also see git-stash(1) Manual Page.

jww
  • 97,681
  • 90
  • 411
  • 885
1

If you dont want your local changes, then do below command to ignore(delete permanently) the local changes.

  • If its unstaged changes, then do checkout (git checkout <filename> or git checkout -- .)
  • If its staged changes, then first do reset (git reset <filename> or git reset) and then do checkout (git checkout <filename> or git checkout -- .)
  • If it is untracted files/folders (newly created), then do clean (git clean -fd)

If you dont want to loose your local changes, then stash it and do pull or rebase. Later merge your changes from stash.

  • Do git stash, and then get latest changes from repo git pull orign master or git rebase origin/master, and then merge your changes from stash git stash pop stash@{0}
rashok
  • 12,790
  • 16
  • 88
  • 100
0

in Git 2.23 git restore is introduced. For detail check this stackoverflow and www.git-tower.com.

Here is the commands to discard the local changes and unstage changes:

To discard local tracked changes

git restore . or git restore file1 file2 ...

To discard local untracked changes

git clean -fd

To unstage changes:

git restore --staged . or git restore --staged file1 file2 ...

abdella
  • 490
  • 5
  • 11
0

When you want to checkout, but incoming changes conflict with local changes, using -f seems to clear only the conflicting files.

git checkout -f $branch

You switch to the new branch, your local changes are cleared, and your untracked files are kept.

kendallbp
  • 61
  • 5