我知道.gitignore文件隐藏Git版本控制中的指定文件。
我如何告诉.gitignore忽略除我使用Git跟踪的文件之外的所有内容?类似于:
# Ignore everything:
*
# Do not ignore these files:
script.pl
template.latex
我知道.gitignore文件隐藏Git版本控制中的指定文件。
我如何告诉.gitignore忽略除我使用Git跟踪的文件之外的所有内容?类似于:
# Ignore everything:
*
# Do not ignore these files:
script.pl
template.latex
当前回答
这是一场灾难,在使用git 16年(2005年)之后,没有一种简单而明显的方法来排除位于目录层次结构深处的一个文件,否则应该忽略它。相反,我们需要采取疯狂的做法,比如反复挖掘结构,并以正确的顺序排除和包含目录。一年中你需要记住4次,这是不可能的。
唯一聪明但非常奇怪的选择是使用git add--force。当你不单独处理gitted回购协议时,某些事情注定会失败。
所以我编写了一个小bash函数来修复这个问题,以便于复制粘贴。让我先解释一下一行:
f=0; y='';for x in $(echo "uploads/rubbish/stuff/KEEP_ME/a.py" | tr '\/' '\n'); do y="$y/$x"; if [ $(($f)) -eq 0 ]; then y="$x"; f=1; fi; echo -e '!'"$y/\n$y/*"; done | sed '$d' |sed -z 's/..$//'; echo;
# output:
!uploads/
uploads/*
!uploads/rubbish/
uploads/rubbish/*
!uploads/rubbish/stuff/
uploads/rubbish/stuff/*
!uploads/rubbish/stuff/KEEP_ME/
uploads/rubbish/stuff/KEEP_ME/*
!uploads/rubbish/stuff/KEEP_ME/a.py
描述
有一个if语句用于调整第一个未获得/的项目。tr'\/''\n'-在标准POSIX路径中\n转换/to(以获取列表)y=“$y/$x”-在上一个目录之后添加下一个子目录(在列表中)。echo-e'!'“$y/\n$y/*”-打印2行子目录项,第一行用!前缀,第二个带/*后缀。sed“$d”-删除最后一行sed-z的/..$//'-从最后一行删除最后2个字符/*
然后我们创建函数:
function gitkeep () { f=0; y='';for x in $(echo "$1" | tr '\/' '\n'); do y="$y/$x"; if [[ f -eq 0 ]]; then y="$x"; f=1; fi; echo -e '!'"$y/\n$y/*"; done | sed '$d' |sed -z 's/..$//'; echo; }
# gitkeep "uploads/rubbish/stuff/KEEP_ME/a"
!uploads/
uploads/*
!uploads/rubbish/
uploads/rubbish/*
!uploads/rubbish/stuff/
uploads/rubbish/stuff/*
!uploads/rubbish/stuff/KEEP_ME/
uploads/rubbish/stuff/KEEP_ME/*
!uploads/rubbish/stuff/KEEP_ME/a
这里,我将算术if语句简化为if[[f-eq 0]];。
享受
其他回答
与OP有类似的问题,但前10名投票支持的答案中没有一个有效。我终于发现了
错误语法:
*
!bin/script.sh
正确语法:
*
!bin
!bin/script.sh
gitignore手册页的解释:
一个可选前缀“!”,用于否定模式;被先前模式排除的任何匹配文件将再次包含。如果排除了文件的父目录,则无法重新包含该文件。由于性能原因,Git没有列出排除的目录,因此包含的文件上的任何模式都不会产生任何影响,无论它们是在哪里定义的。
这意味着上面的“错误语法”是错误的,因为bin/script.sh不能重新包含,因为bin/被忽略。这就是全部。
扩展示例:
$tree。
.
├── .gitignore
└── bin
├── ignore.txt
└── sub
└── folder
└── path
├── other.sh
└── script.sh
$cat.gitignore
*
!.gitignore
!bin
!bin/sub
!bin/sub/folder
!bin/sub/folder/path
!bin/sub/folder/path/script.sh
$git状态--未跟踪的文件--已忽略
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
bin/sub/folder/path/script.sh
Ignored files:
(use "git add -f <file>..." to include in what will be committed)
bin/ignore.txt
bin/sub/folder/path/other.sh
nothing added to commit but untracked files present (use "git add" to track)
我是这样做的:
# Ignore everything
*
# Whitelist anything that's a directory
!*/
# Whitelist some files
!.gitignore
# Whitelist this folder and everything inside of it
!wordpress/wp-content/themes/my-theme/**
# Ignore this folder inside that folder
wordpress/wp-content/themes/my-theme/node_modules
# Ignore this file recursively
**/.DS_Store
使用gig status-u递归查看未跟踪目录中的单个文件-使用git status,您只能看到文件夹,这可能会让您误以为其中的所有内容都被跟踪
最简单的方法是强制添加文件。即使它被隐藏或嵌套在git忽略的子目录树中,它也将在git中进行说明。
例如:
x64文件夹已在中排除。gitignore:
x64/
但您希望包含位于x64/发布/目录中的文件myFile.py。然后你必须:
git add -f x64/Release/myFile.py
您可以对匹配模式的多个文件执行此操作,例如。
git add -f x64/Release/myFile*.py
等等
我有博沃的Jquery和Angular。Bower将其安装在
/public_html/bower_components/jquery/dist/bunch-of-jquery-files
/public_html/bower_components/jquery/src/bunch-of-jquery-source-files
/public_html/bower_components/angular/angular-files
最小化的jquery位于dist目录中,angular位于angular目录中。我只需要最小化的文件就可以提交到github。对gitignore进行了一些篡改,这就是我设法变出来的。。。
/public_html/bower_components/jquery/*
!public_html/bower_components/jquery/dist
/public_html/bower_components/jquery/dist/*
!public_html/bower_components/jquery/dist/jquery.min.js
/public_html/bower_components/angular/*
!public_html/bower_components/angular/angular.min.js
希望有人能发现这个有用。
要从.gitignore中排除文件夹,可以执行以下操作。
!app/
app/*
!app/bower_components/
app/bower_components/*
!app/bower_components/highcharts/
这将忽略bower_components中的所有文件/子文件夹,/hightcharts除外。