Git and GitHub are essential tools for modern developers, providing robust version control and collaboration features. If you’re using a Linux system, here’s a comprehensive guide to get you started.
Prerequisites
A Linux machine (Ubuntu, Fedora, etc.)
A GitHub account (sign up at github.com)
Step 1: Install Git
First, you’ll need to install Git on your Linux machine. Open your terminal and execute the following command based on your Linux distribution.
For Debian-based distributions (e.g., Ubuntu):
bash
sudo apt update
sudo apt install git
For RPM-based distributions (e.g., Fedora):
bash
sudo dnf install git
To verify the installation, run:
bash
git --version
Step 2: Configure Git
After installing Git, you need to set up your username and email address. These details will be included in your commit messages.
bash
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
To confirm your configuration, use:
bash
git config --list
Step 3: Generate SSH Key
For secure communication with GitHub, it’s recommended to use SSH. Generate an SSH key pair using the following command:
bash
ssh-keygen -t ed25519 -C "your.email@example.com"
Press Enter to accept the default file location, and then enter a passphrase for added security.
Step 4: Add SSH Key to GitHub
Copy your public SSH key to your clipboard:
bash
cat ~/.ssh/id_ed25519.pub
Log in to your GitHub account, go to Settings > SSH and GPG keys, and click New SSH key. Paste the copied key and give it a title.
Step 5: Test the SSH Connection
Test your connection to GitHub with:
bash
ssh -T git@github.com
You should see a message like:
Hi <username>! You've successfully authenticated, but GitHub does not provide shell access.
Step 6: Create a Repository
Now, you can create a new repository on GitHub. Click the New repository button on your GitHub dashboard, name your repository, and click Create repository.
Step 7: Initialize a Local Repository
Back on your Linux terminal, navigate to your project directory and initialize a local Git repository:
bash
cd /path/to/your/project
git init
Step 8: Add Remote Repository
Link your local repository to the GitHub repository:
bash
git remote add origin git@github.com:<username>/<repository>.git
Step 9: Add and Commit Files
Add your project files and commit them to your local repository:
bash
git add .
git commit -m "Initial commit"
Step 10: Push to GitHub
Finally, push your local commits to the GitHub repository:
bash
git push -u origin main
And there you go! You’ve set up Git with GitHub on your Linux system. Happy coding!