git update-index --assume-unchanged on directory
Asked Answered
A

4

121

git 1.7.12

I want to mark all files below a given directory as assume-unchanged.

  1. git update-index --assume-unchanged dir/ gives "Ignoring path."

  2. git update-index --assume-unchanged dir/* quickly fails because it will encounter files which are not being tracked, hence it gives "fatal: Unable to mark file" and quits.

  3. Try generating a list of files to mark. cd into the desired directory and then run git ls-files | tr '\n' ' ' | git update-index --assume-unchanged. This produces no error message, but it does not successfully mark the files. The first portion of the command, git ls-files | tr '\n' ' ', correctly produces a space delimited list of all the files I want to mark. If I copy and paste the output of that command onto the command-line, then the git update-index command works. What is not working with the pipes?

No, it is not sufficient for me to add dir to .gitignore. I need these files to be in the repository, but undesired changes will be made locally that need to be ignored so that users can do pulls.

Anabasis answered 5/9, 2012 at 19:10 Comment(2)
You mispelled --assume-unchangedBarbur
@AdamMudianto fixedPearlpearla
C
218

git update-index wants the file names on its command line, not on its standard input.

Step 1:

cd into the folder you want to assume is unchanged

Step 2:

You can do either this:

git update-index --assume-unchanged $(git ls-files | tr '\n' ' ')

or

git ls-files | tr '\n' ' ' | xargs git update-index --assume-unchanged

Although, with either case, file names with spaces will be problematic. If you have those, you can use this:

git ls-files -z | xargs -0 git update-index --assume-unchanged

Edit: incorporated input from @MatthewScharley regarding git ls-files -z.

Windows Commands

Note: If you're on windows, use Git Bash to run these commands

Crin answered 5/9, 2012 at 20:3 Comment(8)
FYI, that ` | tr '\n' ' '` bit is unnecessary.Milagro
@VáclavSlavík You're right... can't remember why I put that in, other than for symmetry with the tr '\n' '\0' case (in which case it is needed)...Crin
@twalberg: You could just use git ls-files -zThreemaster
@Crin How to undo this command "git ls-files -z | xargs -0 git update-index --assume-unchanged"Slaphappy
@MohamedHussain git ls-files -z | xargs -0 git update-index --no-assume-unchanged, I would think...Crin
git ls-files -z | git update-index -z --stdin is cleaner.Scissel
This does not seem to work in git version 2.33.0 for untracked files inside folders.Magallanes
@MichaelTrouw Asker specifically stated git version 1.7.2. Changes made to git behavior between 1.7.2 and 2.33.0 do not invalidate this answer.Crin
G
31

The find command from GNU Findutils has a -exec option which removes most of the hassle of using xargs, although its syntax is a little special. It does however deal perfectly with filenames with spaces.

This command will get git to assume all files in and under the listed directory are unchanged:

find path/to/dir -type f -exec git update-index --assume-unchanged '{}' \;

Find takes every argument after -exec until ; (which you have to escape lest your shell eats it) and runs it once for each file found, while replacing {} (again, single quoted so your shell won't eat it) with the found file's name.

Using find's matching criteria (maximum recursion depth, whether the match is a file or is a directory, whether the filename matches an expression) and -exec you can do all sort of powerful things.

Not sure about other implementations of the find command. YMMV.

Gereron answered 7/8, 2013 at 18:56 Comment(3)
-exec is a standard option. You can also -exec git update-index --assume-unchanged {} + (also defined by the standard) which passes multiple file names to the command at once, reducing the number of times the git command is invoked.Hallie
The disadvantage of the -exec git update-index --assume-unchanged {} + is that if one file fails they all will, so it won't work if there are tracked and untracked files in the directorySeptember
Nice one bro...Shoebill
S
31

Add the directory name to .git/info/exclude. This works for untracked files.

Shang answered 6/10, 2015 at 13:6 Comment(2)
This work great! Did you know if this work with files too?Piper
But this doesn't push the untracked files github! is there any possible way to just ignore them from the records but actually keeping them in the repos!?Crum
C
3

Yeap,

git update-index --assume-unchanged

works with files only, not with directories. I think, one of faster ways:

cd dir
ls | xargs -l git update-index --assume-unchanged
Cent answered 17/5, 2013 at 14:11 Comment(1)
Instead of using cd, you can pass the dir as a final argument: git ls-files -- $DIR | xargs -l git update-index --assume-unchanged -- $DIR.Tripe

© 2022 - 2024 — McMap. All rights reserved.