自动将“git update-index --chmod=+x”应用于可执行文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14267441/
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
Automatically apply "git update-index --chmod=+x" to executable files
提问by Byron Hawkins
I frequently add bash scripts to my git repository, and the scripts have executable permissions in the linux filesystem prior to the git add
. But after pushing the added files to a remote repository and pulling in another location, the files show up with non-executable permissions. There seem to be two ways to correct the problem:
我经常将 bash 脚本添加到我的 git 存储库中,并且脚本在git add
. 但是在将添加的文件推送到远程存储库并拉入另一个位置后,这些文件显示为具有不可执行的权限。似乎有两种方法可以解决这个问题:
1. chmod u+x $script
git commit -am "fixing the script permissions... again..."
or
或者
2. git update-index --chmod=+x $script
Instead of fixing up the permissions every time, is there a way to have git simply look at the file permissions on the script during git add
, recognize that "hey, this here is an executable file!" and add it to the repository with the exectuable permissions directly?
不是每次都修复权限,有没有办法让 git 简单地在 git 期间查看脚本上的文件权限git add
,认识到“嘿,这是一个可执行文件!” 并直接将其添加到具有可执行权限的存储库中?
采纳答案by PassTeT
There are several ways to do that.
有几种方法可以做到这一点。
- Git aliases
- Bash aliases
- Or even combine bash and git aliases
- Git别名
- Bash 别名
- 甚至结合 bash 和 git 别名
Git aliases
You can always use bash within your git alias.
Open your git config:
vim ~/.gitconfig
Add an aliases section to it (if one does not exist):
[alias] addscr = !sh -c 'if [[ ${0: -3} == ".sh" ]]; then git update-index --chmod=+x
; git addfunction gitadd(){ if [[ ${1: -3} == ".sh" ]] then git update-index --chmod=+x fi git add } alias gitadd='gitadd'
'function checkShellFile(){ return ${1: -3} == ".sh" } alias gitadd='checkShellFile ? git addsrcipt "" : && git add ""'
Bash aliases
Edit your bash profile file:
vim ~/.bashrc
Add this at the end of the file:
[alias] addscript = !sh -c 'git update-index --chmod=+x
&& git add[alias] addscr = !sh -c 'if [[ ${0: -3} == ".sh" ]]; then git update-index --chmod=+x
; git addfunction gitadd(){ if [[ ${1: -3} == ".sh" ]] then git update-index --chmod=+x fi git add } alias gitadd='gitadd'
'function checkShellFile(){ return ${1: -3} == ".sh" } alias gitadd='checkShellFile ? git addsrcipt "" : && git add ""'
'[alias] addscript = !sh -c 'git update-index --chmod=+x
&& git add ##代码##'for f in `find . -name '*.sh' -o -regex './s?bin/[^/]+' -o -regex './usr/sbin/[^/]+' -o -regex './usr/lib/[^/]+' `;do ( cd `dirname $f` && git update-index --chmod=+x `basename $f` ) done
Combine git and bash aliases
Edit your bash profile file:
vim ~/.bashrc
Add this to the end of the file:
##代码##Edit your git config file:
vim ~/.gitconfig
Add an aliases section to it (if one does not exist):
##代码##
Git别名
您始终可以在 git 别名中使用 bash。
打开你的 git 配置:
vim ~/.gitconfig
向其中添加别名部分(如果不存在):
##代码##
Bash 别名
编辑您的 bash 配置文件:
vim ~/.bashrc
在文件末尾添加:
##代码##
结合 git 和 bash 别名
编辑您的 bash 配置文件:
vim ~/.bashrc
将此添加到文件末尾:
##代码##编辑您的 git 配置文件:
vim ~/.gitconfig
向其中添加别名部分(如果不存在):
##代码##
None of the above has been tested
以上都没有经过测试
回答by VonC
git 2.9.X/2.10 (Q3 2016) brings chmod
to git add
itself!
混帐2.9.X / 2.10(Q3 2016)带来的chmod
对git add
自身!
See commit 4e55ed3(31 May 2016) by Edward Thomson (ethomson
).
Helped-by: Johannes Schindelin (dscho
).
(Merged by Junio C Hamano -- gitster
--in commit c8b080a, 06 Jul 2016)
请参阅Edward Thomson ( ) 的commit 4e55ed3(2016 年 5 月 31 日)。
帮助者:约翰内斯·辛德林 ( )。(由Junio C Hamano合并-- --在提交 c8b080a 中,2016 年 7 月 6 日)ethomson
dscho
gitster
add
: add--chmod=+x
/--chmod=-x
optionsThe executable bit will not be detected (and therefore will not be set) for paths in a repository with
core.filemode
set to false, though the users may still wish to add files as executable for compatibility with other users who dohavecore.filemode
functionality.
For example, Windows users adding shell scripts may wish to add them as executable for compatibility with users on non-Windows.Although this can be done with a plumbing command (
git update-index --add --chmod=+x foo
), teaching thegit-add
command allows users to set a file executable with a command that they're already familiar with.
add
: 添加--chmod=+x
/--chmod=-x
选项可执行位不会被检测到(并因此将不设置)用于在存储库的路径
core.filemode
设置为false,虽然用户可能仍然希望添加文件可执行与谁其他用户的兼容性做有core.filemode
功能。
例如,添加 shell 脚本的 Windows 用户可能希望将它们添加为可执行文件,以便与非 Windows 上的用户兼容。虽然这可以通过管道命令 (
git update-index --add --chmod=+x foo
)来完成,但教授该git-add
命令允许用户使用他们已经熟悉的命令设置文件可执行文件。
You can see the origin of this new feature in "How to create file execute mode permissions in Git on Windows?" (Feb. 2011)
您可以在“如何在 Windows 上的 Git 中创建文件执行模式权限?”(2011 年 2 月)中了解此新功能的起源
回答by l0pan
Here is a script to automatically apply “git update-index --chmod+x” to executable files:
这是一个自动将“git update-index --chmod+x”应用到可执行文件的脚本:
##代码##回答by A. Sallai
A solution without fancy bash scripting:
没有花哨的 bash 脚本的解决方案:
- Set
fileMode = true
in your.git/config
file (or by runninggit config core.filemode true
as others have pointed out) - Change the executable bit on the file's permissions and commit this change. (
chmod u+x $script
as you pointed out). You only have to do this once. - Push to the remote
fileMode = true
在您的.git/config
文件中设置(或git config core.filemode true
按照其他人指出的方式运行)- 更改文件权限的可执行位并提交此更改。(
chmod u+x $script
正如你所指出的)。您只需执行此操作一次。 - 推到遥控器
The next time you pull from there, git will set the committed executable bit on the file. I also had similar problems, this solved them.
下次从那里拉取时,git 将在文件上设置提交的可执行位。我也有类似的问题,这解决了他们。
fileMode = true
tells git to track the only thing about permissions it can: the executable bit. This means that changes to the executable bit will be recognized by git as changes in the working tree and those changes will be stored in the repo with your next commit.
fileMode = true
告诉 git 跟踪它唯一能做的关于权限的事情:可执行位。这意味着对可执行位的更改将被 git 识别为工作树中的更改,并且这些更改将在您下次提交时存储在 repo 中。
Once you committed the desired executable bit you can also reset your fileMode
to false
so next time git won't bother you with such changes when you don't want to commit them.
一旦您提交了所需的可执行位,您还可以将其重置fileMode
为,false
以便下次当您不想提交这些更改时,git 不会打扰您。
回答by eddiemoya
I don't think this can be done on the git add
command, but you might be able to run a script just right after you run the git commit
command, but before the commit is actually created.
我不认为这可以在git add
命令上完成,但您可能能够在运行git commit
命令后立即运行脚本,但在实际创建提交之前。
Take a look at the pre-commit hook.
看看 pre-commit 钩子。
http://git-scm.com/book/en/Customizing-Git-Git-Hooks
http://git-scm.com/book/en/Customizing-Git-Git-Hooks
Basically just create a file in your .git/hooks/ folder called pre-commit. (There should be samples already in your hooks folder, rename them to remove the ".sample" at the end to activate one.)
基本上只需在您的 .git/hooks/ 文件夹中创建一个名为 pre-commit 的文件。(您的 hooks 文件夹中应该已经有示例,重命名它们以删除末尾的“.sample”以激活一个。)
Theres a gotcha. Make sure your script runs git stash -q
first so that your working on the actual staged versions of files.
有一个问题。确保您的脚本git stash -q
首先运行,以便您处理文件的实际暂存版本。
回答by Smart Coder
I just added thru' Tortoise Git folder update. Right click on all files and update the execute permission checkbox to true and commit/push with message. Git Command line add/commit should also work.
我刚刚通过' Tortoise Git 文件夹更新添加了。右键单击所有文件并将执行权限复选框更新为 true 并提交/推送消息。Git 命令行添加/提交也应该有效。