Commit&Push with a Different Username and Credentials — GitHub

Nil Seri
3 min readSep 19, 2021

--

How to Commit to a Different Project with a Different Username and Credentials

Photo by Andrew Neel on Unsplash

After creating a new project in GitHub (I prefer to create via GitHub web page), you can run these commands:

git initgit config user.name "your_github_username"git config user.email "your_github_email"git add .git commit -m "Initial Commit"git branch -M mastergit remote add origin https://your_github_username:access_token_provided_by_github@github.com/your_github_username/your_project_name.git

“config” commands can also take parameter “global” but here we intend to change our settings just for this local commit.

git config --global user.name "your_github_username"
git config --global user.email "your_github_email"

As of August 2021, you cannot run the latter command with your password but an access token; you will get an error if you run with “https://your_github_username:your_github_password…”:

your_project_name git:(master) git push -u origin master
remote: Support for password authentication was removed on August 13, 2021. Please use a personal access token instead.
remote: Please see https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/ for more information.
fatal: Authentication failed for 'https://your_github_username:your_github_password@github.com/your_github_username/your_project_name.git/'

How to Generate a Personal Access Token

You can follow the instructions from here.

In Github, Go to Settings — Developer Settings — Personal Access Token or directly to https://github.com/settings/tokens after login.

Click “Generate new token” button placed on the right side.

Give a name to your credential (I named it as “my_bash_script”).

You can select expiration duration (default is 30 days):

For just commit operations, selecting “repo” will be enough.

After generation, you will be e-mailed by github to be informed about the generation and your generated token will be shown on the screen with a copy option (copy and keep it safe):

You can click on your access token name to “Edit”.

There, you won’t be able to see your token again (for security reasons, obviously) but you can find “Regenerate token” button with a warning about the update.

If it expires, you can update remote url with your new token using this command:

git remote set-url origin https://your_github_username:your_new_access_token@github.com/your_github_username/your_project_name.git

Happy Coding!

--

--

Nil Seri
Nil Seri

Written by Nil Seri

I would love to change the world, but they won’t give me the source code | coding 👩🏻‍💻 | coffee ☕️ | jazz 🎷 | anime 🐲 | books 📚 | drawing 🎨

No responses yet