Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git how to prevent local modification of public commits

I already added receive.denyNonFastforwards and receive.denyDeletes on my git central repo server. Now I would like to block local history modifcation if the commit was already pushed to the central repo (just like mercurial does by default), I suppose I can use a hook but I couldn't find any example.

Is this a strange configuration?

It sounds like a basic safeguard that anyone using git should have activated and I'm quite surprised for the lack of example hooks.

like image 268
angelodiego Avatar asked Apr 02 '15 09:04

angelodiego


People also ask

How do I stop local changes in git?

Look at git stash to put all of your local changes into a "stash file" and revert to the last commit. At that point, you can apply your stashed changes, or discard them.

How do you remove last commit but keep changes?

In order to undo the last Git commit, keep changes in the working directory but NOT in the index, you have to use the “git reset” command with the “–mixed” option. Next to this command, simply append “HEAD~1” for the last commit.

How do I exclude changes in a commit?

Set “–assume-unchanged” to a path to exclude to check on git commit and it will exclude your file from git commit. You will need to use the git update-index and –assume-unchanged to exclude files from git commit.

How do you dispose of local changes?

There are two Git commands a developer must use in order to discard all local changes in Git, remove all uncommited changes and revert their Git working tree back to the state it was in when the last commit took place. The commands to discard all local changes in Git are: git reset –hard. git clean -fxd.


Video Answer


1 Answers

You can prevent local history modification by using Client-Side Hooks, much like you would with Server-Side Hooks.

Here are some examples of client-side hook code. Near the end of that page is a pre-rebase script that does something close to what you're looking for.

Note that client-side hooks cannot be added to a repository in such a way that they will automatically be set up in downstream repos:

Because hooks aren’t transferred with a clone of a project, you must distribute these scripts some other way and then have your users copy them to their .git/hooks directory and make them executable. You can distribute these hooks within the project or in a separate project, but Git won’t set them up automatically.

like image 138
Tyler Hoppe Avatar answered Oct 07 '22 15:10

Tyler Hoppe