Harish Kumar

Git Cheat Sheet

Published on 25 April 2022 · git

Git Basic Commands

git init <directory> //Create empty git repo in the specific directory
git clone <repo> //Clone repo located at the <repo> defined. Can be either local or remote via HTTP or SSH
git config user.name <name> //Author name that would be used for all commits in current repo
git add <directory>/<file> //Adds the directory/file on your next commit
git commit -m "<message>" //Commits your staged content as the new commit snapshot with a message
git status //Shows modified files in the working directory
git log //Shows all commit in the current branch's history
git diff //Shows the difference of what changed but not staged

Undo the changes

git revert <commit> //Undoes all the changes made in the <commit> by creating a new commit and applies to the current branch
git reset <file> //Removes the file from the staging area, but leaves the working directory unchanged. Unstages  a file without overwriting any changes
git clean //Used to remove the unwanted files from your working directory. Many options to remove the unwanted files: -f, -n, -d etc

Rewrite, reset, rebase

git rebase <branch> //Applies any commits of current branch ahead of the specified one
git reflog //Show a log of changes to the local repository's HEAD
git reset //Reset staging area to match the most recent commit but leaves the working directory unchanged
git reset --hard //same as above except overwrites all changes in the working directory
git reset <commit> //Move the current branch tip backward to <commit>, reset the staging area to match, but leaves the working directrory alone
git reset --hard <commit> //Same as above but resets both staging area & woking directory to match. Deletes uncommited changes, and all commits after <commit>

Branch & Merge

git branch //Lists all your branches
git branch <branch-name> //Create a new branch at the current commit
git checkout //Switch to another branch and check it out into your working directotry
git merge <branch> //Merge the specified branch's history into the current one

Push & Pull

git remote add <alias> <url> //Adds a git URL with a name as the alias
git fetch <alias> //Fetches all the branches from the git remote
git merge <alias>/<branch> //Push the branch commits to the repository. Creates the named branch if it doesn't exist.
git pull //Fetch and merge any commits from the tracking remote branch

Temporary commits

git stash //Saves modified and staged changes
git stash list //List stack-order of stashed file changes
git stash drop //Discard changes from the stash stack

Inspect & compare

git log //Shows all commits in the current branch's history
git log branchB..branchA //Shows the commits on branchA that are not on branchB
git log --follow <file> //Shows the commits that changed file. Including file renames
git diff branchB...branchA //Show the diff of what's in branchA compared to branchB

Tracking path

git rm <file> //Deletes the file from project and stages the removal for commit
git mv <current-path> <new-path> //Changes the current file path and stages the move for commit
git log --stat -M //Show all commit logs with indication of paths that have been moved