Quick Answer: Git is the world’s most popular version-control system. The commands you’ll use most are git clone, git add, git commit, git push, git pull, and git branch. This complete guide walks through every essential Git command โ from your first commit to branching, remotes, and undoing mistakes โ with links to in-depth tutorials for each.
What Is Git?
Git is a free, open-source distributed version control system that tracks changes to your code, lets multiple people collaborate, and makes it easy to branch, merge, and roll back. It’s a foundational skill for every developer and DevOps engineer.
Git Setup & Configuration
git --version # check Git is installed
git config --global user.name "You" # set your name
git config --global user.email "you@example.com"
git config --list # view configurationStarting a Repository
git init # create a new repo in the current folder
git clone <url> # copy a remote repo locally
git status # see what's changedThe Core Workflow (Add, Commit, Push)
git add . # stage all changes
git add file.txt # stage a specific file
git commit -m "message" # commit staged changes
git push # upload commits to the remote
git pull # fetch and merge remote changesBranching & Merging
git branch # list local branches
git branch feature # create a branch
git checkout feature # switch to a branch
git switch -c feature # create and switch (modern syntax)
git merge feature # merge a branch into the current one๐ Deep dives: How to Delete a Git Branch (Locally & Remotely) ยท How to List Remote Branches in Git.
Working with Remotes
git remote -v # list remotes
git remote add origin <url> # add a remote
git fetch origin # download remote changes (no merge)
git branch -r # list remote branchesUndoing Changes & Mistakes
git restore file.txt # discard local changes to a file
git reset --soft HEAD~1 # undo last commit, keep changes staged
git reset --hard HEAD~1 # undo last commit AND discard changes
git revert <commit> # safely undo a pushed commit๐ Deep dive: How to Undo the Most Recent Local Commits in Git.
Git Commands Cheat Sheet
| Command | Description |
|---|---|
git init | Initialize a new repository |
git clone <url> | Clone a remote repository |
git status | Show working-tree status |
git add | Stage changes |
git commit -m | Commit staged changes |
git push / git pull | Upload / download commits |
git branch | List or create branches |
git merge | Merge branches |
git log | View commit history |
git revert / git reset | Undo commits |
Using Git in DevOps pipelines? See our Jenkins CI/CD tutorial and the DevOps Roadmap.
Frequently Asked Questions
What are the most important Git commands?
clone, add, commit, push, pull, branch, checkout/switch, merge, and status cover the vast majority of day-to-day Git work.
What is the difference between git reset and git revert?
git reset moves the branch pointer back (rewriting history); git revert creates a new commit that undoes a change, which is safer for shared branches.
How do I undo my last commit?
Use git reset --soft HEAD~1 to undo the commit but keep your changes, or git reset --hard HEAD~1 to discard them entirely.