0

Say, I create a new repo, and try to commit. Git shows me

*** Please tell me who you are.

Run

git config --global user.email "you@example.com" git config --global user.name "Your Name"

to set your account's default identity. Omit --global to set the identity only in this repository.

Without reading the last line, I run the two lines suggested by git (with --global) and happily commit.

A week later I create a new repo for a completely unrelated project where I'd need to commit under a different user.email. Without even thinking about user.email, I try to commit, it goes through and I push to the origin.

The above happened to me twice and made me think whether there is a way to disallow git config --global.

For example, if I run

git config --global user.email "you@example.com"

I'd like bash not to run the command and show me a warning, but when I run

git config user.email "you@example.com"

I'd like it to go through.

Is it possible?

Leo
  • 103
  • 2
    This question is akin to https://unix.stackexchange.com/questions/61927 and https://unix.stackexchange.com/q/162845/116858 in that it asks how to make the system babysit an absentminded user. In the general case, this is not wanted, impossible to enforce, and never solves the underlying problem. – Kusalananda Dec 30 '21 at 19:05
  • 1
    @they, it seems very much wanted in this case. That is, wanted by the party that actually matters, the person asking the question and ending up as the user of that functionality. – ilkkachu Dec 30 '21 at 19:08
  • 1
    @ilkkachu Of course it's wanted. It's always wanted by the user that have just made a boo boo. No question about that. They want to not have made the mistake. The solution is not technical, though, it's learning not to do the mistake in the future. There can't really be a technical solution to avoiding typing in legal commands and feeling the effect of them, other than not typing those commands in. Sure, set something up to prevent it, but they'll start relying on it to the degree that the "fix" becomes a life-line, and then it backfires as soon as they move to a new installation. – Kusalananda Dec 30 '21 at 19:13
  • 1
    @they, that's for them to judge. Not for some unnamed faceless passive-voice jury. – ilkkachu Dec 30 '21 at 19:14
  • @ilkkachu Sure. I'm just providing commentary. – Kusalananda Dec 30 '21 at 19:25

1 Answers1

2

Simplest is to make a function for this one case (for Bash):

git() {
    for arg in "$@"; do
        if [[ $arg == --global ]]; then
            echo "Don't use '--global'!"
            return 1
        fi
    done 
    command git "$@"
}

Note that that wouldn't check if --global there is actually an option, and not e.g. an option-argument to another option and wouldn't matter in the least if git is run from somewhere other than the shell. Also if you make a git alias to include --global, it won't be caught by this, etc. Obviously you can also circumvent the protection by running command git or /usr/bin/git, or by just undefining the function.

ilkkachu
  • 138,973