GitHub
Initiating a Git repository, adding SSH keys, and managing remote origins are fundamental steps for using Git with platforms like GitHub. Here's a guide covering these actions using the command line.
1. Initiate a Git Repository
This process starts tracking files in a directory with Git.
Command | Description |
---|---|
cd /path/to/your/project |
Navigate to your project's root directory. |
git init |
Initializes an empty Git repository in the directory. |
git add . |
Stages all current files for the first commit. |
git commit -m "Initial commit" |
Creates the first commit with a descriptive message. |
git branch -M main |
Renames the default branch (usually master ) to main (a modern convention). |
2. Generate and Add SSH Keys
Using SSH for Git operations is more secure and convenient than HTTPS with a username and password/token, as it doesn't require repeated authentication.
A. Generate an SSH Key Pair
-
Generate the key:
-t ed25519
: Specifies the modern, recommended encryption algorithm.-C "..."
: Adds a comment to identify the key.- Follow the prompts: You'll be asked where to save the key (default is usually
~/.ssh/id_ed25519
) and to enter a passphrase (highly recommended for security).
-
Start the SSH agent and load the key:
(If you used a different filename, substitute it.)
B. Add the Public Key to GitHub
-
Copy the public key:
Copy the entire output, which starts with
ssh-ed25519
and ends with your email. -
Go to GitHub:
- Navigate to Settings (your profile picture).
- In the sidebar, click SSH and GPG keys.
- Click New SSH key or Add SSH key.
- Give it a descriptive Title (e.g., "My Laptop Key").
- Paste the public key you copied into the Key field.
- Click Add SSH key.
-
Test the connection:
- You should see a message confirming your authentication, like: "Hi username! You've successfully authenticated..."
3. Push Local Files to GitHub using SSH
Once your repository is initialized and your SSH key is set up, you can connect your local repo to a remote one on GitHub.
-
Create a new, empty repository on GitHub:
- Do not check the box to initialize it with a README, license, or
.gitignore
, as you already have local files. - GitHub will provide you with the SSH remote URL (it looks like
git@github.com:USERNAME/REPOSITORY.git
).
- Do not check the box to initialize it with a README, license, or
-
Add the remote origin to your local repository:
git remote add
: Command to add a new remote connection.origin
: The conventional name for the primary remote repository.git@...
: The SSH URL you got from GitHub.
-
Push your local branch to the remote:
git push
: Sends your committed changes to the remote.-u origin main
: Sets the upstream branch, linking your localmain
branch to the remotemain
branch (origin
). This allows you to simply usegit push
andgit pull
later.
4. Adding/Changing Remote Origin
If you need to change the repository your local Git points to, or if you made a mistake adding it, here's how to manage the remote origin.
A. Check the Current Remote
This shows the names and URLs for fetching and pushing. You'll typically see entries for origin
.
B. Change the Remote URL
If you only need to change the URL (e.g., switching from HTTPS to SSH):
Replace the URL with your desired SSH URL.
C. Remove and Re-add the Remote
If you want to completely sever the connection to origin
and start fresh:
-
Remove the existing remote:
(This only removes the link; it does not delete the repository on GitHub.)
-
Add the new remote (referencing step 3.2):
Use this if you are pointing your local repo to an entirely different GitHub repository.