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

How Developers Actually Use Git in Daily Work

In real projects, developers rarely work directly on the main branch. A typical workflow looks like this:

  1. Create a feature or bug-fix branch
  2. Make small, focused commits
  3. Push changes to a remote repository
  4. Open a pull request
  5. Review, test, and merge

This process keeps the codebase stable and makes it easy to track who changed what and why.

Writing Good Commit Messages (That Matter Later)

Bad commit messages cause confusion months later.

Instead of:

  • fix bug
  • update file

Use messages like:

  • Fix checkout total calculation for discounts
  • Add validation to user registration form

Clear messages save time during debugging and code reviews.

Common Git Mistakes Beginners Make

Almost everyone makes these mistakes early on:

  • Committing generated files or secrets
  • Pushing directly to the main branch
  • Making huge commits with unrelated changes
  • Ignoring merge conflicts instead of resolving them

These mistakes are normal the key is learning from them and improving your workflow.

Understanding Merge Conflicts Without Fear

Merge conflicts look scary, but they’re simply Git asking for your help.

Conflicts happen when:

  • Two people edit the same lines
  • A branch is outdated compared to main

Once you understand that conflicts are manual decisions, not errors, they become much easier to handle.

βœ… 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

Subscribe to our Newsletter

Provide your email to get email notification when we launch new products or publish new articles

email

Share with your friends: