Git

How to Add chmod Permissions to File in Git?

Git files have by default β€œ100644” chmod permissions mode. This represents that the file owner can β€œread/write” permission. In contrast, the other members can only have β€œread” permission, indicating they cannot edit the files. Moreover, Git provides the β€œ100755” (owner can read/write/search, and others can search only) and β€œ100777” (all can read/write/search) chmod permissions for files. Git developers can add desired chmod permissions to the files by enabling the β€œcore.filemode” in the configuration file.

This guide will talk about the method of adding chmod permissions to files in Git.

How to Add chmod Permissions to File in Git?

Try the below-listed steps to add chmod permissions to a file in Git:

Step 1: Go to Git Repository

First, use the β€œcd” command along with the desired repository path and move to it:

$ cd "C:\Users\nazma\Git\Test_14"

Step 2: Set File Mode Configuration Value

Next, add the value of the file mode into the configuration file by running the β€œgit config” command with the β€œcore.filemode” parameter and value as β€œtrue”:

$ git config core.filemode true

Step 3: Verify Added File Mode

Now, ensure whether the file mode value is added or not:

$ git config core.filemode

Step 4: List Stage Files With Permissions Mode

After that, execute the β€œgit ls-files” command with the β€œ–stage” option to list all stage files along with their permission mode:

$ git ls-files --stage

According to the below-given output, all listed files have the β€œ100644” permission mode which indicates that the file owner has the β€œread/write” permission. However, other members have only the β€œread” permission:

Step 5: Add Chmod Permissions to Files

Now, run the β€œgit update-index” command along with the desired chmod permission mode and particular file name to which need to add permission:

$ git update-index --chmod=+x file6.txt

Here:

  • β€œ–chmod” is the parameter having the β€œ+x” value which represents the β€œ755” permission mode.
  • β€œfile6.txt” is the desired file name:

Step 6: Verify Added Chmod Permissions Files

Then, execute the provided command to ensure the added permission:

$ git ls-files --stage

It can be seen that the default file permission is successfully changed to the β€œ755” mode:

Step 7: View Added Chmod Permissions

Lastly, execute the β€œls -l” command to view the individual file permissions:

$ ls -l file6.txt

According to the below-provided output, the changed chmod β€œ100755” permission indicates that the file owner has the β€œread/write/search” permission. However, other members can only have the search permissions:

That’s all! We have effectively illustrated the process of adding chmod permissions to files in Git.

Conclusion

To add chmod permissions to a file in Git, first, navigate to the required Git repository. Then, set the file mode configuration value as β€œtrue” in the configuration file and ensure it. Next, list the stage files with their default permissions mode. After that, execute the β€œgit update-index –chmod=+x <file-name>” command. This guide demonstrated the method of adding chmod permissions to files in Git.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.