43

I have a production git repo that I only pull changes from the main repo into; I never change this repo or do commits/pushes from here. I recently accidentally pushed some untracked (at least I thought they were) image files to the main repo from my local dev repo. Now when I try to pull the latest from the main repo, git reports an error regarding overwriting the exiting image file with the file from the main repo. I don't even want this file from the repo (it's located in a .gitignored directory on the production repo).

How can I a) get rid of these unwanted image files in my main repo, or b) exclude these files from my git pull?

Eduardo Cuomo
  • 17,828
  • 6
  • 117
  • 94
Chris Barnhill
  • 621
  • 1
  • 7
  • 12
  • There must be some time when you added these files to the repository. You can always rewrite your history to not contain these adding or any commit related to the files you don't want in your repository. The answer depends on if we are speaking about one commit or several commits of several files you want to untrack. – ikrabbe Jul 17 '15 at 18:11

2 Answers2

30

This allowed me to tell git to ignore a specific file, even though it was already part of a project. All changes I make to it will be ignored:

git update-index --assume-unchanged Localization/el-GR.js

Notes

  • It's ignored by git status, but not by git pull. You can solve it with something like git remote update && git checkout origin/main -- Localization/el-GR.js in order to retrieve the updated version.

Source: http://codethug.com/2013/09/20/4-ways-to-ignore-files-with-git/

Eduardo Cuomo
  • 17,828
  • 6
  • 117
  • 94
  • "All changes I make to it will be ignored"—[not true](https://public-inbox.org/git/xmqqy4z5go1y.fsf@gitster.dls.corp.google.com/): 'Assume-unchanged should not be abused for an ignore mechanism. It is "I know my filesystem operations are slow. **I'll promise Git that I _won't_ change these paths**…" Especially, it is *not* a promise… that Git will always consider these paths are unmodified—if Git can determine a path… has changed without incurring extra `lstat(2)` cost, it reserves the right to report that the path *has been* modified (…`git commit -a` is free to commit that change).' – ChrisGPT was on strike Apr 06 '19 at 20:31
  • 6
    its ignored by `git status`, but not by `git pull`, I still get aborting in git pull if I change it. – MortenB Dec 09 '19 at 13:50
  • Sorry @spiceitup, I can not understand your point. For this question, the `.gitignored` file can not be used, and this is one way to answer the question. Can you please share your correct answer with us? – Eduardo Cuomo Jan 13 '23 at 12:11
8

git pull is equivalent (almost) to git fetch && git merge. You just have to invoke fetch and than merge only specific files - tutorial.

kwarunek
  • 12,141
  • 4
  • 43
  • 48
  • If I do fetch, will git overwrite the existing image files in my production repo? (I don't want that) – Chris Barnhill Aug 05 '13 at 21:28
  • The file I pushed to the main repo was recently added. I did a git fetch like you suggested, but when I attempt git merge on the new file, it fails. Any suggestions? – Chris Barnhill Aug 05 '13 at 22:01