如何在 Windows 上的 Git 中创建文件执行模式权限?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21691202/
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
How to create file execute mode permissions in Git on Windows?
提问by Larry Cai
I use Git in Windows, and want to push the executable shell script into git repo by one commit.
我在 Windows 中使用 Git,并希望通过一次提交将可执行的 shell 脚本推送到 git repo 中。
Usually I need to do two steps (git commit
).
通常我需要做两个步骤(git commit
)。
$ vi install.sh
$ git add install.sh
$ git commit -am "add new file for installation" # first commit
[master f2e92da] add support for install.sh
1 files changed, 18 insertions(+), 3 deletions(-)
create mode 100644 install.sh
$ git update-index --chmod=+x install.sh
$ git commit -am "update file permission" # second commit
[master 317ba0c] update file permission
0 files changed
mode change 100644 => 100755 install.sh
How can I combine these two steps into one step? git configuration? windows command?
如何将这两个步骤合并为一个步骤?git配置?窗口命令?
Remind: Two answers are good, git add --chmod=+x file
is supported in new git version
提醒:两个答案都很好,git add --chmod=+x file
在新的 git 版本中支持
Reference: see question in Git file permissions on Windowsfor second commit
参考:请参阅Windows 上 Git 文件权限中的问题以进行第二次提交
回答by Edward Thomson
There's no need to do this in two commits, you can add the file and mark it executable in a single commit:
无需在两次提交中执行此操作,您可以在一次提交中添加文件并将其标记为可执行:
C:\Temp\TestRepo>touch foo.sh
C:\Temp\TestRepo>git add foo.sh
C:\Temp\TestRepo>git ls-files --stage
100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0 foo.sh
As you note, after adding, the mode is 0644 (ie, not executable). However, we can mark it as executable before committing:
如您所见,添加后,模式为 0644(即,不可执行)。但是,我们可以在提交之前将其标记为可执行文件:
C:\Temp\TestRepo>git update-index --chmod=+x foo.sh
C:\Temp\TestRepo>git ls-files --stage
100755 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0 foo.sh
And now the file is mode 0755 (executable).
现在文件是模式 0755(可执行)。
C:\Temp\TestRepo>git commit -m"Executable!"
[master (root-commit) 1f7a57a] Executable!
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100755 foo.sh
And now we have a single commit with a single executable file.
现在我们有一个带有单个可执行文件的提交。
回答by VonC
Indeed, it would be nice if
git-add
had a--mode
flag
确实,如果
git-add
有--mode
一面旗帜就好了
git 2.9.x/2.10 (Q3 2016) actually will allow that (thanks to Edward Thomson):
git 2.9.x/2.10(2016 年第三季度)实际上将允许(感谢Edward Thomson):
git add --chmod=+x -- afile
git commit -m"Executable!"
That makes the all process quicker, and works even if core.filemode
is set to false.
这使得所有过程更快,即使core.filemode
设置为 false 也能工作。
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
命令允许用户使用他们已经熟悉的命令设置文件可执行文件。
回答by Bohemian
If the files alreadyhave the +x flag set, git update-index --chmod=+x
does nothing and git thinks there's nothing to commit, even though the flag isn't being saved into the repo.
如果文件已经设置了 +x 标志,git update-index --chmod=+x
则什么都不做并且 git 认为没有什么可提交的,即使该标志没有被保存到 repo 中。
You must first remove the flag, run the git command, then put the flag back:
您必须首先删除标志,运行 git 命令,然后将标志放回去:
chmod -x <file>
git update-index --chmod=+x <file>
chmod +x <file>
thengit sees a change and will allow you to commit the change.
然后git 会看到更改并允许您提交更改。
回答by Nabi K.A.Z.
The note is firstly you must sure about filemode
set to false
in config git file, or use this command:
注意首先你必须确定在config git文件中filemode
设置为false
,或者使用这个命令:
git config core.filemode false
and then you can set 0777 permission with this command:
然后您可以使用以下命令设置 0777 权限:
git update-index --chmod=+x foo.sh
回答by khiav reoy
I have no touch
and chmod
command in my cmd.exe
and git update-index --chmod=+x foo.sh
doesn't work for me.
我的 cmd.exe 中没有touch
andchmod
命令,对我git update-index --chmod=+x foo.sh
不起作用。
I finally resolve it by setting skip-worktree
bit:
我终于通过设置skip-worktree
位来解决它:
git update-index --skip-worktree --chmod=+x foo.sh