David Foie Gras
David Foie Gras

Reputation: 2080

How to do git commit using personal access token?

I made all permission reserved personal access token on github.

I saw that

git clone https://<username>:<personal_access_token>@github.com/<username>/<project_name>.git

works.

So How can I clone, commit, and push using personal access tokens?

Like this

cd /tmp
git clone https://<username>:<personal_access_token>@github.com/<username>/<project_name>.git

cd /tmp/auto_tutorial
git commit --allow-empty -m 'Trigger notification'
git push https://<username>:<personal_access_token>@github.com/<username>/<project_name>.git master

Upvotes: 22

Views: 55680

Answers (4)

Dankyi Anno Kwaku
Dankyi Anno Kwaku

Reputation: 1293

Login to your GitHub and go and setup a "Personal Access Token" at https://github.com/settings/tokens

After you have your personal access token, go to terminal and change your "origin" url as below

git remote set-url origin https://[email protected]/REPLACE-WITH-USERNAME/REPLACE-REPO-NAME.git/

If it is your first time starting your repo and haven't pushed before, then add your initial origin as below

git remote add origin https://[email protected]/REPLACE-WITH-USERNAME/REPLACE-REPO-NAME.git/

Upvotes: 34

Falaen
Falaen

Reputation: 383

For Mac

  1. Open Keychain Access
  2. Select Login

enter image description here

  1. Select Passwords and search for GitHub

enter image description here

  1. Double-click github.com

enter image description here

  1. Check the show password box

enter image description here

  1. Replace the password with your personal access token

  2. Save the changes

  3. Try git commit or git push, it should be working now!

Upvotes: 7

bk2204
bk2204

Reputation: 76489

While it is possible to use the personal access token in the URL like that, it's discouraged because (a) it stores your token in plaintext where it can be read and printed and (b) because it means you have to enter it for each repository.

It's better to use a credential manager to store your credentials by setting credential.helper to an appropriate value for your platform, and then entering your username when prompted and your token as the password. That will save your credentials for future use across all your projects. If you need to use multiple accounts, just use your username (but not your token) in the URL, and the credential manager will handle that.

The default credential managers you want to use on most platforms are manager or wincred on Windows, osxkeychain on macOS, and libsecret on Linux. You can also use store to store in a file on your local disk, which is available on all platforms, but less secure.

Once you've cloned the repository, you can just push with git push origin master, since the URL you've cloned from will be set to the remote origin.

Upvotes: 12

Alexander Santos
Alexander Santos

Reputation: 1691

Just push using your remote's name

i.e.: git push origin master

You can use git remote -v to check current remotes list.

When you clone using an address with personal access token, it gets added to this list.

Upvotes: 3

Related Questions