Eoaioaou
Eoaioaou

Reputation: 71

Git allow / prevent commit and push of certain files

Is there a way in Git to define special rights to a certain file or folder?

I have a Node project that is built using a Docker-Compose YML file. My intention is that the team working on this project can modify any code, except the Docker-Compose YML file. I would like to prohibit pushes of that file to anyone except me. Is there a way to achieve this in Git?

Upvotes: 1

Views: 1593

Answers (3)

bk2204
bk2204

Reputation: 77014

There is no intrinsic way to do this with Git, but there are tools that can help.

  1. You could configure your CI system to check pushes and fail if someone other than you pushes to that file.
  2. You could configure your hosting solution, such as GitHub, to use the code owners functionality and require an approval from you before merging.
  3. If your hosting solution supports it, you could configure a pre-receive hook (possibly with push certificates or commit signing) and reject pushes from anyone other than you.
  4. You could clearly communicate your expectations to the team and rely on them honoring your wishes.

Upvotes: 2

Aleksandr Medvedev
Aleksandr Medvedev

Reputation: 9008

You can set the skip-worktree flag for restricted files like this:

git update-index --skip-worktree path/to/the/file

That gives a little different effect from what you need, but provided proper convention within the team, it should pay off: skip-worktree makes git ignore any local changes made to a specified tree (files), so no matter if your teammates change it or not, git does not track these changes. However git still tracks remote changes, so git pull command will update content of the files.

The main disadvantage is that this flag should be set locally on all machines you need those file local changes to be ignored on. For the machines you going to have possibility to push those files, just do not set this flag, or unset it with the following command:

git update-index --no-skip-worktree path/to/the/file

Upvotes: 0

Mark Bramnik
Mark Bramnik

Reputation: 42551

Yes, you can use .gitignore file - this file will contain the files (paths) that should be ignored by git during commits

This file should be a part of your project (you should commit and push it) so that everyone in your project will get it.

To "suppress" gitignore you can git add --force docker-compose.yml - so that only you'll use this command.

Upvotes: 0

Related Questions