Essential Git Commands: A Practical Guide with Examples

admin
Devs3
Published on Nov, 30 2025 4 min read 0 comments
image

Git has become the industry standard for version control, enabling developers to collaborate efficiently and track changes effectively. Whether you're working solo or as part of a team, mastering these fundamental commands will transform your development workflow. Let's explore the most essential Git commands with practical examples you'll use daily.

Getting Started & Basic Workflow

1. Repository Setup

# Clone an existing repository
git clone https://github.com/username/project.git

# Initialize a new repository
git init

2. Tracking Changes

# Check the status of your working directory
git status
# Output shows modified files, staged changes, and untracked files

# Add specific files to staging area
git add index.html style.css

# Add all changes in current directory
git add .

# See what changes are unstaged
git diff
# Shows line-by-line differences in files not yet staged

3. Committing Changes

# Commit staged changes with a descriptive message
git commit -m "Add responsive navigation menu"

# Commit all tracked files without separate add command
git commit -am "Update API endpoint configuration"

# Modify the most recent commit (useful for fixing typos)
git commit --amend -m "Correct typo in README"

Branching and Merging

4. Working with Branches

# List all branches (asterisk shows current branch)
git branch

# Create and switch to new feature branch
git checkout -b feature/user-authentication

# Switch to existing branch
git checkout develop

# Delete a branch (after merging)
git branch -d old-feature

# Force delete an unmerged branch
git branch -D experimental-changes

# Set upstream branch for tracking
git branch --set-upstream-to origin/feature

5. Merging and Rebasing

# Merge feature branch into current branch
git checkout main
git merge feature/user-authentication

# Rebase current branch onto latest main
git checkout feature/user-authentication
git rebase main

# Interactive rebase (reword, squash, edit commits)
git rebase -i HEAD~3  # Last 3 commits

6. Remote Collaboration

# Push local branch to remote repository
git push origin feature/user-authentication

# Pull latest changes from remote
git pull origin main

# Fetch changes without merging (safe inspection)
git fetch origin
git diff origin/main  # See what would be merged

Undoing Changes

7. Soft Reset (Keep Changes)

# Undo last commit but keep changes in working directory
git reset --soft HEAD~1
# Your files remain modified, ready to recommit

8. Hard Reset (Discard Everything)

# Warning: This permanently discards changes
git reset --hard HEAD~1
# All changes from last commit are gone

# Reset to specific remote state
git reset --hard origin/main

9. Revert (Safe Undo)

# Create a new commit that undoes a specific commit
git revert abc1234
# Safer for shared branches as it doesn't rewrite history

10. Checkout Specific States

# Temporarily view project at specific commit
git checkout abc1234
# You'll be in "detached HEAD" state

# Return to latest commit
git checkout main

Advanced Workflow Tools

11. Stashing Changes

# Save uncommitted changes temporarily
git stash
# Switch branches without committing

# List all stashes
git stash list

# Apply and remove latest stash
git stash pop

# Apply specific stash (keeps it in list)
git stash apply stash@{2}

12. Inspecting History

# View commit history with stats
git log --stat
# Shows files changed and line counts

# Compact single-line log
git log --oneline

# See details of specific commit
git show abc1234

13. Cherry-Picking

# Apply a specific commit from another branch
git cherry-pick abc1234
# Useful for applying hotfixes to multiple branches

Practical Workflow Examples

Scenario 1: Daily Development

# Start your day
git checkout main
git pull origin main
git checkout -b feature/new-endpoint

# Make changes, then...
git add .
git commit -m "Add user profile API endpoint"
git push origin feature/new-endpoint

Scenario 2: Fixing Mistakes

# Oops, wrong commit message
git commit --amend -m "Correct commit message"

# Accidentally committed to wrong branch
git reset --soft HEAD~1
git stash
git checkout correct-branch
git stash pop
git add .
git commit -m "Proper commit message"

Scenario 3: Code Review Cleanup

# Squash last 3 commits into one
git rebase -i HEAD~3
# In editor: pick first, squash others

# Update with latest main
git fetch origin
git rebase origin/main
# Resolve any conflicts

Pro Tips for Effective Git Usage

  • Commit Often: Small, focused commits are easier to understand and revert
  • Write Descriptive Messages: Use present tense ("Add feature" not "Added feature")
  • Use Branches Liberally: Keep main branch stable, use branches for features
  • Pull Before Push: Always fetch latest changes before pushing
  • Review Before Committing: Use git diff --cached to review staged changes

Common Aliases for Efficiency

Add these to your ~/.gitconfig:

[alias]
    co = checkout
    br = branch
    ci = commit
    st = status
    lg = log --oneline --graph --all
    unstage = reset HEAD --
    last = log -1 HEAD

Conclusion

Mastering these Git commands will significantly improve your development workflow. Remember that Git is a powerful tool—start with the basics, practice regularly, and gradually incorporate more advanced techniques into your workflow. The key to Git proficiency isn't memorizing every command, but understanding the underlying concepts of commits, branches, and merges.

Golden Rule: When working with shared branches, prefer git revert over git reset to avoid rewriting public history. For local branches, feel free to rebase and reset as needed to keep a clean history.

By integrating these commands into your daily development practice, you'll gain confidence in managing code changes, collaborating with teams, and maintaining a clean project history.

0 Comments