Home > Blockchain >  Change the Account that is used to git pull origin
Change the Account that is used to git pull origin

Time:03-11

I have tried to check several SO questions and answers but still unable to resolve my concern. The scenario is this:

User A with Git Account A git cloned repo to freshly installed server. 
Because of this, the git account that was registered on the server was User A. 
(If I understood it correctly. please correct me if I am wrong in this part)
Now I would like to use User B with Git Account B as commiter/puller/pusher to the server.

What I tried was to change the user and email with the following command:

git config --global user.name "userb"
git config --global user.email "[email protected]"

git config --local user.name "userb"
git config --local user.email "[email protected]"

git config user.name "userb"
git config user.email "[email protected]"

On 3 separate occasions. Still unable to resolve my concern. I can see the updated user and email with command git config --list, git config user.name and git config user.email so I know that the value did change.

If this is not possible, I am also considering to remove the accounts all together and enter the user/email and password when pulling/pushing without removing the git history.

CodePudding user response:

You are doing the wrong thing. You are trying to change the user name used to mark your commits, instead you have to reset your git user account credentials.

Under Linux issue git config --unset credential.helper, under Windows remove the credentials by the Windows Credentials Manager.

CodePudding user response:

User A with Git Account A git cloned repo to freshly installed server. Because of this, the git account that was registered on the server was User A.

No. There are two common / standard transport mechanisms that a Git client (like git clone) will use to talk to a Git server:

  • https: the client provides a user name and additional authentication data (password, token, whatever); or
  • ssh: the client provides a public key; the server looks up the public key to determine who the client claims to be,1 and challenges the client with a task that can only be completed by someone holding the corresponding private key, so that if the client does complete the task, the claim must have been accurate.

These mechanisms are provided not by Git itself, but by some sort of access wrapper: a web server, or an sshd.

At this point, the client is authenticated to the server, and only now does Git itself actually enter the picture. The server's Git software hands to the client every commit in the server's repository (so that the client has all the commits), and shows to the client all the branch names (which the client then changes into remote-tracking names, so that the client has all the commits and no branches at all). Then the client disconnects from the server, creates one branch in the new Git repository, and is done.

The only thing retained here is the URL that the client used to reach the server. This URL is retained in the Git repository the client just created. Unless the server keeps logs (via its web server and/or sshd),2 the server now has no record at all of the client.

The next time the client needs to talk to the server, the client provides the URL, which it has saved conveniently under the short name origin.3 This URL may contain a user name, especially if you used an https:// URL.

So: check the URL, using git remote -v, to find out which protocol you are using and whether, if that protocol is https://, there is a user name embedded in the URL. If so, you can edit or remove that user name. If not, and the URL is an https:// URL, proceed with Antonio Petricca's answer. If the URL is an ssh:// one, look into ssh authentication.


1On some servers, the user logs in using their own account, but for the usual GitHub, GitLab, and Bitbucket setups, the user provides the generic user name git. Hence the server has to use this public-key trick to figure out who the user is claiming to be.

2Most servers do keep logs, but that's up to the server, and they're used at most for auditing and security. It does not affect future attempts to connect to the server, unless, e.g., the people running the server find your connection alarming and block it.

3You can choose some other name, but there is no reason to do that, and presumably you did not.

  • Related