git update-index --assume-unchanged 在目录上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12288212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 14:32:55  来源:igfitidea点击:

git update-index --assume-unchanged on directory

gitcommand-linepipe

提问by Xander Dunn

git 1.7.12

混帐 1.7.12

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

我想将给定目录下的所有文件标记为假设未更改。

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

1)git update-index --assume-unchaged dir/给出“忽略路径”。

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

2)git update-index --assume-unchaged dir/*很快失败,因为它会遇到未被跟踪的文件,因此它给出“致命:无法标记文件”并退出。

3) Try generating a list of files to mark. cdinto 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-indexcommand works. What is not working with the pipes?

3) 尝试生成要标记的文件列表。 cd进入所需目录,然后运行git ls-files | tr '\n' ' ' | git update-index --assume-unchanged. 这不会产生错误消息,但不会成功标记文件。命令的第一部分git ls-files | tr '\n' ' ',正确地生成了我想要标记的所有文件的空格分隔列表。如果我将该命令的输出复制并粘贴到命令行上,则该git update-index命令有效。什么不适用于管道?

No, it is not sufficient for me to add dirto .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.

不,我添加dir到 .gitignore是不够的。我需要将这些文件放在存储库中,但会在本地进行不需要的更改,需要忽略这些更改,以便用户可以进行拉取。

回答by twalberg

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

git update-index希望文件名在其命令行上,而不是在其标准输入上。

Step 1:

第1步:

cdinto the folder you want to assume is unchanged

cd进入您要假设的文件夹未更改

Step 2:

第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.

编辑:合并了@MatthewScharley 关于git ls-files -z.

Windows Commands

Windows 命令

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

注意:如果您使用的是 Windows,请使用Git Bash来运行这些命令

回答by theycallhimart

The findcommand from GNU Findutils has a -execoption 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.

find来自 GNU Findutils的命令有一个-exec选项,它消除了使用 的大部分麻烦xargs,尽管它的语法有点特殊。然而,它确实可以完美地处理带空格的文件名。

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

此命令将使 git 假定列出的目录中和下的所有文件都保持不变:

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

Find takes every argument after -execuntil ;(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.

Find 接受-exec直到之后的每个参数;(你必须逃避,以免你的 shell 吃掉它)并为找到的每个文件运行一次,同时用{}找到的文件名替换(再次,单引号,所以你的 shell 不会吃它)。

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 -execyou can do all sort of powerful things.

使用find的匹配标准(最大递归深度,匹配是文件还是目录,文件名是否与表达式匹配),-exec您可以做各种强大的事情。

Not sure about other implementations of the findcommand. YMMV.

不确定该find命令的其他实现。天啊。

回答by Steve Pitchers

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

将目录名称添加到.git/info/exclude. 这适用于未跟踪的文件。

回答by Victor Perov

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