I use Git in Windows, and want to push the executable shell script into git repo by one commit.
Usually I need to do two steps (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?
Remind: Two answers are good, git add --chmod=+x file
is supported in new git version
Reference: see question in Git file permissions on Windows for second commit
This question is related to
git
Indeed, it would be nice if
git-add
had a--mode
flag
git 2.9.x/2.10 (Q3 2016) actually will allow that (thanks to 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.
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)
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 do havecore.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.
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.
You must first remove the flag, run the git command, then put the flag back:
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.
The note is firstly you must sure about filemode
set to false
in config git file, or use this command:
git config core.filemode false
and then you can set 0777 permission with this command:
git update-index --chmod=+x foo.sh
I have no touch
and chmod
command in my cmd.exe
and git update-index --chmod=+x foo.sh
doesn't work for me.
I finally resolve it by setting skip-worktree
bit:
git update-index --skip-worktree --chmod=+x foo.sh
Source: Stackoverflow.com