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.

itirazimvar
  • 859
  • 1
  • 10
  • 20
  • 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`. – lawlist Sep 03 '14 at 21:52
  • 4
    You can either [change to an SSH URL](http://stackoverflow.com/a/11028423/113848) or [create a `~/.netrc` file and keep using HTTPS](http://stackoverflow.com/a/14417827/113848). – legoscia Sep 04 '14 at 07:57
  • Definitely go ssh, if you don't have experience, this is walk you through it: https://help.github.com/articles/generating-ssh-keys – Jordon Biondo Sep 04 '14 at 11:57

2 Answers2

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.

tarsius
  • 8,147
  • 5
  • 32
  • 46
  • Thanks. I configure my ssh key from Github page from [github](https://help.github.com/articles/generating-ssh-keys) and also 1 times i used from terminal.Now it works. – itirazimvar Sep 09 '14 at 16:03
  • 1
    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. – tarsius Jun 14 '17 at 15:14
  • @tarsius 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) – Alexei Mar 22 '21 at 17:59
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://batman@github.com/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:ghp_123456789409234532843@github.com/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.

Alex028502
  • 3,486
  • 2
  • 23
  • 50