24

I have a website project that has more than 50,000 unimportant files (to development) in some directories.

/website.com/files/1.txt
/website.com/files/2.txt
/website.com/files/3.txt
/website.com/files/etc.txt

The stuff in /files is already in the repo. I want to delete all the files in /files on my local copy but I want git to ignore it so it doesn't delete them when I do a pull on the web server.

Any ideas?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Petah
  • 45,477
  • 28
  • 157
  • 213

6 Answers6

15

Ignoring a whole directory didn't work. I had to do this:

for i in `git status | grep deleted | awk '{print $3}'`; do git update-index --assume-unchanged $i; done
dave1010
  • 15,135
  • 7
  • 67
  • 64
12

The code below works on deleted as well as modified files to ignore it when you do a git status.

git update-index --assume-unchanged dir-im-removing/

or a specific file

git update-index --assume-unchanged config/database.yml

Ignore modified (but not committed) files in git?

Beware: The suggestion above for deleted files when you do a "git commit -am " includes the deleted file!

A solution that would work for me is to instead of deleting the file, just make it's content blank. This is what I used to mute a .htaccess file.

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Quang Van
  • 11,475
  • 12
  • 57
  • 61
3

Ok Ive found a solution. Simply create a new repo in the sub directories and the parent repo will ignore them.

Petah
  • 45,477
  • 28
  • 157
  • 213
  • 2
    that will work, but it's not ideal to end up with a bunch of branches/repos in multiple projects. going forward could become a pain as your project list gets larger. I do recommend becoming a bit more familiar with `.gitignore` and the index management tools. There's a decent tutorial in my answer above. It really is fairly easy once you try it once... – MaurerPower Jun 03 '12 at 02:36
  • @MaurerPower, while I appreciate your descriptive answer, `git rm --cached` doesn't fit my requirements. For example if I use that command, then commit and push. When I pull to another server it will still delete the files from that server, even though it did not delete them from the original server. I did a litte test, here are my results: http://pastebin.com/5X0t1KGs – Petah Jun 03 '12 at 03:09
3

Creating a sub-repo is one solution, but you should make that sub-repo a submodule.

That way:

  • you keep a link between your normal content and that 'files' content
  • if the 'files' content evolves one day, you can register its evolution in the main repo
  • you can checkout your main repo without " git submodule update " (that is without filling the 'files' content
  • on deployment, a git submodule update after the git submodule init will be enough to get back the right version of the 'files' content.
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

To commit only added / modified files (and NOT removed ones) do:

git add . --ignore-removal
git commit -m "commit message"
N. Joppi
  • 416
  • 5
  • 9
0
for i in `git status | grep deleted | awk '{print $2}'`; do git update-index --assume-unchanged $i; done

works with git 2.25.1

if you encounter this issue:

fatal: Unable to mark file PATH/TO/FILE

check there is no space in the file path and use quotes instead

ELCaptain
  • 66
  • 4