Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override configured user for a single git commit

Tags:

git

github

People also ask

How do I override a git commit?

If you've already created a fresh commit, you'll want to use git rebase -i to squash your commit on top of the old one. After you've made this change locally, and verified your commit looks the way you want it to, you'll have to git push --force to overwrite history on the Github remote.

How do I commit to another user?

Creates a new commit by the specified author. Use git commit -m <message> to create a new commit with the specified <message> . Use the --author option to change the <name> and <email> of the commit's author.

What command is used to create local user credentials needed for a commit?

Alternatively, you can do this for only a single command, using the -c option: git -c "user.name=Your Name" -c "user. email=Your email" commit ...


First, the author is not necessarily the same as the committer. Git tracks both.

To set what name to use for just this repository, you could do:

git config user.name "Your Name"
git config user.email "Your email"

Notice the absence of the --global option. This will set the configuration in this repository only.

Alternatively, you can do this for only a single command, using the -c option:

git -c "user.name=Your Name" -c "user.email=Your email" commit ...

But I think it's better to just use the config options above.


Try to prepend this to your git command :

git -c [email protected] -c user.name='Your Name'

This is ok for a one-shot, but you could prefer save this in the .git/config file of the local repo you cloned from git. This can be done by running git config (no --global or --system to make it "local to the repo") or by editing the .git/config (it's the same syntax as ~/.gitconfig


You could also set environment variables GIT_COMMITTER_NAME, GIT_COMMITTER_EMAIL, GIT_AUTHOR_NAME, and GIT_AUTHOR_EMAIL on a per-shell or even per-command basis.


I believe you could run:

git config user.name "Your Name"
git config user.email [email protected]

for different parts of git. Try opening the folder where the work is being done for the repo to the github server.

Make sure that you see a .git folder in that work folder. Now open that folder in Terminal/Console/CommandPrompt and try changing the username and email for that folder only by running the commands I specified above.

By changing that, you may now be able to access the GitHub server.

Take a look here: https://help.github.com/articles/setting-your-username-in-git‎

Also, I believe there is a way to push to a remote repo by using your github username and password in the command itself, but I'm not quoting it because it didn't work for me.


Following up on Jesse Glick's answer here with an expanded implementation because IMO his suggestion is the best way to go for this.

Add the below to your .bash_profile. It will prompt you for a user when first running any git command in the active shell. It'll then remember the user for all subsequent runs.

(obviously cross reference an input user value to exact desired names and email addresses for the various git auther/name values in the case block and update the assigns as needed)

git() {
  echo "Running BETTER git..."
  if [ -z "$GIT_COMMITTER_NAME" ]; then
    while true; do
      read -p "Git User: " UNAME
      case $UNAME in
          user1 ) break;;
          user2 ) break;;
          * ) echo "Invalid User";;
      esac
    done
    GIT_COMMITTER_NAME=$UNAME
    export GIT_COMMITTER_NAME
    [email protected]
    export GIT_COMMITTER_EMAIL
    GIT_AUTHOR_NAME=$GIT_COMMITTER_NAME
    export GIT_AUTHOR_NAME
    GIT_AUTHOR_EMAIL=$GIT_COMMITTER_EMAIL
    export GIT_AUTHOR_EMAIL
  fi
  echo "  using git user: $GIT_AUTHOR_NAME / $GIT_AUTHOR_EMAIL"
  /usr/bin/git "$@"
}

You could also set environment variables GIT_COMMITTER_NAME, GIT_COMMITTER_EMAIL, GIT_AUTHOR_NAME, and GIT_AUTHOR_EMAIL on a per-shell or even per-command basis.

Actually, since Git 2.22 (Q2 2019) you have a new option: Four new configuration variables {author,committer}.{name,email} have been introduced to override user.{name,email} in more specific cases.

See commit 39ab4d0 (04 Feb 2019) by William Hubbs (``).
(Merged by Junio C Hamano -- gitster -- in commit 4e021dc, 07 Mar 2019)

config: allow giving separate author and committer idents

The author.email, author.name, committer.email and committer.name settings are analogous to the GIT_AUTHOR_* and GIT_COMMITTER_* environment variables, but for the git config system.
This allows them to be set separately for each repository.

Git supports setting different authorship and committer information with environment variables. However, environment variables are set in the shell, so if different authorship and committer information is needed for different repositories an external tool is required.

This adds support to git config for author.email, author.name, committer.email and committer.name settings so this information can be set per repository.