Bash – how to prevent git from using logged in username

bashgit

I would like to make it possible for any user to clone a repository without having to specify the username in the clone command.

For example, say my git username is john.doe (e.g. customer requirement) but the local account I am logged into on my PC is j.doe (e.g. company requirement), git will attempt to use j.doe to clone the repository.

This is quite a subtle but significant different so I've used the following 2 examples to illustrate:

1. Cloning with anonymous syntax:

If I do the following:

j.doe~
$ git clone https://mygit.repo.url/somerepository.git

… git assumes without asking that the remote username is the same as the username of the local account (i.e. j.doe) and attempts to clone as follows :

Cloning into somerepository
j.doe@mygit.repo.url's password:

but, in this case, j.doe is not an account on the remote repository.

2. Cloning with explicit username:

The usual workaround is to specify the remote username as follows:

j.doe~
$ git clone https://john.doe@mygit.repo.url/somerepository.git

This succeeds but requires that every user uses a different clone command.

Why do I care?

There are several cases where the anonymous syntax would be more suitable, for example:

  1. It is more portable to automate – no need to write OS or language specific input statements to grab the username.
  2. It is more consistent for the user. The user/password request will always look the same.
  3. It makes it easier to write clear and consistent installation instructions. The user may copy and paste.
  4. Prevents having to write a different wrapper for every scripting language and OS that tries to automate the procedure.
  5. When checking out a repository that relies on git submodules the .gitmodules files must first be edited to include the username if the logged in user is not the same as the git user. This is very inconvenient in the case of multiple users.

Case in point.

I know this functionality is possible because github use it. For example, if you clone a repository on github using the anonymous syntax you will see something like the following:

j.doe~
$ git clone https://github.com/dbostock/d3.git
Cloning into 'd3'...
Username for 'https://github.com':

Pretty nice! Using any shell, any OS, any scripting language, I can identify, authenticate and clone my repository.

So in the end, my question boils down to : How do github do this?

Best Answer

Root can use sudo just like any other user can. Either that, or just change ownership of the files after the clone operation.

Is there a way, either from the client or server, to configure a git repository so that it always asks for the username?

Git will use the username either as specified in ~/.git/config or as specified in your repository URL. Keep in mind, though, that this is the remote system username, not the local username, so it will not affect ownership at all.

The solution here is to use sudo for your git clone operation.