Magit is a good option for using Github. When I want to push my commits to Github, it always asks my username and password. How can Emacs save my password?
Best regards.
Magit is a good option for using Github. When I want to push my commits to Github, it always asks my username and password. How can Emacs save my password?
Best regards.
This hasn't much to do with Magit. If you configure Git correctly, then it also works in Magit. Either use a ssh key or credential.helper. I recommend the former, but then you also have to configure an ssh-agent and how that is done might differ between distributions.
Here is what worked for me:
In the git configuration of the project (.git/config
), add a username (but not a password).
look for a line that looks like this:
url = https://github.com/magit/magit.git
(except it will be your repo origin and not magit's)
and add your username to the url. On github, it doesn't matter what username you use. Just make one up.
url = https://[email protected]/magit/magit.git
side note - if you want to follow the accepted answer to this question, just add your password or PAT after the username and a colon, and you are done url = https://batman:[email protected]/magit/magit.git
But if you don't want to do that (explanation later) continue:
add this to the emacs config file:
(add-hook 'magit-process-find-password-functions
'magit-process-password-auth-source)
Then because I am already using tramps, and have already saved an ssh password, I already have a file called ~/.authinfo
. If you don't, maybe try adding one? Or try using tramps and see if it appears?
Then add this line to it
machine github.com login batman password ghp_123456789409234532843
I guess the made up login in this file has to match the made up username in the git configuration. This actually worked for me with no login in authinfo
, just machine github.com password ghp_123456789409234532843
, except if you have multiple github accounts and PATs, I hope adding the login will get the right PAT for the account every time.
The accepted answer https://stackoverflow.com/a/25675158 is probably what most people who find this question should do.
However, I am working on a raspberry pi over tramp, and the pi is left on a desk, and unencrypted, so I am not comfortable leaving a github PAT on it - which is how I found this question in a duckduckgo search.
My development machine is encrypted. And hope that this way if somebody took the SD card out of the target machine, they would not be able to break into my github account with it.
Most of what I needed to answer was here https://emacs.stackexchange.com/questions/41616/how-to-save-remote-repo-credentials and https://emacs.stackexchange.com/questions/40994/using-auth-source-with-magit-and-bitbucket (also with help from Tarsius). But I added it here anyway in step by step form.
© 2022 - 2024 — McMap. All rights reserved.
magit-process-password-prompt
has a line that reads(process-send-string proc (concat (read-passwd prompt) "\n"))
. You can substitute(read-passwd prompt)
with your own hardcoded password -- e.g.,(process-send-string proc (concat "12345678" "\n"))
. The procedure is similar for the username by modifying the functionmagit-process-username-prompt
. – Curtcurtail~/.netrc
file and keep using HTTPS. – Penzance