Git Commands

Essential Git commands reference with descriptions.

Setup & Config

git config --global user.name "Your Name"

Set your name

git config --global user.email "email@example.com"

Set your email

git config --list

List all settings

git init

Initialize a new repository

git clone <url>

Clone a repository

Basic Commands

git status

Check status of working directory

git add <file>

Stage a file

git add .

Stage all changes

git commit -m "message"

Commit staged changes

git commit -am "message"

Stage and commit all changes

git diff

Show unstaged changes

git diff --staged

Show staged changes

Branching

git branch

List all branches

git branch <name>

Create a new branch

git checkout <branch>

Switch to a branch

git checkout -b <branch>

Create and switch to new branch

git branch -d <branch>

Delete a branch

git merge <branch>

Merge branch into current branch

Remote

git remote -v

List remote repositories

git remote add origin <url>

Add remote repository

git push origin <branch>

Push to remote branch

git push -u origin <branch>

Push and set upstream

git pull

Fetch and merge from remote

git fetch

Download remote changes

History

git log

Show commit history

git log --oneline

Show condensed history

git log --graph

Show branch graph

git show <commit>

Show commit details

git blame <file>

Show who changed each line

Undo Changes

git reset <file>

Unstage a file

git reset --hard

Discard all changes

git checkout -- <file>

Discard changes to file

git revert <commit>

Create new commit that undoes changes

git clean -fd

Remove untracked files and directories

Stashing

git stash

Stash current changes

git stash list

List all stashes

git stash pop

Apply and remove latest stash

git stash apply

Apply latest stash

git stash drop

Delete latest stash

Tags

git tag

List all tags

git tag <name>

Create a lightweight tag

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

Create annotated tag

git push origin <tag>

Push tag to remote

git push origin --tags

Push all tags