learn.fttgsolutions.com · Cheat Sheets
Init. Commit. Push. Repeat.
Create a Repository
| git init | git init [project name] | Initialize a new local repository in the current (or named) directory. |
| git clone | git clone <git_url> | Download a full copy of a remote repository to your machine. |
| git clone (named) | git clone <git_url> <my_directory> | Clone a remote repository into a custom-named local directory. |
Make a Change
| git status | git status | Show which files are modified, staged, or untracked. |
| git add (file) | git add [file] | Stage a specific file for the next commit. |
| git add (all) | git add . | Stage all changed and new files in the working directory. |
| git commit | git commit -m "message" | Record staged changes with a descriptive message. |
| git commit (all) | git commit -am "message" | Stage all tracked file changes and commit in one step. |
| git restore | git restore [file] | Discard unstaged changes in the working directory for a file. |
| git restore --staged | git restore --staged [file] | Unstage a file while keeping its changes in the working directory. |
| git reset (file) | git reset [file] | Unstage a file without modifying its contents. |
| git reset --hard | git reset --hard | Revert all uncommitted changes and return to the last commit state. |
| git diff | git diff | Show unstaged differences between the working directory and the index. |
| git diff --staged | git diff --staged | Show changes staged for the next commit but not yet committed. |
| git rebase | git rebase [branch] | Reapply commits from the current branch on top of another branch. |
Configuration
| config user.name | git config --global user.name "Name" | Set the name that will appear on all commits globally. |
| config user.email | git config --global user.email "email" | Set the email address that will appear on all commits globally. |
| config color.ui | git config --global color.ui auto | Enable automatic colored output in the terminal. |
| config --edit | git config --global --edit | Open the global Git configuration file in the default text editor. |
Working with Branches
| git branch | git branch | List all local branches in the repository. |
| git branch -av | git branch -av | List all local and remote branches with their latest commits. |
| git checkout | git checkout <branch> | Switch to an existing branch and update the working directory. |
| git checkout -b | git checkout -b <new_branch> | Create a new branch and immediately switch to it. |
| git branch -d | git branch -d <branch> | Delete a local branch (only if it has been fully merged). |
| git merge | git merge <branchA> | Merge the specified branch into the currently active branch. |
| git tag | git tag <my_tag> | Create a lightweight tag pointing to the current commit. |
Observe Your Repository
| git log | git log | Display the full commit history for the active branch. |
| git log (range) | git log branchB..branchA | Show commits in branchA that are not yet in branchB. |
| git log --follow | git log --follow [file] | Track the change history of a file, including across renames. |
| git diff (branches) | git diff branchB...branchA | Show the diff between what is in branchA but not in branchB. |
| git show | git show [SHA] | Display the contents and metadata of a specific commit by its SHA. |
Synchronize
| git fetch | git fetch [alias] | Download all branches from a remote without merging. |
| git merge (remote) | git merge [alias]/[branch] | Merge a remote-tracking branch into the current local branch. |
| git merge --no-ff | git merge --no-ff [alias]/[branch] | Merge without fast-forwarding, always creating a merge commit. |
| git merge --ff-only | git merge --ff-only [alias]/[branch] | Merge only if the branch can be fast-forwarded; abort otherwise. |
| git push | git push [alias] [branch] | Upload local branch commits to the remote repository. |
| git pull | git pull | Fetch from the tracked remote and merge into the current branch. |
| git cherry-pick | git cherry-pick [commit_id] | Apply the changes from a specific commit onto the current branch. |
Remote
| git remote add | git remote add [alias] [url] | Register a new remote repository under a short alias. |
| git remote | git remote | List the names of all configured remote connections. |
| git remote -v | git remote -v | List all remotes along with their fetch and push URLs. |
| git remote rm | git remote rm [name] | Remove a remote connection from the repository. |
| git remote set-url | git remote set-url origin [git_url] | Update the URL of an existing remote (e.g. after a repo migration). |
Temporary Commits (Stash)
| git stash | git stash | Save modified and staged changes onto a temporary stack. |
| git stash list | git stash list | Show all entries currently saved in the stash stack. |
| git stash pop | git stash pop | Apply the most recent stash entry and remove it from the stack. |
| git stash drop | git stash drop | Discard the most recent stash entry without applying it. |
Tracking Path Changes
| git rm | git rm [file] | Delete a file from the working directory and stage the removal. |
| git mv | git mv [existing-path] [new-path] | Move or rename a file and stage the change. |
| git log --stat -M | git log --stat -M | Show commit logs with a summary of file movements and renames. |
Rename Branch
| branch -m | git branch -m <new_name> | Rename the currently checked-out local branch. |
| push renamed | git push origin -u <new_name> | Push the renamed branch to origin and set upstream tracking. |
| delete old remote | git push origin --delete <old_name> | Remove the old branch name from the remote repository. |
Log Tricks
| log -S (search) | git log -S'<term>' | Search commit history for changes that add or remove a specific string. |
| log -p (file) | git log -p <file_name> | Show the full diff of every commit that touched a specific file. |
| log --graph | git log --pretty=oneline --graph --decorate --all | Print a visual ASCII graph of the full branch and merge history. |
Branch Tricks
| branch -vv | git branch -vv | List branches with their upstream tracking info and last commit. |
| checkout - | git checkout - | Switch back to the previously checked-out branch. |
| branch -r | git branch -r | List only remote-tracking branches. |
| checkout file | git checkout <branch> -- <file> | Restore a single file from another branch without switching branches. |
Rewriting History
| commit --amend | git commit --amend -m "new message" | Replace the most recent commit with an updated message or content. |
Git Aliases
| alias co | git config --global alias.co checkout | Create a shorthand `git co` for the checkout command. |
| alias br | git config --global alias.br branch | Create a shorthand `git br` for the branch command. |
| alias ci | git config --global alias.ci commit | Create a shorthand `git ci` for the commit command. |
| alias st | git config --global alias.st status | Create a shorthand `git st` for the status command. |