Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do git commit using personal access token?

Tags:

git

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
like image 398
touchingtwist Avatar asked Jun 03 '20 15:06

touchingtwist


4 Answers

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/
like image 108
Dankyi Anno Kwaku Avatar answered Oct 17 '22 01:10

Dankyi Anno Kwaku


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.

like image 21
bk2204 Avatar answered Oct 17 '22 00:10

bk2204


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!

like image 41
Falaen Avatar answered Oct 17 '22 00:10

Falaen


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.

like image 43
Alexander Santos Avatar answered Oct 17 '22 00:10

Alexander Santos