|
| 1 | +--- |
| 2 | +author: "Phong Nguyen" |
| 3 | +title: "Git Notes" |
| 4 | +date: "2026-05-30" |
| 5 | +description: "Git Notes" |
| 6 | +tags: ["git"] #tags search |
| 7 | +FAcategories: ["syntax"] #The category of the post, similar to tags but usually for broader classification. |
| 8 | +FAseries: ["Themes Guide"] #indicates that this post is part of a series of related posts |
| 9 | +aliases: ["migrate-from-jekyl"] #Alternative URLs or paths that can be used to access this post, useful for redirects from old posts or similar content. |
| 10 | +ShowToc: true # Determines whether to display the Table of Contents (TOC) for the post. |
| 11 | +TocOpen: true # Controls whether the TOC is expanded when the post is loaded. |
| 12 | +weight: 10 # The order in which the post appears in a list of posts. Lower numbers make the post appear earlier. |
| 13 | +--- |
| 14 | + |
| 15 | +## Git Cheat Sheet |
| 16 | + |
| 17 | +**`<commit>`** can be any Git reference that points to a commit |
| 18 | + |
| 19 | +Local git config: `.git/config` |
| 20 | +(See all possible config options man git-config) |
| 21 | +Global git config: `~/.gitconfig` |
| 22 | +Git ignore file : `.gitignore` |
| 23 | +Local ignore rules (not committed): `.git/info/exclude` |
| 24 | + |
| 25 | +```bash |
| 26 | +# Start a new repo |
| 27 | +$ git init |
| 28 | + |
| 29 | +# clone an existing repo |
| 30 | +$ git clone <url> |
| 31 | + |
| 32 | +# Add untracked file or unstaged changes |
| 33 | +$ git add <file> |
| 34 | + |
| 35 | +# Add all untracked files and unstaged changes |
| 36 | +$ git add . |
| 37 | + |
| 38 | +### Choose which parts of a file to stage |
| 39 | +$ git add -p |
| 40 | + |
| 41 | +# Delete file |
| 42 | +$ git rm <file> |
| 43 | + |
| 44 | +# Tell Git to forget about a file without deleting it |
| 45 | +$ git rm --cached <file> |
| 46 | + |
| 47 | +# Unstage one file |
| 48 | +git reset <file> |
| 49 | + |
| 50 | +# Unstage everything |
| 51 | +git reset |
| 52 | + |
| 53 | +# Check what you added |
| 54 | +git status |
| 55 | + |
| 56 | +# Make a commit (and open text editor to write message) |
| 57 | +git commit |
| 58 | + |
| 59 | +# Make a commit |
| 60 | +git commit -m 'message' |
| 61 | + |
| 62 | +# Commit all unstaged changes |
| 63 | +git commit -am 'message' |
| 64 | + |
| 65 | +# Make an empty commit for specific purpose |
| 66 | +git commit --allow-empty -m 'message' |
| 67 | + |
| 68 | +# Switch branches |
| 69 | +$ git switch <name> |
| 70 | +$ git checkout <name> |
| 71 | + |
| 72 | +# Create a new branch from the current branch |
| 73 | +$ git switch -c <new-branch> |
| 74 | +$ git checkout -b <new-branch> |
| 75 | + |
| 76 | +# Create a new branch from another local branch |
| 77 | +$ git switch -c <new-branch> <base-branch> # prefer |
| 78 | +$ git checkout -b <new-branch> <base-branch> |
| 79 | + |
| 80 | +# Create a new branch from a remote branch |
| 81 | +$ git switch -c <new-branch> origin/<remote-branch> # prefer |
| 82 | +$ git checkout -b <new-branch> origin/<remote-branch> |
| 83 | + |
| 84 | +# List branches |
| 85 | +$ git branch |
| 86 | +$ git branch -r # remote branches |
| 87 | + |
| 88 | +# Delete a branch |
| 89 | +$ git branch -d <name> |
| 90 | +$ git branch -D <name> # Force |
| 91 | + |
| 92 | +# Diff all staged and unstaged changes |
| 93 | +$ git diff HEAD |
| 94 | + |
| 95 | +# Diff just staged changes |
| 96 | +$ git diff --staged |
| 97 | + |
| 98 | +# Diff just unstaged changes |
| 99 | +$ git diff |
| 100 | + |
| 101 | +# Save to file |
| 102 | +$ git diff >> file |
| 103 | + |
| 104 | +# Compare two refs and list changed files |
| 105 | +git diff --name-only <branch> <commit> > files.txt |
| 106 | + |
| 107 | +# Show diff between a commit and its parent |
| 108 | +git show <commit> |
| 109 | + |
| 110 | +# Diff two commits |
| 111 | +git diff <commit> <commit> |
| 112 | + |
| 113 | +# Diff one file since a commit |
| 114 | +git diff <commit> <file> |
| 115 | + |
| 116 | +# Show a summary of a diff |
| 117 | +git diff <commit> --stat git show <commit> --stat |
| 118 | + |
| 119 | +# Discard unstaged changes in one file |
| 120 | +git restore <file> |
| 121 | +git checkout -- <file> |
| 122 | + |
| 123 | +# Discard all changes (staged and unstaged) in one file |
| 124 | +git restore --staged --worktree <file> |
| 125 | +git checkout HEAD -- <file> |
| 126 | + |
| 127 | +# Delete all staged and unstaged changes |
| 128 | +git reset --hard |
| 129 | + |
| 130 | +# Force Delete untracked files/director |
| 131 | +git clean -fd |
| 132 | + |
| 133 | +# 'Stash' all staged and unstaged changes |
| 134 | +git stash |
| 135 | + |
| 136 | +# "Undo" the most recent commit |
| 137 | +git reset HEAD^ |
| 138 | + |
| 139 | +# Squash the last 5 commits into one |
| 140 | +git rebase -i HEAD~6 |
| 141 | + |
| 142 | +# Open the log history |
| 143 | +git reflog |
| 144 | +git reflog BRANCHNAME |
| 145 | + |
| 146 | +# Move current branch back to <commit> |
| 147 | +git reset --hard <commit> |
| 148 | + |
| 149 | +# Recreate the branch using hash |
| 150 | +git checkout -b <new-branch> <commit> |
| 151 | + |
| 152 | +# Change a commit message / add a file |
| 153 | +git commit --amend |
| 154 | + |
| 155 | +# Show commit history |
| 156 | +git log main |
| 157 | + |
| 158 | +# Show compact commit history |
| 159 | +git log --oneline main |
| 160 | + |
| 161 | +# Show commit history as a branch graph |
| 162 | +git log --graph main |
| 163 | + |
| 164 | +# Show a compact graph of all branches |
| 165 | +git log --oneline --graph --all --decorate |
| 166 | + |
| 167 | +# Show every commit that modified a file |
| 168 | +git log <file> |
| 169 | + |
| 170 | +# Show every commit that modified a file, including before it was renamed |
| 171 | +git log --follow <file> |
| 172 | + |
| 173 | +# Find every commit that added or removed some text |
| 174 | +git log -G "<text>" |
| 175 | + |
| 176 | +# Show who last changed each line of a file |
| 177 | +git blame <file> |
| 178 | + |
| 179 | +# Cherry pick to copy one commit onto the current branch |
| 180 | +git cherry-pick <commit> |
| 181 | + |
| 182 | +# Replace the current version of a file with the version from another commit |
| 183 | +git restore --source <commit> <file> |
| 184 | + |
| 185 | +# Add a Remote |
| 186 | +git remote add <name> <url> |
| 187 | + |
| 188 | +# Push the main branch to the remote origin |
| 189 | +git push origin main |
| 190 | + |
| 191 | +# Push the current branch to its remote "tracking branch" |
| 192 | +git push |
| 193 | + |
| 194 | +# Push a branch that you've never pushed before |
| 195 | +git push -u origin <name> |
| 196 | + |
| 197 | +# Force push |
| 198 | +git push --force-with-lease # the remote has not changed |
| 199 | +git push --force |
| 200 | + |
| 201 | +# Create a tag |
| 202 | +$ git tag <tag> |
| 203 | +$ git tag -a <tag> -m "<des>" |
| 204 | + |
| 205 | +# Push tags |
| 206 | +$ git push origin <tag> |
| 207 | +$ git push --tags |
| 208 | + |
| 209 | +# Fetch changes |
| 210 | +git fetch origin main |
| 211 | + |
| 212 | +# Fetch changes and then rebase your current branch |
| 213 | +git pull --rebase |
| 214 | + |
| 215 | +# Set a config option |
| 216 | +git config user.name <user_name> |
| 217 | +git config user.email <user_email> |
| 218 | + |
| 219 | +# Set option globally |
| 220 | +git config --global ... |
| 221 | + |
| 222 | +# Mark a repository as safe (useful with Docker, WSL, shared folders, etc.) |
| 223 | +$ git config --global --add safe.directory <path> |
| 224 | + |
| 225 | +# Initialize and update submodules |
| 226 | +git submodule update --init |
| 227 | + |
| 228 | +# Run a single Git command without SSL certificate verification |
| 229 | +git -c http.sslVerify=false <git-command> |
| 230 | + |
| 231 | +# Set upstream branch |
| 232 | +git branch --set-upstream-to=origin/<branch_name> |
| 233 | + |
| 234 | +# Copy a file from another branch without switching branches |
| 235 | +git checkout <branch> -- <file> |
| 236 | +``` |
| 237 | + |
| 238 | +--- |
| 239 | +TBD |
0 commit comments