Setting up GitHub
This guide requires using the terminal (also called command line or shell). All commands shown here should be run in your terminal application.
Why use Git and GitHub?
Git and GitHub provide essential tools for modern software development:
- Version control: Track changes to your code over time, making it easy to see what changed and when
- Collaboration: Multiple people can work on the same project without conflicts
- Safety: Create branches to work on features without breaking your main code
- Backup: Your code is stored remotely, protecting against local data loss
- History: See the complete history of your project and revert to previous versions if needed
Create a GitHub account
- Go to github.com
- Click "Sign up" in the top right corner
- Follow the prompts to create your account
- Verify your email address when prompted
Install gh (GitHub CLI)
The GitHub CLI (gh) makes it easy to authenticate and work with GitHub from the command line. This requires using the terminal to install and run commands.
Open your terminal
Before installing, open your terminal application:
macOS:
- Press
Cmd + Spaceto open Spotlight - Type "Terminal" and press Enter
- Or go to Applications → Utilities → Terminal
Linux:
- Press
Ctrl + Alt + T(most distributions) - Or search for "Terminal" in your applications menu
- Common names: Terminal, Konsole, GNOME Terminal
Windows:
- Press
Windows + Xand select "Windows PowerShell" or "Terminal" - Or press
Windows + R, typepowershell, and press Enter - Or search for "PowerShell" or "Command Prompt" in the Start menu
- Windows Terminal (recommended) can be installed from the Microsoft Store
macOS
brew install gh
Linux
# Debian/Ubuntu
sudo apt install gh
# Fedora
sudo dnf install gh
# Arch Linux
sudo pacman -S github-cli
Windows
# Using winget
winget install --id GitHub.cli
# Or using Chocolatey
choco install gh
# Or download from: https://cli.github.com/
After installation, verify it works:
gh --version
Configure GitHub authentication
-
Run the authentication command:
gh auth login -
Follow the interactive prompts:
- Select
GitHub.com - Choose
SSHas your preferred protocol - When asked for your SSH key, select the default option (usually
/Users/<computer_username>/.ssh/id_ed25519.pubon macOS/Linux, orC:\Users\<computer_username>\.ssh\id_ed25519.pubon Windows) - If you don't have an SSH key,
ghwill help you generate one
- Select
-
Complete the browser authentication:
- A security code will be displayed in your terminal
- Your default browser will open automatically
- Paste the security code into the browser
- Authorize GitHub CLI to access your account
-
Verify authentication:
gh auth status
You're all set! Your SSH key is now configured and you can push and pull without entering credentials each time.
Why use SSH?
SSH (Secure Shell) keys provide a more secure and convenient way to authenticate with GitHub:
- Security: Your credentials are saved securely on your computer, not transmitted with each request
- Convenience: No need to log in with a password at each push or pull
- Best practice: SSH is the recommended authentication method for GitHub
Create your first repository
- On GitHub.com, click the
+button in the top right corner - Select "New repository"
- Fill in the repository details:
- Repository name: Choose a descriptive name (e.g.,
my-first-project) - Description: Optional, but helpful for others to understand your project
- Visibility: Choose Public (visible to everyone) or Private (only you can see it)
- Initialize repository: You can skip this since we'll add files manually
- Repository name: Choose a descriptive name (e.g.,
- Click "Create repository"
Prepare your local code
-
Create a directory for your project:
mkdir -p ~/code/<repo_name>
cd ~/code/<repo_name>Replace
<repo_name>with your actual repository name. -
Initialize Git in this directory:
git init -
Create a test file:
touch test_file.txtOr create any file you want to track.
Add, commit, and push
Now you'll connect your local repository to GitHub and push your code:
-
Stage your files (tell Git which files to include):
git add .This adds all files in the current directory. You can also add specific files:
git add test_file.txt -
Create your first commit (save a snapshot of your code):
git commit -m "first commit"The
-mflag lets you add a message describing what this commit does. -
Rename the branch to
main(GitHub's default branch name):git branch -M mainThis ensures your local branch matches GitHub's naming convention.
-
Connect to your GitHub repository:
git remote add origin git@github.com:<username>/<repo_name>.gitReplace
<username>with your GitHub username and<repo_name>with your repository name. This will be shown on the GitHub repository page. -
Push your code to GitHub:
git push -u origin mainThe
-uflag sets up tracking so future pushes can be done with justgit push.
Refresh your GitHub repository page—you should see your files!
Next steps
Now that you have GitHub set up, you can:
- Make changes to your files locally
- Use
git add .andgit commit -m "your message"to save changes - Use
git pushto upload your changes to GitHub - Create branches to work on new features safely
- Collaborate with others by sharing your repository