To push code to your GitHub repositories, we will use the recommended HTTPS method. There are also instructions for using SSH. To prevent git from asking for your username and password every time you push a commit you can cache your credentials by running the following command, as described in the instructions.
Text Only
1
git config --global credential.helper osxkeychain
In fact, the mail and your name above are of no sense. They will not be used any more in your future. So you can just leave it and go on next.
PS:
- You must create a repo on GitHub and then clone it into local devices
- You are not allowed to initialize a local repo as a "git-permitted" one!
- So, follow the steps above and you will be satisfied
First check for existing SSH keys on your computer by running:
Text Only
12
ls -al ~/.ssh
# Lists the files in your .ssh directory, if they exist
Bash
1 2 3 4 5 6 7 8 91011121314151617181920
# for example (mine)
❯ls-al~/.ssh
drwx------huluobostaff288BSunApr2117:40:232024.
drwxr-x---huluobostaff2.8KBThuApr2500:25:472024..
.rw-r--r--huluobostaff303BSatApr1300:11:322024config
.rw-------huluobostaff2.6KBTueJan2315:29:132024id_rsa
.rw-r--r--huluobostaff588BTueJan2316:03:162024id_rsa.pub
.rw-------huluobostaff3.4KBSatFeb2401:26:252024id_rsa_GithubConnection
.rw-r--r--huluobostaff747BSatFeb2401:26:252024id_rsa_GithubConnection.pub
.rw-------huluobostaff2.8KBSunApr2117:40:232024known_hosts
.rw-------huluobostaff2.1KBSunApr2117:40:102024known_hosts.old
# the missing signal means "lsd" can not be classfied with ASCII, you can ignore it here```
Checkthedirectorylistingtoseeifyouhavefilesnamedeither`id_rsa.pub`or`id_dsa.pub`.Ifyoudon't have either of those files then read on, otherwise skip the next section.### Generate a new SSH keyIf you don'thaveanSSHkeyyouneedtogenerateone.Todothatyouneedtorunthecommandsbelow,andmakesuretosubstitutetheplaceholderwithyouremail(thistimetheemailshouldbeseriousforit's significant in generating your isa). The default settings are preferred, so when you'reaskedtoenterafileinwhichtosavethekey,justpressEntertocontinue.
Creates a new ssh key, using the provided email as a label¶
Text Only
123
### Add your SSH key to the ssh-agent
Run the following commands to add your SSH key to the `ssh-agent`.
eval "$(ssh-agent -s)"
Text Only
1
If you're running macOS Sierra 10.12.2 or later, you will need to modify your `~/.ssh/config` file to automatically load keys into the ssh-agent and store passphrases in your keychain:
The config file contains all the ssh-keys that you owned, and you must config them well, otherwise you may not be able to contact with the specified Servers or Website.
No matter what operating system version you run you need to run this command to complete this step:
ssh-add -K ~/.ssh/id_rsa
Text Only
123
### Adding a new SSH key to your GitHub account
The last step is to let GitHub know about your SSH key so GitHub can recognize you. Run this command to copy your key to your clipboard:
pbcopy < ~/.ssh/id_rsa.pub
Text Only
1234567
Then go to GitHub and [input your new SSH key](https://github.com/settings/ssh/new). Paste your key in the "Key" text-box and pick a name that represents the computer you're currently using.
We are now ready to use SSH with GitHub!
### Clone repositories using SSH
After creating a new repo on GitHub, clone it using
- if you had initialized with a README.
If you did not, follow the instructions in the section below.
### Set up a new or existing repo with SSH for GitHub
If you are setting up a new repo, add at least one file and commit first. Then, configure the remote and push to GitHub by running: