Magit save password and username
Asked Answered
T

2

9

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.

Thymic answered 3/9, 2014 at 21:30 Comment(3)
You will undoubtedly get other answers, but here's my unofficial comment based upon a home installation where security is not an issue. The function 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 function magit-process-username-prompt.Curtcurtail
You can either change to an SSH URL or create a ~/.netrc file and keep using HTTPS.Penzance
Definitely go ssh, if you don't have experience, this is walk you through it: help.github.com/articles/generating-ssh-keysNeidaneidhardt
S
16

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.

Schulze answered 4/9, 2014 at 21:19 Comment(3)
Thanks. I configure my ssh key from Github page from github and also 1 times i used from terminal.Now it works.Thymic
Or maybe I was just trying to ensure that every Magit question had an adequate answer (by answering those that don't) [as a free service to the users] and in the process kept coming back to this question because its answer was not properly marked as such. I couldn't care less if I had or had not a few additional brownie points.Schulze
@Schulze This help: To change the default password cache timeout, enter the following: $ git config --global credential.helper 'cache --timeout=3600' # Set the cache to timeout after 1 hour (setting is in seconds)Weasand
A
0

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.

Aide answered 31/1, 2023 at 14:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.