LeMoussel LeMoussel - 26 days ago 5x
Git Question

How can I determine username from the Url of a remote repository?

Before to pull a project from Git repository, I want to check if the git user name is in the URL of remote repository.

Eg: https://usernameAA@remote => if git username == "usernameAA" OK to pull.

I can get the URL with

git ls-remote --get-url
and finding username with
git config user.name

How can I determine if username from the Url of a remote repository is equal to username to associate commits?


Why not extract the username from the url returned by git ls-remote --get-url:

username=$(git ls-remote --get-url origin | cut -d@ -f1 | cut -d/ -f3| cut -d: -f1)

The last cut -d: -f1 is in the (improbable) case where the url includes the username and password: http://username:password@domain.com/...