Terminal

Bash scripts, command line tips, etc.

Using git config to force credentials for HTTP git urls

Storing the user name

When your project contains submodules or other predefined git URLs in HTTP form, without a user name, cloning will fail unless the project is a public one.

# Try this!
git clone https://github.com/Example/something.git/
# ...and it will fail if it isn't public: remote: Repository not found.

# Normally you can add the user name:
git clone https://username@github.com/Example/something.git/
# ...but this isn't an option for submodules, pods, or other hard-coded URLs

This is where using ~/.gitconfig becomes useful. Do the following:

# Edit your local git configuration file
nano ~/.gitconfig
# Now add this to the config file (change github.com to whatever your git http domain is):
[credential "https://github.com"]
	username = yourgithubusername

Next time you clone anything from Github via http method, git will try to authenticate with yourgithubusername and prompt you for the password.

Storing the password

You can also store the full credentials (password included) for git Terminal commands. Be aware that this will store the password in clear text in the .git-credentials file - so don't use this method unless absolutely necessary! It is also probably a good idea to remove the credentials file after use.

[credential "https://github.com"]
        username = yourusername
        helper = store
        # For security: remove the helper = store once you are done and remove the .git-credentials file!