16

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
Jadim
  • 182
  • 2
  • 12
touchingtwist
  • 1,930
  • 4
  • 23
  • 38

4 Answers4

27

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://REPLACE-WITH-TOKEN@github.com/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://REPLACE-WITH-TOKEN@github.com/REPLACE-WITH-USERNAME/REPLACE-REPO-NAME.git/
Junior
  • 1,007
  • 4
  • 16
  • 26
12

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.

bk2204
  • 64,793
  • 6
  • 84
  • 100
  • 1
    This sounds close to what I need but I don't know how to "by setting credential.helper" and "libsecret on Linux". I have a raspberry pi robot that I do regular "git push, username,password" and git is telling me I need to use token after Aug 2021. I created developer token, but need to tell git on my robot what the token is and have "git push" not ask me for the credentials. – Cyclical Obsessive May 08 '21 at 01:44
  • 2
    You can set a credential helper using `git config credential.helper store`. The `libsecret` credential helper will require a desktop environment, usually GNOME, MATE, or similar, so `store` is probably better for you. And you may also wish to use an SSH with a deploy key instead. Answering in more detail is hard in the comments, so if you're unsure, please ask a new question. – bk2204 May 08 '21 at 02:02
6

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!

Falaen
  • 363
  • 4
  • 13
3

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.

Alexander Santos
  • 1,458
  • 11
  • 22