Skip to main content

Git Commands Reference

A complete list of commonly used Git commands for daily development: commits, merges, tags, comparisons, branching, stashing, logs, remotes, and more.


1. Basic Commands

Initialize Repository

git init

Clone Repository

git clone <repo-url>

Check Status

git status

Add Files

git add <file>
git add .
git add -A

Remove Files

git rm <file>
git rm --cached <file>

Change Default Branch Name

git config --global init.defaultBranch <branch-name>

2. Commit Commands

Commit with message

git commit -m "message"

Commit all changes

git commit -a -m "message"

Amend last commit

git commit --amend

3. Branch Commands

List branches

git branch

Create branch

git branch <name>

Switch branch

git checkout <branch>
git switch <branch>

Create & switch branch

git checkout -b <branch>
git switch -c <branch>

Delete branch

git branch -d <branch>
git branch -D <branch>

4. Merge Commands

Merge branch into current

git merge <branch>

Abort merge

git merge --abort

5. Rebase Commands

Rebase onto another branch

git rebase <branch>

Interactive rebase

git rebase -i HEAD~5

Abort rebase

git rebase --abort

6. Tag Commands

Create tag

git tag <tag-name>

Create annotated tag

git tag -a <tag-name> -m "message"

List tags

git tag

Push tag

git push origin <tag>

Push all tags

git push --tags

Delete tag

git tag -d <tag>
git push origin :refs/tags/<tag>

7. Compare Files (Diff)

Compare working directory changes

git diff

Compare staged changes

git diff --staged

Compare two commits

git diff <commit1> <commit2>

Compare branches

git diff <branch1> <branch2>

8. Log Commands

Basic log

git log

One-line log

git log --oneline

Log with graph

git log --graph --oneline --decorate --all

Show commit details

git show <commit>

9. Stash Commands

Save changes

git stash

Save with message

git stash push -m "message"

List stash

git stash list

Apply stash

git stash apply
git stash apply stash@{n}

Pop stash

git stash pop

Drop stash

git stash drop stash@{n}

10. Remote Commands

List remotes

git remote -v

Add remote

git remote add origin <url>

Change remote URL

git remote set-url origin <url>

Remove remote

git remote remove <name>

Fetch

git fetch
git fetch --all

Pull

git pull

Push

git push

Force push

git push --force

11. Reset Commands

Soft reset

git reset --soft <commit>

Mixed reset (default)

git reset <commit>

Hard reset

git reset --hard <commit>

12. Clean Commands

Preview clean

git clean -n

Delete untracked files

git clean -f

13. Submodule Commands

Add submodule

git submodule add <url> <path>

Update submodules

git submodule update --init --recursive

14. Useful Shortcuts

View last commit

git log -1

Check which branch you are on

git branch --show-current

15. Git Ignore Files & Local File Management

Create .gitignore file

# Create .gitignore in root directory
touch .gitignore

Common .gitignore entries

# Environment files
.env
.env.local
.env.*.local

# Node modules
node_modules/
package-lock.json
yarn.lock

# Python
__pycache__/
*.pyc
*.pyo
venv/
env/

# IDE
.vscode/
.idea/
*.swp
*.swo

# Build directories
dist/
build/
out/

# Logs
logs/
*.log
npm-debug.log*

# OS files
.DS_Store
Thumbs.db

Check if file matches .gitignore pattern

git check-ignore <file>
git check-ignore -v <file>

List all ignored files

git status --ignored

Show all tracked files

git ls-files

Show ignored files

git ls-files --others --ignored --exclude-standard

16. Assume-Unchanged & Skip-Worktree Commands

These commands allow you to tell Git to ignore local changes to tracked files without committing gitignore changes.

Mark file as assumed-unchanged

git update-index --assume-unchanged <file>

This tells Git to assume the file hasn't changed locally, useful for configuration files modified locally.

Unmark assumed-unchanged

git update-index --no-assume-unchanged <file>

List all assumed-unchanged files

git ls-files -v | grep '^[a-z]'

The lowercase letter indicates an assume-unchanged file.

Skip-worktree (preferred alternative)

Skip-worktree is more reliable than assume-unchanged:

# Mark file as skip-worktree
git update-index --skip-worktree <file>

# Unmark skip-worktree
git update-index --no-skip-worktree <file>

# List all skip-worktree files
git ls-files -v | grep '^S'

Difference between assume-unchanged and skip-worktree

Featureassume-unchangedskip-worktree
PurposeOptimize performanceIgnore local changes
Git operationsBypass stat checkSkip file entirely
ReliabilityCan break with certain operationsMore reliable
Use caseLarge binary filesConfig files with local changes

Clear assume-unchanged files for a fresh start

# Remove all assume-unchanged flags
git ls-files -v | grep '^[a-z]' | awk '{print $2}' | xargs git update-index --no-assume-unchanged

# Remove all skip-worktree flags
git ls-files -v | grep '^S' | awk '{print $2}' | xargs git update-index --no-skip-worktree