Kac*_*che 149 git ignore gitignore
.gitignore
可以忽略整个文件,但有没有办法在编码时忽略特定的代码行?
我经常反复在项目中添加相同的调试行,只需记住在提交之前删除它们.我想在代码中保留这些行并让git忽略它们.
Kac*_*che 134
这是你可以用git过滤器做的事情:
<project root>/.gitattributes
,即运行<project root>/.git/info/attributes
在所有*.rb filter=gitignore
文件上命名的过滤器gitignore
在您的*.rb
:中定义过滤器:
gitignore
,即删除这些行gitconfig
,即从repo中提取文件时什么也不做注意:
当然,这适用于ruby文件,在行结束时$ git config --global filter.gitignore.clean "sed '/#gitignore$/'d"
应用,全局应用$ git config --global filter.gitignore.smudge cat
.根据您的需要修改此项.
警告!!
这使您的工作文件与repo不同(当然).任何退房或重新定位都意味着这些线路将会丢失!这个技巧似乎没用,因为这些行在签出,rebase或pull时反复丢失,但我有一个特定的用例才能使用它.
就#gitignore
在过滤器处于非活动状态时(暂时禁用它~/.gitconfig
或其他东西).这样,我的调试代码可以随时git stash save "proj1-debug"
对我的代码进行处理,而不必担心这些行会被意外提交.
我有一个可能的想法来处理这些问题,但我会尝试在其他时间实现它.
感谢Rudi和jw013提到git过滤器和gitattributes.
Mik*_*ike 21
编写java代码时遇到了类似的问题.我的解决方案是标记我不想提交的代码,然后添加一个预提交钩子来寻找我的标记:
#!/bin/bash
#
# This hook will look for code comments marked '//no-commit'
# - case-insensitive
# - dash is optional
# - there may be a space after the //
#
noCommitCount=$(git diff --no-ext-diff --cached | egrep -i --count "(@No|\/\/\s?no[ -]?)commit")
if [ "$noCommitCount" -ne "0" ]; then
echo "WARNING: You are attempting to commit changes which include a 'no-commit'."
echo "Please check the following files:"
git diff --no-ext-diff --cached --name-only -i -G"(@no|\/\/s?no-?)commit" | sed 's/^/ - /'
echo
echo "You can ignore this warning by running the commit command with '--no-verify'"
exit 1
fi
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
47287 次 |
最近记录: |