Skip to main content
Ganesh Joshi
Back to Blogs

Git commands you use every day (and when to use them)

February 9, 20265 min read
Tutorials
Terminal or command line on screen

Git is the standard version control system for development teams. You do not need to memorize every command; a small set covers most day-to-day work. Here is a practical reference with when to use each.

Checking status and history

git status

See what has changed:

git status

Shows:

  • Modified files (not staged)
  • Staged files (ready to commit)
  • Untracked files (new)
  • Current branch

Use it often to know where you are.

git log

View commit history:

git log                    # Full history
git log --oneline          # Compact view
git log -n 5               # Last 5 commits
git log --graph            # Branch visualization
git log --author="name"    # Filter by author
git log -- path/to/file    # History of specific file

git diff

See what changed:

git diff                   # Unstaged changes
git diff --staged          # Staged changes
git diff HEAD~1            # Changes since last commit
git diff branch1..branch2  # Difference between branches

Saving changes

git add

Stage files for commit:

git add file.js            # Stage specific file
git add .                  # Stage all changes
git add -p                 # Stage interactively (review each change)

Only staged changes are included in the next commit.

git commit

Create a commit:

git commit -m "Add user login feature"
git commit                 # Opens editor for message
git commit -am "message"   # Add all tracked + commit

Write clear, present-tense messages. Describe what the commit does.

git commit --amend

Modify the last commit:

git commit --amend -m "Better message"
git commit --amend --no-edit  # Add staged changes to last commit

Only amend commits you have not pushed, or coordinate with your team.

Syncing with remote

git pull

Get and merge remote changes:

git pull                   # Pull current branch
git pull origin main       # Pull specific branch
git pull --rebase          # Rebase instead of merge

Run before starting work to avoid conflicts later.

git push

Send commits to remote:

git push                   # Push current branch
git push origin main       # Push specific branch
git push -u origin feature # Set upstream for new branch

git fetch

Download without merging:

git fetch                  # Fetch all remotes
git fetch origin           # Fetch specific remote

Safer when you want to inspect before merging.

Branches

git branch

List and manage branches:

git branch                 # List local branches
git branch -a              # List all (including remote)
git branch feature-x       # Create new branch
git branch -d feature-x    # Delete branch
git branch -D feature-x    # Force delete

git switch / git checkout

Switch branches:

git switch main            # Switch to existing branch
git switch -c feature-x    # Create and switch
git checkout main          # Older syntax (still works)
git checkout -b feature-x  # Older create and switch

Use switch (Git 2.23+) for clarity.

git merge

Merge branches:

git merge feature-x        # Merge feature-x into current branch
git merge --no-ff feature  # Force a merge commit

If conflicts occur:

  1. Git marks conflicting files
  2. Edit files to resolve (look for <<<<<<<, =======, >>>>>>>)
  3. git add resolved files
  4. git commit to complete

Undoing changes

git restore

Discard changes (Git 2.23+):

git restore file.js        # Discard unstaged changes
git restore --staged file  # Unstage (keep changes)
git restore .              # Discard all unstaged changes

Older syntax: git checkout -- file.js

git reset

Move HEAD and optionally change staging/working directory:

git reset --soft HEAD~1    # Undo commit, keep changes staged
git reset HEAD~1           # Undo commit, keep changes unstaged
git reset --hard HEAD~1    # Undo commit, discard changes

Be careful with --hard on shared branches.

git revert

Create a new commit that undoes a previous one:

git revert abc123          # Revert specific commit
git revert HEAD            # Revert last commit

Safe for shared branches since it adds history instead of rewriting.

Useful combinations

Start a feature

git checkout main
git pull
git switch -c feature/new-thing

Finish a feature

git add .
git commit -m "Add new thing"
git push -u origin feature/new-thing
# Create PR on GitHub/GitLab

Update feature branch with main

git checkout feature
git merge main             # Or: git rebase main
# Resolve any conflicts
git push

Quick fix on main

git stash                  # Save current work
git checkout main
git pull
# Make fix
git commit -am "Fix bug"
git push
git checkout feature
git stash pop              # Restore work

Command cheatsheet

Command Purpose
git status See current state
git log --oneline View history
git diff See changes
git add . Stage all
git commit -m "msg" Commit
git push Send to remote
git pull Get from remote
git switch -c name New branch
git merge branch Merge
git restore file Discard changes
git reset --soft HEAD~1 Undo commit

Tips

  1. Commit often - Small, focused commits are easier to review and revert
  2. Pull before push - Avoid conflicts by staying updated
  3. Use branches - Keep main clean, develop on feature branches
  4. Write good messages - Future you will thank present you
  5. Use aliases - Set up shortcuts for common commands

Example aliases

git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.lg "log --oneline --graph"

Summary

These commands cover most daily Git work:

  1. Check status with git status and git log
  2. Save changes with git add and git commit
  3. Sync with git pull and git push
  4. Branch with git switch and git merge
  5. Undo with git restore and git reset

The Git documentation is the authoritative reference for full syntax and options.

Frequently Asked Questions

The essentials are: git status, git add, git commit, git push, git pull, git branch, git checkout/switch, git merge, and git log. These cover most daily workflows.

git fetch downloads changes from the remote but does not merge. git pull fetches and merges in one step. Use fetch when you want to inspect changes before merging.

Use 'git reset --soft HEAD~1' to undo the last commit but keep changes staged. Use 'git reset --hard HEAD~1' to discard changes completely (be careful).

git switch (Git 2.23+) is specifically for switching branches. git checkout does many things. For branches, switch is clearer and recommended.

Git marks conflicting files. Open them, find the conflict markers (<<<, ===, >>>), edit to resolve, then git add the files and git commit to complete the merge.

Related Posts