-1

I use a laptop that contains an old configuration: git global credentials

git config --global user.email => blockeduser@email.com

so when I push my first commit with my username & password, I found the blocked user in my commit detail how this can happen? is there a way to prevent this in the configuration of Gitlab?

NB now I changed the global config, but I m asking if there is a way to prevent this from happening in the future by other users

OAH
  • 1,160
  • 2
  • 11
  • 24
  • AFAIK blocking a user just stops them from doing anything with the repository. But that doesn't mean that *you* can't upload commits that are attributed to them. Note that committer/author information in git is almost entirely unrelated to how users authenticate to the server. – Joachim Sauer Sep 16 '20 at 11:49
  • Change the git config? – matt Sep 16 '20 at 11:51
  • @matt, of course, I change it , the problem is in the first time i didn't even know – OAH Sep 16 '20 at 11:52
  • So what’s the question? – matt Sep 16 '20 at 11:54
  • I m asking if there is a way to prevent this from happening in the future by other users – OAH Sep 16 '20 at 11:55

1 Answers1

2

[edit] I wrote my long-ish answer, and then I saw you mentioned gitlab.

By googling "gitlab block author", I landed on this page :
https://docs.gitlab.com/ee/push_rules/push_rules.html#enabling-push-rules

There is a push rule :

Check whether author is a GitLab user
Starter 7.10
Restrict commits by author (email) to existing GitLab users.

See if you can activate that one on your server or account.

If you (or any user) have created locally a commit with the wrong user account, though, you would still have to edit this commit as below before pushing.


First : there are two places where something that looks like a username is mentioned :

  1. the information stored in a commit (the author of this commit is blockeduser@email.com)
  2. the credentials used to pull/push to a remote (the username in https://blockeduser@github.com, or the username associated to the ssh key when going through ssh)

1. comes from the configuration parameters git config user.name and git config user.email,
2. comes from the urls you use to acces your remote, and some configuration on your system (e.g : ~/.ssh/config if you use ssh, possibly the credential manager used by git, etc ...).


When you say that the user is "blocked" you probably mean that 2. wouldn't work anymore if you used this user's credentials.

For 1. however : even if blockeduser is now blocked, it is legitimate to keep the information on past commits which were created/edited by this user as is.
You may take some actions to refuse new commits where blockeduser is the committer, but most of the time no check is applied on the author/committer of a commit.

The general rule is :

  • update git's configuration to have the correct author/committer set in new commits you created,
  • if you mistakenly created some commits with the other guy's email, you can edit these commits, and push -f the result.

To do that, see for example these SO questions :

How to change the commit author for one specific commit? :

git commit --amend --author="Author Name <email@address.com>" --no-edit

How to change the author and committer name and e-mail of multiple commits in Git?

Spot in your history the id of the first commit to rewrite (say: eacf32) and replace <parent> below with eacf32^ :

git filter-branch --env-filter '
OLD_EMAIL="your-old-email@example.com"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="your-correct-email@example.com"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_COMMITTER_NAME="$CORRECT_NAME"
    export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_AUTHOR_NAME="$CORRECT_NAME"
    export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
'   <parent>..HEAD

If said commits are already merged or included in other people's work, it may be very cumbersome to change that, and you may have to live with commits attributed to the wrong author.

LeGEC
  • 46,477
  • 5
  • 57
  • 104