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