Git is a distributed version control system (DVCS) created by Linus Torvalds in 2005 to manage the Linux kernel’s source code. Unlike centralized systems, Git keeps a complete history of your project on every developer’s machine, making it easy to revert, branch, and merge changes no matter your workflow.
Below is a reorganized, step-by-step guide to the 50 Git commands you’ll use most often, grouped by category. You’ll find concise explanations, usage examples, and tips so you can copy-and-paste as you learn.
📋 1. Configuration
Before you start, tell Git who you are:
# View all settings (name, email, cache, editor, etc.) git config -l # Set your name (appears in commits) git config --global user.name "Your Name" # Set your email (appears in commits) git config --global user.email "you@example.com" # Cache HTTPS credentials (default: 15 minutes) git config --global credential.helper cache # Change the cache timeout (in seconds), e.g., 1 hour: git config --global credential.helper 'cache --timeout=3600'
🌱 2. Creating a Repository
Initialize a new project:
# In your project folder, start tracking with Git git init
➕ 3. Staging Changes
Prepare your work for committing:
# Add ONE file git add filename.txt # Add ALL files (new, modified, deleted) git add . # Add only files matching a pattern, e.g., all “app-” files git add app-* # Interactive staging: choose hunks to stage git add -p
✔️ 4. Checking Status & Differences
Inspect what you’ve modified:
# Show staged vs. unstaged vs. untracked git status # Show unstaged changes in working directory git diff # Show staged changes ready to commit git diff --staged # Show changes for a specific file git diff path/to/file.py
💾 5. Committing
Record your changes in project history:
# Open your editor to write a multi-line message git commit # One-line commit message git commit -m "Short summary of changes" # Stage AND commit modified files in one go git commit -a -m "Fix typo in README"
📜 Viewing Commit History
# Simple log git log # Log with patch (diffs) git log -p # Log with stats (lines and file names changed) git log --stat # One-line, ASCII graph of commits git log --graph --oneline
🔍 Inspecting a Single Commit
# Show full details of a specific commit git show <commit-hash>
🔄 6. Undoing & Amending
# Revert unstaged changes in working directory git checkout -- filename.txt # Unstage a file (keep working-dir changes) git reset HEAD filename.txt # Unstage interactively git reset -p # Amend last commit (modify message or include new changes) git commit --amend # Create a “reverting” commit that undoes a specific commit git revert <commit-hash> # Roll back latest commit by reverting HEAD git revert HEAD
⚠️ Caution: Avoid
--amend
on commits you’ve already pushed, as rewriting history can confuse collaborators.
🌿 7. Branching & Merging
# List all local branches git branch # Create a new branch git branch feature/login # Switch to another branch git checkout feature/login # Create + switch in one command git checkout -b feature/signup # Delete a local branch (after merging) git branch -d old-branch # Merge another branch into your current one git merge feature/login # Abort a conflicted merge git merge --abort
🔗 8. Working with Remotes
Link your local repo to a shared server (GitHub, GitLab, Bitbucket):
# Add a remote named “origin” git remote add origin https://github.com/you/repo.git # List remote URLs git remote -v # Show detailed info about a remote git remote show origin # Download from remote without merging git fetch # Download + merge (shorthand) git pull # Upload commits to remote git push # Push a new branch and set upstream git push -u origin feature/login # Delete a remote branch git push --delete origin old-branch
🔄 9. Rebasing (Advanced)
Rebase lets you “replay” commits from one branch onto another:
# Non-interactive rebase onto master git rebase master # Interactive rebase (reorder, squash, edit) git rebase -i master
Tip: Rebasing gives you a cleaner, linear history—but avoid rebasing public branches.
🎯 10. Miscellaneous Tips
# View remote-tracking branches git branch -r # Show log of remote main branch git log origin/main # Update all remotes without merging git remote update # Force-push (careful: overwrites remote!) git push -f
🚀 Conclusion
With these 50 commands, you have a solid foundation to:
- Configure Git for your identity
- Stage and commit changes efficiently
- Branch, merge, and even rebase safely
- Collaborate using remotes and pull/push workflows
Bookmark this list, try them in a test repo, and soon they’ll become second nature. Happy coding!
Top comments (4)
Pretty cool, having all the main commands in one spot honestly makes life easier for me.
Nice Post!
Useful ❤️
Super helpful breakdown! Cheat sheets like this save me from making silly mistakes, do you have any tricks for dealing with tricky merge conflicts?