【Git】git 常见问题

215 阅读1分钟

1. gitignore文件不生效

问题描述

当我们将 .gitignore 文件配置好后,却往往不能失效。这是因为 .gitignore 只能忽略那些没有被追踪(track)的文件,因为 git 存在本地缓存,如果文件已经纳入了版本管理,那么修改 .gitignore 是不能失效的。

解决方案

将 git 的本地缓存删除,然后重新提交。

git rm -r --cached .
git add .
git commit -m "update .gitignore"

2. sh文件添加可执行权限

需要使用 git update-index 命令

2.1查看文件权限

PS D:\workspace\tools> git ls-files --stage
100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0       1.sh

文件权限为 100644(r=4,w=2,x=1),添加可执行权限需要将其修改为可执行权限755

git ls-files --help

git ls-files [-z] [-t] [-v] [-f]
                (--[cached|deleted|others|ignored|stage|unmerged|killed|modified])*
                (-[c|d|o|i|s|u|k|m])*
                [--eol]
                [-x <pattern>|--exclude=<pattern>]
                [-X <file>|--exclude-from=<file>]
                [--exclude-per-directory=<file>]
                [--exclude-standard]
                [--error-unmatch] [--with-tree=<tree-ish>]
                [--full-name] [--recurse-submodules]
                [--abbrev] [--] [<file>…​]

2.2修改文件可执行权限

增加可执行权限

git update-index --chmod +x 1.sh

删除可执行权限

git update-index --chmod -x 1.sh

git update-index --help

git update-index
	     [--add] [--remove | --force-remove] [--replace]
	     [--refresh] [-q] [--unmerged] [--ignore-missing]
	     [(--cacheinfo <mode>,<object>,<file>)…​]
	     [--chmod=(+|-)x]
	     [--[no-]assume-unchanged]
	     [--[no-]skip-worktree]
	     [--[no-]fsmonitor-valid]
	     [--ignore-submodules]
	     [--[no-]split-index]
	     [--[no-|test-|force-]untracked-cache]
	     [--[no-]fsmonitor]
	     [--really-refresh] [--unresolve] [--again | -g]
	     [--info-only] [--index-info]
	     [-z] [--stdin] [--index-version <n>]
	     [--verbose]
	     [--] [<file>…​]