• Skip to primary navigation
  • Skip to main content
  • Skip to footer
Jabal Torres Logo
  • About
  • Work
  • Blog
  • Contact Me

The Complete Git Command Guide: From Beginner to Advanced

October 30, 2025October 30, 2025 by JabalPosted in WebTags: git

Whether you’re just starting your journey with Git or looking to level up your version control skills, this comprehensive guide covers everything you need to know. Git is the industry-standard version control system that helps developers track changes, collaborate effectively, and maintain code history.

Getting Started with Git

Installation and Configuration

Before diving into commands, let’s verify your Git installation:

# Check where Git is installed
which git

# Check your Git version
git --version

Pro Tip: Create aliases to speed up your workflow. For example, make a shortcut for git status:

git config --global alias.st status

Need help with any Git command? Simply use:

git help

Essential Beginner Commands

Initializing and Basic Operations

Every Git project starts with initialization:

# Initialize a new Git repository
git init

# Check the status of your repository
git status

Adding Files to Staging

Git uses a staging area before committing changes:

# Add a specific file
git add README.md

# Add all files in the current directory
git add .

# Add all changes (additions, modifications, and deletions)
git add --all

# Interactive mode for selective staging
git add -i

# Stage only HTML files using wildcards
git add *.html

# Update all tracked files (stages modifications and deletions)
git add -u

Committing Your Changes

Once files are staged, commit them to your repository:

# Commit with a message
git commit -m 'First commit'

# Add all changes and commit in one step (tracked files only)
git commit -am "Comment Added"

# Amend the most recent commit and reuse its message
git commit --amend -C HEAD

# Update the most recent commit message
git commit --amend -m "New Message"

File Management

Removing and Moving Files

# Remove a file from Git tracking and filesystem
git rm index.html

# Remove file from Git tracking but keep in filesystem
git rm --cached index.html

# Move or rename files
git mv index.html dir/index_new.html

Undoing Changes

Made a mistake? Git has you covered:

# Restore a file from the latest committed version
git checkout -- index.html

# Restore file from a specific commit
git checkout 6eb715d -- index.html

# Unstage a file (undo add)
git reset HEAD index.html

Understanding Resets and Reverts

Git offers different ways to undo changes depending on your needs:

# Revert to a previous commit (creates new commit)
git revert 073791e7dd71b90daa853b2c5acc2c925f02dbc6

# Soft reset: Move HEAD only (staging and working directory unchanged)
git reset --soft 073791e7dd71b90daa853b2c5acc2c925f02dbc6

# Undo the latest commit (keeps changes staged)
git reset --soft HEAD~

# Mixed reset: Move HEAD and reset staging (working directory unchanged)
git reset --mixed 073791e7dd71b90daa853b2c5acc2c925f02dbc6

# Hard reset: Reset everything to match the commit (DANGER!)
git reset --hard 073791e7dd71b90daa853b2c5acc2c925f02dbc6

Warning: Be careful with hard resets as they permanently discard changes!

Cleaning Untracked Files

# Preview which files would be deleted
git clean -n

# Delete untracked files
git clean -f

Working with Branches

Branches are fundamental to Git workflows, allowing you to develop features independently.

Creating and Managing Branches

# Show all local branches
git branch

# Create a new branch
git branch branchname

# Switch to a branch
git checkout branchname

# Create and switch to a new branch in one command
git checkout -b branchname

# Rename a branch
git branch -m branchname new_branchname
# or
git branch --move branchname new_branchname

# Show branches that are completely merged
git branch --merged

# Delete a merged branch
git branch -d branchname
# or
git branch --delete branchname

# Force delete an unmerged branch
git branch -D branch_to_delete

Merging Branches

# Fast-forward merge (default)
git merge branchname

# Merge only if fast-forward is possible
git merge --ff-only branchname

# Force a merge commit even if fast-forward is possible
git merge --no-ff branchname

# Abort a merge (in case of conflicts)
git merge --abort

# Alternative abort method (for Git < v1.7.4)
git reset --merge

Stashing: Temporary Storage for Changes

Stashing lets you save work in progress without committing:

# Stash your changes with a descriptive message
git stash save "Message"

# List all stashes
git stash list

# Show files changed in a stash
git stash show stash@{0}

# Show detailed changes in a stash
git stash show -p stash@{0}

# Apply a stash and remove it from the stash list
git stash pop stash@{0}

# Apply a stash without removing it
git stash apply stash@{0}

# Delete a specific stash
git stash drop stash@{0}

# Delete all stashes
git stash clear

Working with .gitignore

Prevent sensitive or unnecessary files from being tracked:

# Create or edit .gitignore
nano .gitignore

# Track an empty directory with a placeholder file
touch dir/.gitkeep

Resources:

  • How to use .gitignore
  • Useful .gitignore templates

Already committed files you want to ignore? Check out this solution.

Viewing History and Changes

Commit Logs

Git’s log command is incredibly powerful for viewing project history:

# Show all commits
git log

# One-line summary of commits
git log --oneline

# Show last three commits
git log --oneline -3

# Show full SHA-1 in one-line format
git log --format=oneline

# Filter by author
git log --author="Sven"

# Filter by commit message
git log --grep="Message"

# Filter by date
git log --until=2013-01-01
git log --since=2013-01-01

# Show commits with different detail levels
git log --format=short
git log --format=full
git log --format=fuller
git log --format=email
git log --format=raw

# Show changes introduced in each commit
git log -p

# Show commits for a specific file since a certain commit
git log 6eb715d.. index.html

# Show changes for a specific file since a certain commit
git log -p 6eb715d.. index.html

# Show statistics about changes
git log --stat --summary

# Visualize branch history as a graph
git log --graph

# Detailed graph with all branches
git log --oneline --graph --all --decorate

Comparing Changes with Diff

# Show changes in working directory
git diff

# Highlight word-level changes
git diff --color-words index.html

# Compare staged changes
git diff --staged

# Compare branches
git diff master..branchname
git diff --color-words master..branchname^

# Compare commits
git diff 6eb715d
git diff 6eb715d..HEAD
git diff 6eb715d..537a09f

# Compare specific file between commits
git diff 6eb715d index.html
git diff 6eb715d..537a09f index.html

# Ignore whitespace changes
git diff -b 6eb715d..HEAD
# or
git diff --ignore-space-change 6eb715d..HEAD

# Ignore all whitespace
git diff -w 6eb715d..HEAD
# or
git diff --ignore-all-space 6eb715d..HEAD

# Summary of changes
git diff --stat --summary 6eb715d..HEAD

Finding Who Changed What

# Show who last modified each line (blame)
git blame -L10,+1 index.html

Tagging Releases

Tags mark important points in your repository’s history:

# Show all tags
git tag

# Show tags with their annotations
git tag -l -n1

# Create a lightweight tag
git tag v1.0.0

# Create an annotated tag with a message
git tag -a v1.0.0 -m 'Message'

# Checkout a specific tag
git checkout v1.0.0

Working with Remote Repositories

Setting Up Remotes

# Show remote repositories
git remote

# Show remote details
git remote -v

# Add a remote repository (GitHub)
git remote add origin https://github.com/user/project.git

# Add a remote repository (SSH)
git remote add origin ssh://root@123.123.123.123/path/to/repository/.git

# Remove a remote
git remote rm origin

# Show remote branches
git branch -r

# Show all branches (local and remote)
git branch -a

Pushing Changes

# Push to remote and set upstream (first time)
git push -u origin master

# Push to default remote
git push origin master

# Push to GitHub Pages
git push -u origin gh-pages

# Delete a remote branch
git push origin :branchname
# or
git push origin --delete branchname

Pulling and Fetching

# Fetch changes from remote (doesn't merge)
git fetch origin

# Pull changes from remote (fetch + merge)
git pull

# Pull a specific branch
git pull origin branchname

# Merge fetched commits
git merge origin/master

# Compare local branch with remote
git diff origin/master..master

Cloning Repositories

# Clone a repository
git clone https://github.com/user/project.git

# Clone via SSH
git clone ssh://user@domain.com/~/dir/.git

# Clone to a specific directory
git clone https://github.com/user/project.git ~/dir/folder

# Clone a specific branch
git clone -b branchname https://github.com/user/project.git

Advanced Operations

Creating Archives

# Create a zip archive of the current state
git archive --format zip --output filename.zip master

Exporting Information

# Export commit history to a file
git log --author=sven --all > log.txt

Security: Hiding .git on Web Servers

Add this to your .htaccess file to prevent web access to Git files:

RedirectMatch 404 /\.git

More information on this technique

Git Large File Storage (LFS)

For projects with large binary files, Git LFS is essential:

# Install Git LFS (using Homebrew on macOS)
brew install git-lfs

# Track large files (e.g., Photoshop files)
git lfs track "*.psd"

# Then use standard Git commands: init, add, commit, push

Visit the Git LFS website for more information.

Best Practices and Tips

General Guidelines

  1. Don’t commit dependencies – Use package managers and .gitignore for node_modules, vendor directories, etc.
  2. Write meaningful commit messages – Your future self will thank you
  3. Commit often – Small, focused commits are easier to review and revert
  4. Use branches – Keep your main branch stable
  5. Review before committing – Use git diff and git status frequently

Helpful Resources

  • Git Style Guide
  • Git Branch Naming Best Practices
  • Pretty Git Branch Graphs
  • Basic Git Commands (Atlassian)
  • Git Best Practices for Teams
  • Hello World – Git Tutorial
  • Interactive Beginners Tutorial
  • Git Tips from the Trenches

Conclusion

Git is an incredibly powerful tool with a steep learning curve, but mastering these commands will significantly improve your development workflow. Start with the basics, practice regularly, and gradually incorporate more advanced features as you become comfortable.

Remember: Git is designed to preserve your work and make collaboration easier. Don’t be afraid to experiment – with proper branching and the ability to undo changes, Git provides a safety net for your code.

Happy coding, and may your merges always be conflict-free! 🚀


Have a Git tip or trick not covered here? Share your experience in the comments below!

  • Share on facebook
  • Share on twitter
  • Share on linkedin

Latest Posts

  • 10 Cron Automations Marketing Developers Should Steal Today
  • When to Stop Building and Start Shipping
  • The Complete Git Command Guide: From Beginner to Advanced
  • Postmark MCP Server
  • Comprehensive PHP CLI Commands
  • KrateCMS: A Developer-Friendly CMS with Laravel 11
  • Managing Multiple .env Files in Laravel for Local, Staging, and Production Environments
Posted in WebTags: git

Post navigation

Previous: Postmark MCP Server
Next: When to Stop Building and Start Shipping

Copyright © 2026. Jabal Torres. All rights reserved.

  • About
  • Work
  • Blog
  • Contact Me