Connecting a Local Git Repository to GitHub

This guide explains how to initialize a local Git repository, configure user credentials, generate SSH keys, and link the repository to a remote GitHub repository.

Configure Git User and Email

Set the global Git username and email address. When connecting to GitHub, the email should match your GitHub account. The username does not need to be your GitHub username.

git config --global user.name "YourName"
git config --global user.email "youremail@example.com"

Example:

$ git config --global user.name "RocSun"
$ git config --global user.email "710989028@qq.com"

Generate SSH Key Pair

GitHub requires an SSH key pair for secure communication. Generate one using the command below, replacing the email with your GitHub registered email. The keys will be saved in C:\Users\<Username>\.ssh\ on Windows (id_rsa and id_rsa.pub).

ssh-keygen -t rsa -C "your-email@example.com"

Example output:

$ ssh-keygen -t rsa -C "710989028@qq.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Roc/.ssh/id_rsa):
Created directory '/c/Users/Roc/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/Roc/.ssh/id_rsa.
Your public key has been saved in /c/Users/Roc/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:s9KZlaSzp3qlmY/u/nEpxOIpRwV04CNY7WFcpIPxUBE 710989028@qq.com
The key's randomart image is:
+---[RSA 2048]----+
|      +=E*+      |
|     o *=+       |
|    . oo*.o      |
|       ..B .     |
|        S =      |
|       + %.  .   |
|      o X=+ o    |
|       +=+ +     |
|      .**oo      |
+----[SHA256]-----+

After generating the key, add the public key (id_rsa.pub) to your GitHub account settings under SSH and GPG keys.

Test the connection:

ssh -T git@github.com

Successful response example:

$ ssh -T git@github.com
The authenticity of host 'github.com (13.229.188.59)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,13.229.188.59' (RSA) to the list of known hosts.
Hi RocsSun! You've successfully authenticated, but GitHub does not provide shell access.

Initialize a Local Repository

Navigate to you're project directory and initialize a Git repository.

git init

Example:

$ cd /e/note
$ ls
 Bug/            Flask/        MySQL/     python安装报错.md      Vue/
 Django/         Git/          Project/   Redis/                 孙鹏飞.md
 Django回顾.md   JavaScript/   Python/   'Virtualenv&wrapper'/

$ git init
Initialized empty Git repository in E:/note/.git/

Stage and Commit Files

Add files to the staging area and commmit them.

git add <file_or_directory>
git commit -m "Commit message"

Example:

$ git add ./*
$ git commit -m "Initialize repository with previous notes"
[master (root-commit) 53ab25a] Initialize repository with previous notes
 39 files changed, 2260 insertions(+)
 create mode 100644 Bug/Windows-0x80070570.md
 ...

Link to Remote Repository

Create a new empty repository on GitHub (do not initialize with README, .gitignore, or license). Then add the remote URL.

git remote add origin git@github.com:YourUsername/RepositoryName.git

Example:

$ git remote add origin git@github.com:RocsSun/note.git

Push to GitHub

Push the local master branch to the remote repository. Use the -u option on the first push to set upstreamm.

git push -u origin master

Example output:

$ git push -u origin master
Enumerating objects: 49, done.
Counting objects: 100% (49/49), done.
Delta compression using up to 4 threads
Compressing objects: 100% (43/43), done.
Writing objects: 100% (49/49), 32.97 KiB | 1.14 MiB/s, done.
Total 49 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), done.
remote:
remote: Create a pull request for 'master' on GitHub by visiting:
remote:      https://github.com/RocsSun/note/pull/new/master
remote:
To github.com:RocsSun/note.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

Tags: Git GitHub ssh Tutorial

Posted on Thu, 16 Jul 2026 17:29:54 +0000 by Hopps