如何在Windows上的Git中創建文件執行模式權限?

本文翻譯自:How to create file execute mode permissions in Git on Windows?

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? git配置? windows command? Windows命令?

Remind : Two answers are good, git add --chmod=+x file is supported in new git version 提醒 :兩個答案都不錯,新的git版本支持git add --chmod=+x file

Reference: see question in Git file permissions on Windows for second commit 參考:請參閱Windows上Git文件權限中的問題以進行第二次提交


#1樓

參考:https://stackoom.com/question/1T0s6/如何在Windows上的Git中創建文件執行模式權限


#2樓

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). 如您所述,添加後,模式爲0644(即,不可執行)。 However, we can mark it as executable before committing: 但是,我們可以在提交之前將其標記爲可執行文件:

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. 現在,我們有了一個帶有單個可執行文件的提交。


#3樓

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 ) . 參見Edward Thomson( ethomson )的 commit 4e55ed3 (2016年5月31日
Helped-by: Johannes Schindelin ( dscho ) . 幫助人: Johannes Schindelin( dscho
(Merged by Junio C Hamano -- gitster -- in commit c8b080a , 06 Jul 2016) (由Junio C gitster - gitstercommit c8b080a中合併 ,2016年7月6日)

add : add --chmod=+x / --chmod=-x options add :添加--chmod=+x / --chmod=-x選項

The 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 do have core.filemode functionality. 可執行位將不與倉庫的路徑進行檢測(因此不會設置) core.filemode設置爲false,雖然用戶仍然可能希望添加文件作爲可執行文件與誰有其他用戶的兼容性core.filemode功能。
For example, Windows users adding shell scripts may wish to add them as executable for compatibility with users on non-Windows. 例如,添加外殼腳本的Windows用戶可能希望將它們添加爲可執行文件,以與非Windows用戶兼容。

Although this can be done with a plumbing command ( git update-index --add --chmod=+x foo ), teaching the git-add command allows users to set a file executable with a command that they're already familiar with . 儘管這可以通過管道命令( git update-index --add --chmod=+x foo )來完成,但是教導git-add命令允許用戶使用他們已經熟悉的命令來設置可執行文件


#4樓

If the files already have 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也認爲沒有要提交的內容。

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>

then git sees a change and will allow you to commit the change. 然後 git看到更改,並允許您提交更改。


#5樓

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

#6樓

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中沒有touchchmod命令,並且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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章