Skip to content

Git Commands#

Show status#

git status

Show commit logs#

git log

Show compact log#

git log --oneline

Show unstaged changes#

git diff

Show staged changes#

git diff --staged

Stage file#

git add <file>

Stage all changes#

git add .

Commit with message#

git commit -m "msg"

Stage and commit tracked files#

git commit -am "msg"

Amend last commit#

git commit --amend

List branches#

git branch

Create branch#

git branch <name>

Switch branch#

git checkout <name>

Create and switch branch#

git checkout -b <name>

Delete branch#

git branch -d <name>

Merge branch#

git merge <branch>

List remotes#

git remote -v

Fetch remote changes#

git fetch

Pull from remote#

git pull

Push to remote#

git push

Push and set upstream#

git push -u origin main

Clone repository#

git clone <url>

Discard changes in file#

git restore <file>

Unstage file#

git restore --staged <file>

Undo last commit (keep changes)#

git reset HEAD~1

Undo last commit (discard changes)#

git reset --hard HEAD~1

Revert commit#

git revert <commit>

Stash changes#

git stash

List stashes#

git stash list

Apply and remove stash#

git stash pop

Apply stash (keep it)#

git stash apply

Delete stash#

git stash drop

Visual branch history#

git log --graph --oneline --all

Last 5 commits#

git log -n 5

Commits by author#

git log --author="name"

Show commit details#

git show <commit>