Is there a way to lock individual files or directories on fork when using git?

GitGithubGithooksGithub Api

Git Problem Overview


We are a team of 60+ developers working on the same product and are moving from SVN to Git and GitHub. We have a process in SVN where in individual files are locked and whenever a developer wants to commit code, he needs to get it unlocked by the owner of the file. Three of us are the owners of the total 150+ files. The unlocking is preceded by a code review.

In Github, we are planning to use the Fork-Clone model - each project a group of dev is working on will do a fork, each developer will do a clone of the fork, write the code & commit to origin, the lead of the feature will do a pull request to upstream.

Though this seems fine, the problem is when a big project gets delivered, it brings in lots of changes for review and hence, increases the load for the file owners. Also, this might happen in the later cycles of development and hence the project might be jeopardized.

One method we thought might work is to have hooks when the git push is done to the origin (fork). There can be one final review git pull to upstream.

However, we could not find any github extensions or push hooks for the same. Is there a quick way (read, existing extension) to do this with Github or should we use the same hooks that we would use with git?

Git Solutions


Solution 1 - Git

No chance, if file is not mergeable and you need to lock it, use a centralized solution instead of GIT, i.e. SVN or ClearCase.

Solution 2 - Git

If you are using git LFS (which is supported by some git hosting providers, like GitHub) you could use File Locking.

Mark a file type as lockable by editing the .gitattributes file:

*.docx lockable
# Make MS Word files lockable

And lock it with:

$ git lfs lock example.docx

You can unlock your files with git lfs unlock example.docx and those of somebody else by adding --force.

Solution 3 - Git

this is possible. git-lfs 2.0 introduces the ability to lock files: see these links: https://github.com/git-lfs/git-lfs/wiki/File-Locking. Support for this feature is available starting from TFS 2017.2: https://docs.microsoft.com/en-us/vsts/release-notes/.

Solution 4 - Git

Not exactly locking, but Github has introduced a concept called "Code Owners". Allows you to restrict part of your codebase to only allow commits after review by the code owners

Solution 5 - Git

Git does not provide any locking functionality, since it is decentralized. However, if you host your code on GitLab Enterprise Edition Premium, you can use the web interface to lock individual files or folders, achieving exactly what you want to do.

If you do not want to host your project on someone else's server (their website), you can also download GitLab and host it on your own webserver.

Solution 6 - Git

You can use LFS and you could lock individual files, or instead jus add the files to .gitattributes file,

https://github.com/git-lfs/git-lfs/wiki/File-Locking

Solution 7 - Git

This use case is one of the reasons Git is so much better than SVN --> rebase! If you follow good git workflow you rebase from upstream before submitting your Pull Requests. You don't need to worry about file locking and stomping on another person's commits and merge conflicts etc... a rebase sets your work aside, applies the remote commits and then applies your work on top.

I think this just takes a rethinking in your process and relying on the strengths of git versus force fitting a Subversion workflow on top of git. Your "fork-clone" model might need another look as well. Most often every developer has their own fork, you can share repos via remotes between teams if you want. But contributors sharing the same origin sets up some bad habits.

Gitflow is a very popular git workflow, and Github themselves has some nice tips and shares their workflow.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKarthick SView Question on Stackoverflow
Solution 1 - Gituser5694595View Answer on Stackoverflow
Solution 2 - GitNijin22View Answer on Stackoverflow
Solution 3 - Giterradi mouradView Answer on Stackoverflow
Solution 4 - GitMichaelView Answer on Stackoverflow
Solution 5 - GitLeonardo TorokView Answer on Stackoverflow
Solution 6 - GitCamilo PinedaView Answer on Stackoverflow
Solution 7 - GitKyle CamposView Answer on Stackoverflow