Home > Back-end >  In git, is it possible to specify that certain changes ought never be committed?
In git, is it possible to specify that certain changes ought never be committed?

Time:01-19

There's a project I'm working on that accesses an FTP server to upload an image. When developing locally, it can't access the FTP server as it is only accessible within a certain virtual private cloud.

In order to test it locally, I need to rewrite parts of the code to skip the FTP upload. The obvious answer would be to add some kind of check for an environment variable or something, and have it upload or not upload depending on the environment. However, when I've asked for permission to make this change, it has been consistently put off due to being low priority.

The problem is, I know that eventually I'm going to make these temporary changes and then accidentally commit them. I've done it once before but luckily realized my mistake in time to revert the changes.

Is there any way that I can make the necessary temporary changes, and then immediately git prevent-commit filename.php or something like that on the related files, so that git yells at me if I try to commit those changes in the future, or something along those lines?

CodePudding user response:

This is essentially a Git FAQ: How do I tell Git to ignore tracked files? It's important to emphasize that there is no "real" way to do this. However, setting the --skip-worktree flag on the index entry will be close enough for your particular purposes. You will need to remember to unset the flag at times. See also Git - Difference Between 'assume-unchanged' and 'skip-worktree'

CodePudding user response:

TL;DR

Pre-commit hooks are designed to do just that: refuse any future commits with some testable characteristics.

Details

You can create a pre-commit hook to reject any commits that contain some do-not-commit marker. Pre-commit hooks are run on your local machine, before actually creating the commit, so if they fail, you don't even get to write a commit message.

Here is the simplest pre-commit hook that would reject any commit with DO-NOT-COMMIT found in the changes being committed:

#!/bin/sh
exec 1>&2 # redirect output to stderr
if git diff --cached | grep DO-NOT-COMMIT; then
    echo "DO-NOT-COMMIT found - rejecting commit"
    exit 1
fi

Save this file as .git/hooks/pre-commit in your sandbox, make it executable with chmod x .git/hooks/pre-commit, and next time you try to commit a file with DO-NOT-COMMIT anywhere, Git will yell at you, as you desire.

Then, we you modify that program for local testing, start your changes with a # DO-NOT-COMMIT comment and you know you won't commit it by accident.

Install this in every sandbox

The nuisance with pre-commit hooks is that they have to be installed and enabled in each sandbox separately. So if you're looking for a solution for you personally, install the pre-commit hook in your sandboxes and you're good to go. But if you're looking for something organization wide, they're not the right tool. Server-side hooks would be needed in that case.

But that can also be a feature: pre-commit hooks are local and fully under your control. No need to get anyone's permission to enable them for yourself.

  •  Tags:  
  • Related