Pushing your project to GitHub is an essential step for sharing your code, collaborating with others, and maintaining version control. This guide will walk you through the process using Git and SSH authentication.
Prerequisites
- Git Installed: Ensure Git is installed on your system. You can download it from Git’s official website.
- GitHub Account: Have an active GitHub account. If you don’t have one, you can sign up at GitHub.
- A Local Project Directory: Have your project ready in a local directory.
1. Configure Git with Your User Details
Set up your username and email address for Git, which will associate your commits with your identity. Replace Ankush More and ankushitguy@gmail.com with your details:
git config --global user.name "Ankush More"
git config --global user.email "ankush.more.email@gmail.com"
git config --global init.defaultBranch main2. Generate an SSH Key
An SSH key enables secure authentication without the need for a password. Run the following command to generate an SSH key:
ssh-keygen -t rsa -C "ankush.more.email@gmail.com"When prompted, press Enter to save the key in the default location (~/.ssh/id_rsa). This generates two files:
id_rsa: Your private key (keep this secure).id_rsa.pub: Your public key.
To display your public key, type:
cat ~/.ssh/id_rsa.pubCopy the output of the public key.

3. Add SSH Key to Your GitHub Account
- Log in to your GitHub account.
- Navigate to Settings > SSH and GPG Keys.
- Click New SSH Key.
- Add a label (e.g., My Laptop) and paste the copied SSH key into the text box.
- Click Add SSH Key.
4. Test SSH Connection
Verify the SSH connection with GitHub:
ssh -T git@github.comYou should see a confirmation message similar to:
Hi ankushitguy! You've successfully authenticated, but GitHub does not provide shell access.This indicates that SSH authentication is set up correctly.
5. Initialize a Git Repository
Navigate to your project directory and initialize a Git repository:
cd /path/to/your/project
git initThis creates a .git directory in your project folder, which tracks version control.
6. Stage and Commit Your Files
Add Files to Git: Use git add to stage files for commit. To add all files in your project directory:
git add .Create a .gitignore File: If there are files or directories you want Git to ignore (e.g., node_modules, .env), create a .gitignore file. Example:
node_modules/
.envAdd it to the repository:
git add .gitignoreCommit Changes: Record your changes in the local repository:
git commit -m "Initial commit"7. Connect to a Remote Repository
Create a repository on GitHub. Note the repository URL (e.g., git@github.com:ankushitguy/blog.altgr.in.git).
Add the remote repository to your local project:
git remote add origin git@github.com:moreskylab/blog.altgr.in8. Push Your Project to GitHub
Push your local repository to the GitHub remote repository:
git push -u origin mainThe -u flag sets the origin repository as the default remote, and master is the branch you are pushing to. For subsequent pushes, you can simply type:
git pushSummary of Commands
| Command | Description |
|---|
git config --global user.name | Sets your Git username globally. |
git config --global user.email | Sets your Git email globally. |
ssh-keygen -t rsa -C "email" | Generates an SSH key for authentication. |
ssh -T git@github.com | Tests your SSH connection with GitHub. |
git init | Initializes a local Git repository. |
git add . | Stages all files for commit. |
git commit -m "message" | Commits staged changes with a message. |
git remote add origin [url] | Links the local repository to a remote repository. |
git push -u origin main | Pushes changes to the remote repository and sets the upstream. |
Ref: https://kbroman.org/github_tutorial
Discover more from Altgr Blog
Subscribe to get the latest posts sent to your email.
