How to Set Up Git and Connect to GitHub on Linux

Jun Takemura · September 20, 2024

How to Set Up Git and Connect to GitHub on Linux

This article provides a overview of setting up Git, connecting to GitHub, and understanding basic Git commands on Linux. I


What is Git and GitHub?

Git

Git is a distributed version control system that tracks changes in your code and allows multiple people to work on a project simultaneously without overwriting each other’s changes.

GitHub

GitHub is a hosting platform for Git repositories. It provides tools for collaboration, such as pull requests, issue tracking, and wikis, making it easier to manage projects.


Setup

Generate an SSH Key

To securely connect to GitHub, generate an SSH key:

ssh-keygen -t ed25519 -C "[email protected]"

Press Enter to accept the default file location.


Add Your SSH Key to GitHub

Copy the Public Key

Retrieve the public key:

cat ~/.ssh/id_ed25519.pub

Add the Key to GitHub

Go to GitHub > Settings > SSH and GPG Keys > New SSH Key.
Paste the copied key into the Key field.
Save the changes.


Test the SSH Connection

Verify the SSH key is correctly added:

Expected output:

Hi username! You've successfully authenticated, but GitHub does not provide shell access.

Configure the SSH Agent

To avoid re-entering the passphrase, configure the SSH agent:
Add the following to ~/.zshrc (or ~/.bashrc):

if ! pgrep -u "$USER" ssh-agent > /dev/null 2>&1; then
  eval "$(ssh-agent -s)" > /dev/null
fi
ssh-add -l > /dev/null 2>&1 || ssh-add ~/.ssh/id_ed25519 > /dev/null 2>&1

Apply the changes:

source ~/.zshrc

Note that many modern Linux environments automatically handle SSH agent configuration. You may not need to do this if you use latest Ubuntu, Debian, etc.


How to Use Git and GitHub

Initialize a Git Repository

Git tracks changes in a project by initializing a repository:

Navigate to your project directory:

cd /path/to/your/project

Initialize Git:

git init

This creates a .git folder to track changes.

Stage and commit files:

git add .
git commit -m "Initial commit"

git add . stages all changes for the next commit.
git commit -m “message” saves the changes with a descriptive message.


Add a Remote Repository

To share your code on GitHub, link the local repository to a remote:

Add the remote repository:

git remote add origin [email protected]:username/repository-name.git

Push changes to GitHub:

git branch -M main
git push -u origin main

git branch -M main renames the default branch to main. git push -u origin main uploads your code to the main branch on GitHub.


Understanding Key Git Commands

Pull and Fetch

Pull: Updates your local repository with the latest changes from the remote repository:

git pull origin main

Combines fetch (download changes) and merge (integrate changes).

Fetch: Only downloads changes from the remote repository without applying them:

git fetch origin

You can review the changes before merging.

Clone

To copy a repository from GitHub to your local system:

git clone [email protected]:username/repository-name.git

Branches

Branches allow you to work on new features without affecting the main codebase:

Create a new branch:

git checkout -b feature-branch

Switch branches:

git checkout main

Merge changes from one branch to another:

git merge feature-branch

Twitter, Facebook