Git Commands Tutorial
Master 30 essential Git commands every developer should know
git config
What it does
Sets configuration values like username, email, and editor. Use --global for user-wide settings.
Example
$ git config --global user.name "John Doe"Output
(sets username globally)
git init
What it does
Initializes a new Git repository in the current directory. Creates a hidden .git folder.
Example
$ git initOutput
Initialized empty Git repository in /project/.git/
git clone
What it does
Creates a copy of a remote repository on your local machine. Downloads all history and branches.
Example
$ git clone https://github.com/user/repo.gitOutput
Cloning into 'repo'... Receiving objects: 100%
git remote
What it does
Manages remote repository connections. Use -v to see URLs, add to link new remotes.
Example
$ git remote -vOutput
origin https://github.com/user/repo.git (fetch) origin https://github.com/user/repo.git (push)
git status
What it does
Shows the working tree status - modified files, staged changes, and untracked files.
Example
$ git statusOutput
On branch main Changes not staged for commit: modified: index.js
git add
What it does
Stages file changes for commit. Use . for all files, -p for interactive staging.
Example
$ git add .Output
(stages all changes)
git commit
What it does
Records staged changes to the repository. Use -m for inline message, -a to auto-stage tracked files.
Example
$ git commit -m "Add new feature"Output
[main abc1234] Add new feature 2 files changed, 50 insertions(+)
git push
What it does
Uploads local commits to a remote repository. Use -u to set upstream for first push.
Example
$ git push origin mainOutput
Enumerating objects: 5, done. Writing objects: 100%
git pull
What it does
Fetches and integrates changes from remote. Combines fetch and merge in one command.
Example
$ git pull origin mainOutput
Updating abc1234..def5678 Fast-forward 2 files changed
git fetch
What it does
Downloads objects and refs from remote without merging. Safe way to check remote changes.
Example
$ git fetch originOutput
From https://github.com/user/repo * branch main -> FETCH_HEAD
git diff
What it does
Shows changes between commits, working tree, or staged changes. Use --staged for staged diff.
Example
$ git diffOutput
-old line +new line
git stash
What it does
Temporarily saves uncommitted changes. Use pop to restore, list to see all stashes.
Example
$ git stashOutput
Saved working directory and index state WIP on main: abc1234
git branch
What it does
Lists, creates, or deletes branches. Use -d to delete, -a to show all including remote.
Example
$ git branch feature/loginOutput
(creates new branch)
git checkout
What it does
Switches branches or restores files. Use -b to create and switch in one command.
Example
$ git checkout -b feature/authOutput
Switched to a new branch 'feature/auth'
git switch
What it does
Modern way to switch branches (Git 2.23+). Use -c to create new branch.
Example
$ git switch mainOutput
Switched to branch 'main'
git merge
What it does
Joins two or more development histories together. Use --no-ff to preserve merge commit.
Example
$ git merge feature/loginOutput
Merge made by 'recursive' strategy. 3 files changed
git rebase
What it does
Reapplies commits on top of another base. Use -i for interactive mode to squash/edit.
Example
$ git rebase mainOutput
Successfully rebased and updated refs/heads/feature.
git cherry-pick
What it does
Applies changes from specific commits. Useful for moving single commits between branches.
Example
$ git cherry-pick abc1234Output
[main def5678] Pick commit message 1 file changed
git remote add
What it does
Adds a new remote repository connection with a shortname.
Example
$ git remote add upstream https://github.com/original/repo.gitOutput
(adds remote)
git push -u
What it does
Pushes and sets upstream tracking. Future pushes can just use git push.
Example
$ git push -u origin feature/authOutput
Branch 'feature/auth' set up to track remote branch.
git pull --rebase
What it does
Fetches remote and rebases local commits on top. Keeps history linear.
Example
$ git pull --rebase origin mainOutput
Successfully rebased and updated refs/heads/main.
git push --force
What it does
Force pushes, overwriting remote history. Use with caution! Prefer --force-with-lease.
Example
$ git push --force-with-lease origin featureOutput
+ abc1234...def5678 feature -> feature (forced update)
git reset
What it does
Unstages files or moves HEAD. --soft keeps changes staged, --hard discards everything.
Example
$ git reset HEAD~1Output
Unstaged changes after reset: M index.js
git revert
What it does
Creates a new commit that undoes a previous commit. Safe for shared history.
Example
$ git revert abc1234Output
[main def5678] Revert "Add feature" 1 file changed
git restore
What it does
Restores working tree files (Git 2.23+). Use --staged to unstage files.
Example
$ git restore --staged index.jsOutput
(unstages file)
git clean
What it does
Removes untracked files from working directory. Use -n for dry run, -f to force.
Example
$ git clean -fdOutput
Removing temp/ Removing debug.log
git commit --amend
What it does
Modifies the most recent commit. Can change message or add forgotten files.
Example
$ git commit --amend -m "Better message"Output
[main abc1234] Better message Date: Sat Jan 11
git log
What it does
Shows commit history. Use --oneline for compact view, --graph for branch visualization.
Example
$ git log --oneline -5Output
abc1234 Add feature def5678 Fix bug ghi9012 Initial commit
git show
What it does
Displays detailed information about a commit, including the diff.
Example
$ git show abc1234Output
commit abc1234 Author: John +new code added
git blame
What it does
Shows who last modified each line of a file. Great for finding when bugs were introduced.
Example
$ git blame index.jsOutput
abc1234 (John 2026-01-11) const app = express();
💡 Pro Tip
Use git help <command> to see detailed help for any command.
Example: git help commit
About Git Commands
Quick reference for Git version control commands. From basic commits to advanced rebasing, find the command you need with syntax and examples. Perfect for daily development work.
Git is essential for modern development but has a steep learning curve. This cheat sheet covers everyday commands plus the less common ones you'll need when things go wrong—with clear examples.
How to use Git Commands
Search for what you want to do (e.g., 'undo commit').
Or browse by category (basics, branching, undoing).
View the command syntax.
Click to copy the command.
Paste into your terminal.
Examples
Basic workflow
The commands you'll use every day:
git status # Check what changed git add . # Stage all changes git commit -m "msg" # Commit with message git push # Push to remote git pull # Get remote changes
Branching
Working with branches:
git branch # List branches git branch feature # Create branch git checkout feature # Switch to branch git checkout -b fix # Create and switch git merge feature # Merge into current
Undoing changes
When you need to go back:
git restore file.txt # Discard changes git reset HEAD~1 # Undo last commit (keep changes) git reset --hard HEAD~1 # Undo last commit (lose changes) git revert abc123 # Create undo commit
Features
When to use this
- •Daily development workflow
- •Learning Git version control
- •Recovering from mistakes
- •Branch and merge operations
- •Collaborating with teams
- •Code review workflows