Git bash | command line cheat sheet
Setup
Global user information settingsSet the name you will use to add changes to the repo
git config --global user.name "[firstname lastname]"
Set the email address will use to add changes to the repo
git config --global user.email "[valid-email]"
Format colors for easy review
git config --global color.ui auto
Initialization
Configuring new reposInitialize an existing directory as a repo
Copy entire remote repo to current directory
Staging
Working with your local filesystem repoShow modified files in your working directory. These files are staged for your next commit
Diff list of what files have changed, but are not staged
Display diff list of what files have been staged, but not committed
Add a file as-is to your next commit, aka stage the file
Stage all changes in [directory] for the next commit
Remove file from staging, but keep the changes
Commit your staged files as a new commit
git commit -m "[descriptive message]"
Branching and merging
Organize your changes into branches and merge them togetherList your branches. Your currently active branch will have a * next to it
Create a new branch [branch-name] at the current commit
Switch to another branch and check it out to your current working directory
Merge [branch-name]'s history into the current branch
Display all commits in the current branch's history
Inspect and compare
Display commits that are on branchA but not on branchB
Display the commits of the file, even across renames
Display the diff of what is in branchA but not branchB
git diff branchB...branchA
Display any git object in human-readable format
Tracking Changes
Removing and moving filesDelete the local file and stage the removal for commit
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
Ignoring patterns
Prevent uninentional staging or commiting of filesAdd 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 reposAdd a git URL as an alias
git remote add [alias] [url]
Fetch all the branches from the remote alias repo
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
Rewrite history
Rewrite branches, update commites, and clear historyApply any commits of current branch ahead of specified 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.
Create a new commit that undoes all the changes in [commit], then apply it to the current branch
Shows which files would be removed from working directory by clean
Removes files from working directory
Temporary commits
Temporarily store modified and tracked files in order to change branchesSave modified and staged changes and reverts to pre-change status
Display list stack-order of stashed file changes in the file
Write working files from top of stash stack
Discard the changes from the top of the stash stack