Post

Git Playbook: The Complete Guide to Git Commands for Version Control

If you’ve ever felt a bit lost setting up Git on Linux, you’re not alone and this blog is here to help. I’ll walk you through everything you need to get Git up and running, from installing it to setting up your name, email, and SSH keys. By the end, you won’t just have Git installed you’ll know how to actually use it like a pro.

Git Playbook: The Complete Guide to Git Commands for Version Control

What is Git?

Git is a free, open-source version control system that helps you track changes in your code over time. Think of it as a powerful undo button for your projects whether you’re working alone or collaborating with others. It lets you save snapshots of your work, switch between versions, and collaborate without overwriting each other’s code. Git is fast, flexible, and has become the industry standard for managing code especially when paired with platforms like GitHub or GitLab.

Installation

If you’re on Fedora (or any closely-related RPM-based distribution, such as RHEL or CentOS), you can use dnf:

1
sudo dnf install git-all

If you’re on a Debian-based distribution, such as Ubuntu, try apt:

1
apt update && apt install git -y

We can download Git for windows from here. For more details on installation please visit this guide from git.

Once installed we can verify the installation in our terminal with:

1
git --version

Configuring Git

Once Git is installed, it’s important to configure your identity so that your commits are properly attributed. This information is stored in your Git configuration file and will be used in every repository you work with.

Start by setting your name and email address:

1
2
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

The --global flag ensures that these settings apply to all repositories on your system. If you want to override these details for a specific project later, you can re-run the same commands without --global from within that repository.

1
git config --list

This will display your Git setup, including user information, editor, and other useful defaults.

Setting Up SSH Keys

To securely connect your local Git setup with services like GitHub or GitLab, it’s recommended to use SSH authentication. This avoids entering your username and password every time you push or pull from a remote repository.

Step 1: Check for existing SSH keys

First, check if you already have an SSH key:

1
ls -al ~/.ssh

If you see files like id_rsa and id_rsa.pub, you already have a key pair.

Step 2: Generate a new SSH key

If you don’t have one, or want a fresh key for Git:

1
ssh-keygen -t ed25519 -C "you@example.com"

If you’re using an older system that doesn’t support ed25519, use:

1
ssh-keygen -t rsa -b 4096 -C "you@example.com"

Just press Enter through the prompts to use the default file location and no passphrase (or set one if you prefer more security).

Step 3: Add the SSH key to your agent

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

Step 4: Adding Your SSH Key to GitHub

  1. Copy your public SSH key to the clipboard:
1
cat ~/.ssh/id_ed25519.pub
  1. Go to GitHub → Settings → SSH and GPG keys

New SSH Key

  1. Click “New SSH key”, give it a name (like “my-ssh-key”), and paste the key
  2. Click Add SSH key

Git CheatSheet

Create / Clone Repository

1
2
git init                     # Initialize new Git repo
git clone <url>              # Clone existing repo

Working with Files

1
2
3
4
git status                   # Show file changes
git add <file>               # Stage specific file
git add .                    # Stage all files
git restore <file>           # Unstage or discard changes

Committing Changes

1
2
git commit -m "message"      # Commit staged changes
git commit -am "message"     # Stage + commit tracked files

Syncing with Remote

1
2
3
4
5
git remote add origin <url>  # Add remote repo
git remote set-url origin git@github.com:your-username/your-repo.git # Set Origin
git push -u origin main      # Push to remote (first time)
git push                     # Push changes
git pull                     # Pull latest changes

Branching

1
2
3
4
5
6
git branch                   # List branches
git branch <name>            # Create branch
git checkout <name>          # Switch branch
git switch -c <name>         # Create and switch
git merge <name>             # Merge branch into current
git branch -d <name>         # Delete branch

Viewing History

1
2
3
git log                      # Full commit history
git log --oneline --graph    # Visual history
git show <commit>            # Show specific commit

Undo / Reset

1
2
3
git reset --soft HEAD~1      # Undo last commit (keep changes)
git reset --hard HEAD~1      # Remove commit + changes
git revert <commit>          # Create revert commit

Tags

1
2
3
git tag                      # List tags
git tag <v1.0>               # Create tag
git push origin <tagname>    # Push tag
This post is licensed under CC BY 4.0 by the author.