Git bash | command line cheat sheet
  • 10 Aug 2022
  • Dark
  • PDF

Git bash | command line cheat sheet

  • Dark
  • PDF

Article Summary


Global user information settings

Set the name you will use to add changes to the repo
git config --global "[firstname lastname]"
Set the email address will use to add changes to the repo
git config --global "[valid-email]"
Format colors for easy review
git config --global color.ui auto


Configuring new repos

Initialize an existing directory as a repo
git init
Copy entire remote repo to current directory
git clone [url]


Working with your local filesystem repo

Show modified files in your working directory. These files are staged for your next commit
git status
Diff list of what files have changed, but are not staged
git diff
Display diff list of what files have been staged, but not committed
git diff --staged
Add a file as-is to your next commit, aka stage the file
git add [file]
Stage all changes in [directory] for the next commit
git add [directory]
Remove file from staging, but keep the changes
git reset [file]
Commit your staged files as a new commit
git commit -m "[descriptive message]"

Branching and merging

Organize your changes into branches and merge them together

List your branches. Your currently active branch will have a * next to it
git branch
Create a new branch [branch-name] at the current commit
git branch [branch-name]
Switch to another branch and check it out to your current working directory
git checkout
Merge [branch-name]'s history into the current branch
git merge [branch-name]
Display all commits in the current branch's history
git log

Inspect and compare

Display commits that are on branchA but not on branchB
git log branchB..branchA
Display the commits of the file, even across renames
git log --follow [file]
Display the diff of what is in branchA but not branchB
git diff branchB...branchA
Display any git object in human-readable format
git show [SHA]

Tracking Changes

Removing and moving files

Delete the local file and stage the removal for commit
git rm [file]
Change or rename existing file path and stage the change for commit
git mv [existing-path] [new-path]
Display all commit logs of filepaths that have been moved
git log --stat -M

Ignoring patterns

Prevent uninentional staging or commiting of files

Add filter to .gitignore to avoid committing these files. Can use string literals or use "*" wildcard
Global ignore pattern for all local system repos
git config --global core.excludesfile [file]

Share & Update

Retrieve updates from a different repo and update local repos

Add a git URL as an alias
git remote add [alias] [url]
Fetch all the branches from the remote alias repo
git fetch [alias]
Merge a remote branch into your current branch
git merge [alias]/[branch]
Transfer your local branch commits to the remote branch repo
git push [alias] [branch]
Fetch and merge any commits from the tracking remote branch
git pull

Rewrite history

Rewrite branches, update commites, and clear history

Apply any commits of current branch ahead of specified branch
git rebase [branch]
Clear staging area, rewrite working tree from specified commit
git reset --hard [commit]
Replace the previous commit with the stange changes and last commit combined. Use with nothing staged to edit the last commit's message.
git commit --amend
Create a new commit that undoes all the changes in [commit], then apply it to the current branch
git revert [commit]
Shows which files would be removed from working directory by clean
git clean -n
Removes files from working directory
git clean -f

Temporary commits

Temporarily store modified and tracked files in order to change branches

Save modified and staged changes and reverts to pre-change status
git stash
Display list stack-order of stashed file changes in the file
git stash list
Write working files from top of stash stack
git stash pop
Discard the changes from the top of the stash stack
git stash drop