Skip to content

Instantly share code, notes, and snippets.

@andersonbosa
Created August 8, 2025 19:56
Show Gist options
  • Save andersonbosa/e31873e2e4cbffcee9e0c952516575cd to your computer and use it in GitHub Desktop.
Save andersonbosa/e31873e2e4cbffcee9e0c952516575cd to your computer and use it in GitHub Desktop.

Git Syntax Cheat Sheet

1. HEAD

  • HEAD refers to the latest commit on the current branch.
  • HEAD^ refers to the parent commit of HEAD.
  • HEAD^^ refers to the grandparent commit, and so on.

2. Tilde ~

  • HEAD~n: Refers to the commit n commits before HEAD. For example, HEAD~2 refers to the second ancestor commit.

3. Caret ^

  • HEAD^n: Refers to the nth parent of a merge commit, useful for merge commits with more than two parents.

4. Double Dot ..

  • A..B: Refers to all commits reachable from B but not from A (commits in B that are not in A).

5. Triple Dot ...

  • A...B: Refers to all commits in both A and B, except for those reachable by both (symmetric difference).

6. Plus Sign +

  • +branch_name: Forces an update by discarding any in-progress changes.

Examples

Example 1: Reviewing History

  • git log HEAD~3..HEAD: Shows the log for the last three commits up to the current HEAD.

Example 2: Comparing Branches

  • git diff master..feature: Shows the difference between the master branch and the feature branch, displaying changes exclusive to feature.

Example 3: Understanding Differences

  • git log branchA...branchB: Lists all commits that are in branchA or branchB but not in both, helping to understand divergent histories.

Example 4: Resetting Changes

  • git reset --hard HEAD^: Moves the current branch pointer to the parent of the current commit, discarding all changes since that commit.

Example 5: Forced Update

  • git push origin +master: Forcefully updates the remote master branch to match the local master, potentially overwriting changes on the remote.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment