Git

How to Update and Commit Only a File’s Permissions Using Git Version Control

Git is the popular versioning control tool among developers that tracks the GitHub changes to the local machine. Developers can create and update multiple files and folders in the Git repositories. Moreover, they can also allow changing the file permissions mode. When files are created, they have the β€œ100644” chmod permissions mode, which means the file owner can read and write. However, other members can just read the file. Similarly, multiple modes are available with different permissions, such as β€œ100755” and β€œ100777”.

This post will discuss about updating and committing only file permissions using Git version control.

How to Update and Commit Only a File’s Permissions Using Git?

To update and commit only file permissions using Git version control, check out the following steps:

  • Move to the required repository.
  • View the list of content.
  • Display the file where HEAD is pointing with default permissions by running the β€œgit ls-tree HEAD” command.
  • Execute the β€œgit update-index –chmod=+x <file-name>” command.
  • Lastly, commit changes and verify them.

Step 1: Redirect to Desired Repository

At first, switch to the particular Git repository by running the β€œcd” command:

$ cd "C:\Users\nazma\Git\naz-test"

Step 2: List Available Content

Next, execute the β€œls” command to display the all existing content of the current working repository:

$ ls

According to the below-given output, repositories contain β€œfile1.txt” and β€œfile2.py” files:

Step 3: Display HEAD Pointing SHA-Hash

Now, to show the SHA-hash of the commit where HEAD is pointing which contains the file by utilizing the β€œgit ls-tree” command:

$ git ls-tree HEAD

As you can see, the β€œfile1.txt” contains the β€œ644” permissions mode by default:

Step 4: Update Index

After that, execute the β€œgit update-index” command to update the permissions of the file where HEAD is pointing:

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

Here, the β€œ–chmod=” will take the desired permissions mode. For instance, we specified the β€œ+x” which indicates the β€œ755” permission mode:

Step 5: Commit Permission Updated Changes

Finally, commit the added changes to the Git repository by running the β€œgit commit” command along with the particular commit message:

$ git commit -m "file permissions change"

It can be observed that specifically file mode changes are committed:

Step 6: Verify File Change Permissions

Lastly, use the β€œgit ls-tree” command along with the β€œHEAD” pointer:

$ git ls-tree HEAD

As you can see, the permissions of the β€œfile1.txt” are changed successfully from β€œ644” to β€œ755” mode:

That’s it! We have explained the easiest way to change and commit file permissions using Git.

Conclusion

To update and commit only file permissions using Git version control, first, move to the required repository and list its content. Then, execute the β€œgit ls-tree HEAD” command to display the file where HEAD is pointing with the default permissions. After that, use the β€œgit update-index –chmod=+x <file-name>” command. Lastly, commit changes and verify them. This post demonstrated the method of updating and committing only file permissions using Git version control.

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.