0

if you git commit without telling git who you are you get a message like this

$ git commit -m "From Qworp"

*** 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.

How can I make every single git commit prompt for user.name and user.email? something like

$ git commit -am "From Qworp"
user.name: Qworp
user.email: qworp@qworp.com
[master be42c9e] From Qworp
 1 file changed, 5 insertions(+), 5 deletions(-)
...
$ git commit -am "Now Ace"
user.name: Ace
user.email: ace@qworp.com
[master 56ec176] Now Ace
 1 file changed, 2 insertions(+), 1 deletions(-)

And set author and committer details based on that?

theonlygusti
  • 11,032
  • 11
  • 64
  • 119
  • 3
    That's not supported out of the box. Do you simply want to pretend to be someone else for each commit or is there some underlying problem that you're trying to solve? If it's the first one, I'd write a shell script to wrap `git commit`and make that change the config before each commit. Note that you can use `--author` to specify the author on the command line. – Joachim Sauer Mar 25 '21 at 15:10
  • @JoachimSauer yes, the first one exactly. Thanks for such a clear comment. I guess I'll take a look at wrapping it – theonlygusti Mar 25 '21 at 15:13
  • What do you want to accomplish? Different defaults for different repos? I use [conditional includes](https://git-scm.com/docs/git-config#_conditional_includes) for that purpose... – kapsiR Mar 25 '21 at 15:20
  • @kapsiR: that could already be solved by simply configuring those values per-repo, no need to complicate it further. – Joachim Sauer Mar 25 '21 at 15:26
  • @JoachimSauer Yes but not for every new repo. If I would rely on that with e.g. the global defaults, then it wouldn't work for me - that's the reason why I have folder based defaults. – kapsiR Mar 25 '21 at 15:32
  • do you want the commiter or the author to be changed? – OznOg Mar 25 '21 at 19:25

1 Answers1

1

Usually a git hook would be the way to go. But even in the first git hook pre-commit that runs, the user is already set. Therefore, use the following wrapper around git. Define this function inside your .bashrc and/or .bash_profile.

git() {
  local opts=()
  if [ "$1" = commit ]; then
    read -p "user.name: " name
    read -p "user.email: " email
    opts+=(-c user.name="${name}" -c user.email="${email}")
  fi
  command git "${opts[@]}" "$@"
}
user3840170
  • 26,597
  • 4
  • 30
  • 62
Socowi
  • 25,550
  • 3
  • 32
  • 54
  • Why did you change to a bash answer instead of git pre-commit ? – theonlygusti Mar 26 '21 at 12:20
  • 1
    Thank you for the note and please excuse my poor tests. Git hooks do not have stdin by default. You can read stdin using [`exec < /dev/tty`](https://stackoverflow.com/a/10015707/6770384). However, I found that `git config user.name` does not affect the current commit inside `pre-commit`, but only future ones. My bad. Seems like git hooks aren't the right approach here, so I switched to a wrapper function. – Socowi Mar 26 '21 at 12:20
  • Can a pre-commit not change the author/committer for the current commit? – theonlygusti Mar 26 '21 at 12:21
  • I cannot say for sure. But so far I haven't found a way. Maybe a `git commit --amend` in a `post-commit` hook works, but that would be error prone if the users aborts a commit midway using ctrl+c. – Socowi Mar 26 '21 at 12:22
  • Thanks for coming back to this – theonlygusti Mar 26 '21 at 12:24
  • @user3840170 why is it not safe to run git many times in a row/ – theonlygusti Mar 26 '21 at 23:37