Skip to content

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

  1. Generate the key:

    ssh-keygen -t ed25519 -C "your_email@example.com"
    
    • -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).
  2. Start the SSH agent and load the key:

    eval "$(ssh-agent -s)"
    ssh-add ~/.ssh/id_ed25519
    

    (If you used a different filename, substitute it.)

B. Add the Public Key to GitHub

  1. Copy the public key:

    cat ~/.ssh/id_ed25519.pub
    

    Copy the entire output, which starts with ssh-ed25519 and ends with your email.

  2. 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.
  3. Test the connection:

    ssh -T git@github.com
    
    • 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.

  1. 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).
  2. Add the remote origin to your local repository:

    git remote add origin git@github.com:USERNAME/REPOSITORY.git
    
    • 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.
  3. Push your local branch to the remote:

    git push -u origin main
    
    • git push: Sends your committed changes to the remote.
    • -u origin main: Sets the upstream branch, linking your local main branch to the remote main branch (origin). This allows you to simply use git push and git 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

git remote -v

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):

git remote set-url origin git@github.com:NEW_USERNAME/NEW_REPOSITORY.git

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:

  1. Remove the existing remote:

    git remote remove origin
    

    (This only removes the link; it does not delete the repository on GitHub.)

  2. Add the new remote (referencing step 3.2):

    git remote add origin git@github.com:NEW_USERNAME/NEW_REPOSITORY.git
    

    Use this if you are pointing your local repo to an entirely different GitHub repository.