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:
chmod u+x $script git commit -am "fixing the script permissions... again..."
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 "Hey, this here is an executable file!" and add it to the repository with the executable permissions directly?
In short, chmod +x following by a filename, usually a script, means that you make it executable. In Ubuntu or other distro that uses GNOME, you can achieve the same result by right click on your file/script and choose Properties, then switch to Permissions tab, tick the Allow executing file as program checkbox.
The solution is to use the Git update-index command to assign the execute permissions. This will assign execute permissions to the bash file. After that you can commit the changes to the repo.
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.
git 2.9.X/2.10 (Q3 2016) brings chmod
to git add
itself!
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.
You can see the origin of this new feature in "How to create file execute mode permissions in Git on Windows?" (Feb. 2011)
There are several ways to do that.
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 $0; git add $0'
Bash aliases
Edit your bash profile file:
vim ~/.bashrc
Add this at the end of the file:
function gitadd(){ if [[ ${1: -3} == ".sh" ]] then git update-index --chmod=+x $1 fi git add $1 } alias gitadd='gitadd'
Combine git and bash aliases
Edit your bash profile file:
vim ~/.bashrc
Add this to the end of the file:
function checkShellFile(){ return ${1: -3} == ".sh" } alias gitadd='checkShellFile ? git addsrcipt "$1" : && git add "$1"'
Edit your git config file:
vim ~/.gitconfig
Add an aliases section to it (if one does not exist):
[alias] addscript = !sh -c 'git update-index --chmod=+x $0 && git add $0'
None of the above has been tested
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With