LeMoussel LeMoussel - 1 year ago 76
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?

Answer Source

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/...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download