storieasy-logo

Mastering Git: The Ultimate Guide from Beginner to Advanced

milan

Milan Patel

29 May 2025

|

30 min to read

git-command

Git

Github

GitCommands

GitGuide

GitForBeginner

GitAdvanced

GitWorkflow

Learn all Git commands from beginner to advanced in this complete 2025 guide. Master version control, GitHub integration, branching, merging, undoing changes, and more with real examples and clear explanations.

๐Ÿง  What Is Git (In Simple Terms)?

Git is a version control system that tracks your file changes over time. You can:

  • Save versions of your code.
  • Collaborate with others.
  • Undo mistakes.
  • Share work using services like GitHub.

๐Ÿ”ง Git Setup: Initial Configuration (One-Time Only)

1. Install Git (if not already installed):

For Windows:

Download from https://git-scm.com

For macOS:

1brew install git

For Ubuntu/Linux:

1sudo apt update
2sudo apt install git

Check version:

1git --version

๐Ÿ‘ค Git Configuration

1git config --global user.name "Your Name"
2git config --global user.email "you@example.com"
3git config --global core.editor "code --wait"  # Set VS Code as default editor

View all config:

1git config --list

๐Ÿ“ Repository Basics

Initialize Git

1git init

Clone Repository

1git clone <repo-url>

๐Ÿ“„ File Tracking Commands

Check Status

1git status

Add Files to Staging

1git add <filename>     # Add a specific file
2git add .              # Add all files in current directory
3git add -A             # Add all changes (tracked/untracked/deleted)

Commit Changes

1git commit -m "Your message"

Commit with Staged + Unstaged Info

1git commit -am "Commit tracked changes"

๐Ÿ”„ Working with Remote Repositories

Add Remote

1git remote add origin <url>

View Remotes

1git remote -v

Push Code

1git push -u origin main    # First time push with upstream
2git push                   # Normal push

Pull Latest Changes

1git pull origin main

๐ŸŒฟ Branching & Merging

Create a New Branch

1git branch <branch-name>

Switch Branch

1git checkout <branch-name>
2git switch <branch-name>         # Recommended

Create + Switch Branch

1git checkout -b <branch-name>
2git switch -c <branch-name>

Merge Branch

1git checkout main
2git merge <branch-name>

๐Ÿ“œ View History

View Commit History

1git log
2git log --oneline
3git log --graph --all --decorate

๐Ÿช„ Undoing Changes

Unstage a File

1git reset <file>

Undo Last Commit (keep changes)

1git reset --soft HEAD~1

Undo Last Commit (remove staged)

1git reset --mixed HEAD~1

Undo Last Commit (remove everything)

1git reset --hard HEAD~1


๐Ÿšซ Ignoring Files

Create .gitignore

1# .gitignore
2node_modules/
3.env
4.DS_Store
5*.log
6

๐Ÿ“ฆ Stashing Work

Temporarily save changes without committing:

1git stash
2git stash list
3git stash apply         # Apply latest stash
4git stash pop           # Apply and delete

โœ‚๏ธ Deleting & Renaming Files

1git rm <file>
2git mv <old> <new>

๐Ÿ” Viewing Changes

1git diff                  # Unstaged changes
2git diff --staged         # Staged changes
3git diff HEAD             # All changes since last commit

๐Ÿ” Rebase

1git checkout feature
2git rebase main
  • Makes history linear
  • Resolve conflicts like merges

๐Ÿ’ Cherry-pick

Apply a specific commit to your current branch:

1git cherry-pick <commit-hash>


๐Ÿงน Clean Untracked Files

1git clean -n             # Show what will be deleted
2git clean -f             # Delete untracked files


๐Ÿ•ต๏ธโ€โ™‚๏ธ Blame & Annotate

1git blame <file>       # Shows who changed each line

๐Ÿ” GitHub Integration

Generate SSH Key

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

Add SSH Key

  • Add the public key (~/.ssh/id_ed25519.pub) to GitHub โ†’ Settings โ†’ SSH Keys.

Clone with SSH

1git clone git@github.com:user/repo.git

โš™๏ธ Advanced Config & Shortcuts

Aliases

1git config --global alias.st status
2git config --global alias.co checkout
3git config --global alias.br branch
4git config --global alias.cm 'commit -m'

Now use:

1git st
2git co main
3git cm "msg"

๐Ÿงฉ Git Submodules

Used to include one repo inside another.

1git submodule add <repo-url> path/
2git submodule update --init --recursive
3

โœ… Final Tips

  • Commit often with clear messages.
  • Pull frequently to avoid conflicts.
  • Use branches for features or bugs.
  • Push only tested and reviewed code.
  • Learn rebase vs merge for clean histories.

๐Ÿ“š Git Command Cheat Sheet

git initStart a repo
git clone Copy a repo
git add Stage changes
git commit Save changes
git push Upload to remote
git pull Download from remote
git branchList or create branches
git merge Merge branches
git rebase Rebase commits
git reset Undo changes
git log View commit history
git stash Save uncommitted work
git cherry-pick Apply a specific commit
git remote Manage remote repos
git diff View file differences
git tag Create version tags

Newsletter

Subscribe to our newsletter and get our latest updates.

Share with your friends: