Back to Blog

Quick Revision on Git and GitHub Commands

A comprehensive guide for developers and students learning version control

"Version control is the foundation of modern software development. Whether you're working on a personal project or collaborating in a large team, understanding Git and GitHub helps you track, manage, and share your code effectively."

๐Ÿ”น Introduction

This quick revision guide covers the most essential Git and GitHub commands every developer should know.

I originally compiled these commands for my own use while learning version control โ€” written in simple, natural language to ensure clarity. Over time, they've become my go-to reference whenever I need a quick refresher.

Let's dive in ๐Ÿ‘‡

โš™๏ธ Git Basics

โœ… Check Git Version

Make sure Git is installed and check its version:

git --version

๐Ÿงฉ Configure Git

Set up your username and email (these appear in your commits):

git config --global user.name "Your Name" git config --global user.email "your@email.com"

View your configuration:

git config --list

๐Ÿ“ Local Repository Management

๐Ÿ—๏ธ Initialize a Repository

Create a new Git repository in your project folder:

git init

๐Ÿ” Check Repository Status

View changes, staged files, and untracked files:

git status

โž• Add Files

Add specific files or all files to the staging area:

git add filename git add .

๐Ÿ’พ Commit Changes

Save staged changes with a descriptive message:

git commit -m "Your message here"

๐ŸŒฟ Branch Information

View all branches or see which branch you're on:

git branch

๐Ÿ“œ View Commit History

List all commits:

git log

๐Ÿงฎ Working with File Versions

๐Ÿง Compare Changes

Compare modified files before committing:

git diff

Compare staged files:

git diff --cached

๐Ÿ•“ View Previous File Versions

See the content of a file in a previous commit:

git show commit-id:filename

๐Ÿ” Switch Between Versions

Checkout an older version and return to current:

git checkout commit-id -- filename git checkout branch-name -- filename

๐Ÿ”ง Undoing Changes

๐Ÿงน Restore Unstaged Changes

Undo changes before staging:

git restore filename

๐Ÿงฑ Restore Staged Changes

Undo files added with git add:

git restore --staged filename

โช Reset Committed Changes

Undo commits:

git reset --soft HEAD^ # Uncommit but keep changes git reset --hard HEAD^ # Uncommit and delete changes

๐Ÿ’พ Stash Temporary Changes

Save changes temporarily without committing:

git stash # Save current changes git stash push -m "message" # Save with message

Retrieve stashed changes:

git stash pop # Apply and remove latest stash git stash apply # Apply but keep stash

Manage stashes:

git stash list # View all stashes git stash drop # Delete latest stash

๐Ÿ•ฐ๏ธ Git Logs & History

View your project's commit history in different formats:

git log -p -2 # Last two commits with diff git log --stat # Summary of changes git log --pretty=oneline git log --pretty=format:"%h - %an, %ar : %s"

๐Ÿ”Ž Search Within Logs

Find specific commits:

git log -S function_name git log --grep="keyword"

๐Ÿ—“๏ธ Filter by Time or Author

git log --since="2024-01-01" git log --author="Sameen" git log --no-merges

โ˜๏ธ Remote Repository Management (GitHub)

๐ŸŒ Link Local Repo to Remote

git remote add origin remote_URL git branch -M main git push -u origin main

๐Ÿš€ Push & Pull

Push your commits:

git push

Pull updates from the remote repo:

git pull

๐Ÿ“ฆ Clone a Repository

git clone repo_URL

This downloads the repo and full commit history to your local machine.

๐ŸŒฟ Branching & Merging

๐Ÿ”ง Branch Management

Create and switch to a new branch (modern way):

git switch -c new-branch

Or the traditional way:

git branch new-branch git checkout new-branch

Switch between existing branches:

git switch branch-name git checkout branch-name

๐Ÿ”„ Merge Branches

git checkout main git merge feature-branch

๐Ÿ”„ Rebase (Alternative to Merge)

Rebase replays commits on top of another branch (creates linear history):

git checkout feature-branch git rebase main

Interactive rebase (edit commit history):

git rebase -i HEAD~3 # Rebase last 3 commits

Rebase vs Merge: Rebase creates cleaner history but rewrites commit history. Use merge for shared branches.

โš ๏ธ Handling Merge Conflicts

If the same file is edited differently in two branches, Git will flag a merge conflict. You'll need to manually resolve it, save, and commit again.

๐Ÿ’ Cherry-Pick Commits

Apply specific commits from one branch to another:

git cherry-pick commit-hash

Cherry-pick multiple commits:

git cherry-pick commit1 commit2 commit3

Cherry-pick a range of commits:

git cherry-pick start-commit^..end-commit

Useful for applying hotfixes or specific features to different branches.

๐Ÿงญ GitHub Workflow

๐Ÿด Forking and Pull Requests

On GitHub:

  • Click Fork to create your own copy of a repo.
  • Make your changes locally.
  • Push your updates to your forked repo.
  • Open a Pull Request (PR) to suggest merging your changes into the original repository.

This is the core of open-source collaboration.

๐Ÿงน Git Utilities

๐Ÿช„ .gitignore

Exclude files or folders from version control:

# Example .gitignore node_modules/ .env __pycache__/

๐Ÿงฝ Clean Untracked Files

git clean -n # Preview git clean -f # Delete untracked files

๐Ÿท๏ธ Tags (Versioning)

Create and manage version tags:

git tag -a v1.0 -m "Initial release" git push origin --tags

Delete tags:

git tag -d tag_name git push origin --delete tag_name