Skip to main content

Core Commands

git commit

Description: Record changes to the repository Regex: /^git +commit($|\s)/ Shortcut: gc, git ci Options:
--amend
flag
Modify the most recent commit instead of creating a new one
-a
flag
Stage all modified files (displays warning that staging is automatic)
--all
flag
Same as -a, stage all modified files
-m
string
Set the commit message
-am
string
Combine -a and -m options
Examples:
git commit                    # Create new commit
git commit --amend            # Modify last commit
git commit -m "Add feature"   # Commit with message
git commit -am "Fix bug"      # Stage all and commit

git checkout

Description: Switch branches or restore working tree files Regex: /^git +checkout($|\s)/ Shortcut: go, git co Options:
-b
string
Create a new branch and switch to itFormat: git checkout -b <branch> [<start-point>]
-B
string
Create or reset a branch and switch to it (force)Format: git checkout -B <branch> [<start-point>]
-
flag
Switch to the previous branch
Examples:
git checkout main             # Switch to main branch
git checkout C3               # Detach HEAD to commit C3
git checkout -b feature       # Create and switch to feature
git checkout -b fix C2        # Create fix branch at C2
git checkout -                # Switch to previous branch

git switch

Description: Switch branches (modern alternative to checkout) Regex: /^git +switch($|\s)/ Shortcut: gsw, git sw Options:
-c
string
Create a new branch and switch to it
--create
string
Same as -c, create and switch
-C
string
Force create (or reset) and switch to branch
--force-create
string
Same as -C, force create and switch
-
flag
Switch to the previous branch
Examples:
git switch main               # Switch to main
git switch -c feature         # Create and switch to feature
git switch -                  # Switch to previous branch

git branch

Description: List, create, or delete branches Regex: /^git +branch($|\s)/ Shortcut: gb, git br Options:
-d
string[]
Delete one or more branches
-D
string[]
Force delete branches
-f
string
Force move/create branchFormat: git branch -f <branch> [<start-point>]
--force
string
Same as -f
-a
flag
List all branches (local and remote)
-r
flag
List only remote branches
-u
string
Set upstream tracking branchFormat: git branch -u <upstream> [<branch>]
--contains
string
List branches that contain a commit
Examples:
git branch                    # List local branches
git branch -a                 # List all branches
git branch feature            # Create feature branch
git branch -f main C6         # Force main to point to C6
git branch -d feature         # Delete feature branch
git branch -u o/main main     # Set main to track o/main

Merging and Rebasing

git merge

Description: Join two or more development histories together Regex: /^git +merge($|\s)/ Options:
--no-ff
string
Create a merge commit even if fast-forward is possible
--squash
string
Squash all commits into one
Examples:
git merge feature             # Merge feature into current branch
git merge --no-ff feature     # Force merge commit

git rebase

Description: Reapply commits on top of another base tip Regex: /^git +rebase($|\s)/ Shortcut: gr Options:
-i
string
Interactive rebase modeFormat: git rebase -i <base> [<branch>]
--onto
string
Rebase onto a different baseFormat: git rebase --onto <newbase> <upstream> [<branch>]
-p
flag
Preserve merge commits during rebase
--preserve-merges
flag
Same as -p
Examples:
git rebase main               # Rebase current branch onto main
git rebase main feature       # Rebase feature onto main
git rebase -i HEAD~3          # Interactive rebase last 3 commits
git rebase --onto main side bugFix  # Rebase bugFix onto main from side

git cherry-pick

Description: Apply changes from existing commits Regex: /^git +cherry-pick($|\s)/ Examples:
git cherry-pick C2            # Apply C2 to current branch
git cherry-pick C2 C4 C6      # Apply multiple commits

Remote Operations

git clone

Description: Clone a repository into a new directory Regex: /^git +clone *?$/ Examples:
git clone                     # Create origin remote

git fetch

Description: Download objects and refs from another repository Regex: /^git +fetch($|\s)/ Options:
--force
flag
Force update of local refs
Examples:
git fetch                     # Fetch all remote branches
git fetch origin              # Explicitly specify origin
git fetch origin main         # Fetch only main branch
git fetch origin main:side    # Fetch main to local side
git fetch origin :bugFix      # Create local bugFix from remote

git push

Description: Update remote refs along with associated objects Regex: /^git +push($|\s)/ Options:
--force
flag
Force push (overwrite remote)
--delete
string
Delete remote branch
-d
string
Same as --delete
Examples:
git push                      # Push current branch
git push origin main          # Push main to origin
git push origin main:remote   # Push main to remote branch
git push origin :side         # Delete remote side branch
git push origin --delete side # Same as above
git push --force              # Force push (dangerous!)

git pull

Description: Fetch from and integrate with another repository or branch Regex: /^git +pull($|\s)/ Options:
--rebase
flag
Rebase instead of merge after fetch
--force
flag
Force update local branches
Examples:
git pull                      # Pull from tracked branch
git pull origin main          # Pull main from origin
git pull --rebase             # Rebase instead of merge

git remote

Description: Manage set of tracked repositories Regex: /^git +remote($|\s)/ Options:
-v
flag
Verbose output showing URLs
Examples:
git remote                    # List remotes
git remote -v                 # List with URLs

History and Information

git log

Description: Show commit logs Regex: /^git +log($|\s)/ Golf: Does not count for golf scoring Examples:
git log                       # Show commit history
git log main                  # Show history of main

git show

Description: Show various types of objects Regex: /^git +show($|\s)/ Golf: Does not count for golf scoring Examples:
git show                      # Show HEAD commit
git show C3                   # Show commit C3

git status

Description: Show the working tree status Regex: /^git +status($|\s)/ Shortcut: gst, gs, git st Golf: Does not count for golf scoring Examples:
git status                    # Show current status

git describe

Description: Give an object a human readable name based on tags Regex: /^git +describe($|\s)/ Examples:
git describe                  # Describe HEAD
git describe main             # Describe main branch

Tags

git tag

Description: Create, list, or delete tag references Regex: /^git +tag($|\s)/ Options:
-d
string
Delete a tag
Examples:
git tag                       # List all tags
git tag v1.0                  # Create tag at HEAD
git tag v1.0 C3               # Create tag at C3
git tag -d v1.0               # Delete tag v1.0

Undo Operations

git reset

Description: Reset current HEAD to a specified state Regex: /^git +reset($|\s)/ Options:
--hard
string
Reset and discard changes (shows warning)
--soft
string
Not supported (throws staging error)
Examples:
git reset HEAD~1              # Move branch back one commit
git reset --hard C1           # Hard reset to C1

git revert

Description: Revert some existing commits Regex: /^git +revert($|\s)/ Examples:
git revert C2                 # Create commit that reverts C2
git revert C1 C2 C3           # Revert multiple commits

Utility Commands

git add

Description: Add file contents to the staging area (not functional) Regex: /^git +add($|\s)/ Shortcut: ga Golf: Does not count for golf scoring Note: Throws informational message that staging is automatic in Learn Git Branching.

git gc

Description: Cleanup unnecessary files and optimize the repository Regex: /^git +gc($|\s)/ Examples:
git gc                        # Prune unreachable commits

git rev-list

Description: List commits reachable from specified commit(s) Regex: /^git +rev-list($|\s)/ Golf: Does not count for golf scoring Examples:
git rev-list main             # List commits in main

Learn Git Branching Specific

git fakeTeamwork

Description: Simulate commits on remote branches (learning tool) Regex: /^git +fakeTeamwork($|\s)/ Format:
  • git fakeTeamwork - Add 1 commit to remote main
  • git fakeTeamwork <n> - Add n commits to remote main
  • git fakeTeamwork <branch> - Add 1 commit to remote branch
  • git fakeTeamwork <branch> <n> - Add n commits to remote branch
Examples:
git fakeTeamwork              # 1 commit on remote main
git fakeTeamwork 3            # 3 commits on remote main
git fakeTeamwork side         # 1 commit on remote side
git fakeTeamwork side 5       # 5 commits on remote side

git mergeMR / git mergePR

Description: Simulate merge request/pull request on remote (learning tool) Regex: /^git +merge[MP]R($|\s)/ Options:
--delete-after-merge
flag
Delete source branch after merge
Format: git mergeMR <source> <target> Examples:
git mergeMR feature main      # Merge feature into main on remote
git mergePR feature main --delete-after-merge

Command Shortcuts

Quick reference for command shortcuts:
ShortcutCommand
gcgit commit
gogit checkout
gbgit branch
grgit rebase
gagit add
gst, gsgit status
gswgit switch

See Also

Special Commands

Level navigation and help commands

Mercurial Commands

Alternative VCS command set

Build docs developers (and LLMs) love