如何实现 Github 免密码 git push 内容

如何实现 Github 免密码 git push 内容

最后修改于 2022-5-5 ⋅ 共 595 字 ⋅ 2分钟 / #Tutorial / #Git, #Github, #Ssh

Contents from Github Help.

Git Cnfig as a backup #

This part is a backup of the git config of my hugo blog.

./REPO/*

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
[core]
        repositoryformatversion = 0
        filemode = false
        bare = false
        logallrefupdates = true
        symlinks = false
[submodule "public"]
        url = git@github.com:USERNAME***/USERNAME***.github.io.git
        active = true
[remote "origin"]
        url = git@github.com:USERNAME***/REPO***.git
[branch "master"]
        remote = origin
        merge = refs/heads/master

./XXX/public/

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
[core]
        repositoryformatversion = 0
        filemode = false
        bare = false
        logallrefupdates = true
        symlinks = false
[remote "origin"]
        url = git@github.com:USERNAME***/USERNAME***.github.io.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master

Generating a new SSH key #

  1. Open Git Bash.
  2. Paste the text below, substituting in your GitHub email address.
1
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

This creates a new ssh key, using the provided email as a label.

1
> Generating public/private rsa key pair.
  1. When you’re prompted to “Enter a file in which to save the key,” press Enter. This accepts the default file location.
1
> Enter a file in which to save the key (/c/Users/you/.ssh/id_rsa):[Press enter]

At the prompt, type a secure passphrase. For more information, see “Working with SSH key passphrases”.

1
2
> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]

Adding your SSH key to the ssh-agent #

Before adding a new SSH key to the ssh-agent to manage your keys, you should have checked for existing SSH keys and generated a new SSH key.

If you have GitHub Desktop installed, you can use it to clone repositories and not deal with SSH keys. It also comes with the Git Bash tool, which is the preferred way of running git commands on Windows.

  1. Ensure the ssh-agent is running:
  • If you are using the Git Shell that’s installed with GitHub Desktop, the ssh-agent should be running.
  • If you are using another terminal prompt, such as Git for Windows, you can use the “Auto-launching the ssh-agent” instructions in “Working with SSH key passphrases”, or start it manually:
1
2
3
## start the ssh-agent in the background
$ eval $(ssh-agent -s)
> Agent pid 59566
  1. Add your SSH private key to the ssh-agent. If you created your key with a different name, or if you are adding an existing key that has a different name, replace id_rsa in the command with the name of your private key file.
1
$ ssh-add ~/.ssh/id_rsa

Add the SSH key to your GitHub account #

  1. Copy the SSH key to your clipboard.

If your SSH key file has a different name than the example code, modify the filename to match your current setup. When copying your key, don’t add any newlines or whitespace.

1
2
$ clip < ~/.ssh/id_rsa.pub
## Copies the contents of the id_rsa.pub file to your clipboard

Tip: If clip isn’t working, you can locate the hidden .ssh folder, open the file in your favorite text editor, and copy it to your clipboard.

  1. In the upper-right corner of any page, click your profile photo, then click Settings.

  2. In the user settings sidebar, click SSH and GPG keys.

  3. Click New SSH key or Add SSH key.

  4. In the “Title” field, add a descriptive label for the new key. For example, if you’re using a personal Mac, you might call this key “Personal MacBook Air”.

  5. Paste your key into the “Key” field.

  6. Click Add SSH key.

  7. If prompted, confirm your GitHub password.