The Only Git Cheat Sheet You Need

Anna Med
6 min readFeb 21, 2019

--

Basically, git tracks your files and any changes within your files for you. This is called “version control”.

Main git commands to know:

git init
  • this initializes a repository (starts tracking your files in the folder you are in when you type git init ***IMPORTANT: make sure you are in the root/main folder of your app/project when you type git init*** DON’T type git init in your main folder outside of your app/project, it will track all files on your computer then and will slow everything down. (Look to the left of where you are typing your commands (on the same line as your cursor) to see which folder you are in, or type “pwd” which shows you which folder/file you are currently in)
git add .
  • with the “.” this adds ALL of your files to the ‘staging area’. Once added to the staging area, that file/folder is now being tracked by git. If you only want to add a specific file use this:
git add filename
  • Replace filename with the name of the file you want to add to the ‘staging area’
git commit -m "type a detailed message of changes you have made/why you are committing here"
git diff --cached
  • Review changes you’ve staged for commit
git status
  • You can never use this too much! This command will show you what files that are being tracked have changes, any untracked files, any staged files ready to be committed, and whether you are up to date with your remote branch. This is the time to see if you are on track or need to make any changes to get back on track.
git push
  • This command pushes all of the changes to your files that you have added and committed to your branch, and also to your remote project branch if you have one set up
git branch -a
  • List all branches for current project, on your local machine as well as remotes
git checkout -b <branch_name>
  • Create a new branch on your local machine.
  • Replace <branch_name> including arrows with whatever you’d like to name your branch. In naming, be specific and expressive of what that branch will be used for to help your future self and others.
git branch -d <branch_name>
  • Delete a branch on your local machine.
  • Git Etiquette → delete branches you are finished using to keep your workspace neat and clean :)
git fetch --prune
  • This updates all of your remotes on your local machine. Ever wonder why you delete branches on GitHub but they still show up in vs code? This updates it for you :)
  • Connects to remote, fetches all branches, and removes any deleted remote branches from showing up in vs code
git push origin --delete <branch_name>
  • Delete a remote branch
git stash
  • Saves current changes made without having to commit and goes back to previous “head” state before all of your changes
  • For example if you’re on a branch working on a new feature, then need to quickly switch to dev or some other branch to fix a bug or something
git stash list
  • List all your stashes & their respective {index_number}
git stash apply 
# OR
git stash apply index_number
# OR
git stash pop
  • Bring back the changes you stashed
  • Apply vs Pop = apply keeps the stash in your list, pop applies & removes it from list
  • Index Number is optional, useful when you have multiple stashes in your stash list
git log
  • View recent commits
  • Type q and hit enter/return to exit
git log --oneline --decorate
  • — oneline shows commit history summarized/ abridged version
  • — decorate includes more details such as tags
  • — stat shows # of file insertions & deletions
git log -Ssearching_for_name -p
  • This is amazing! Search through all commits to find something specific, like a class, function, or method name, etc. (Actually checks the diff, not just commit message!)
  • Can also: -S’search for words’
  • Or -G for regex instead of -S
  • — all to search all branches (two dashes)
Photo by Thought Catalog on Unsplash

Github Fun →

Okay, enough basics. Let’s do some cool stuff with GitHub…

Create remote repo in 3 lines:

(These steps are instead of manually creating the repo in GitHub GUI)

curl -u ‘USER’ https://api.github.com/user/repos -d ‘{“name”:”REPO”}’git remote add origin git@github.com:USER/REPO.gitgit push -u origin master
  1. Remember replace USER with your username and REPO with your repository/application name! This creates a repo in GitHub

2. This sets the repo you just created in GitHub as the remote repo to your current local repo

3. Push upstream to your remote repo

If you’re having issues adding a remote, try this:

git remote add origin https://USERNAME:PASSWORD@github.com/username/reponame.git
  • If your password has @ in it, replace it with: %40

Setup Configurations:

git config --global user.email enter_email
git config --global user.name enter_username
git config credential.username enter_username

Check your git configuration:

git config --list

HOW TO UNDO THINGS:

Warning: These commands can be dangerous, use with knowledge & caution

How To Delete A File From Git and Git History (Including Github):

git filter-branch --force --index-filter \
‘git rm --cached --ignore-unmatch PATH-TO-YOUR-FILE-TO-ERASE’ \
--prune-empty --tag-name-filter cat -- --all
  • This removes file from git tracking & git history. ^ Copy Pasta these 3 lines

*Add your file to gitignore (if you want to keep it locally on your project but not tracked by git)

git push origin --force --all
  • Force pushes your changes to GitHub 💯
  • PS Anyone else working on this project needs to rebase branches created from this one

More dangerous things you can do… or use to UNDO:

git reset HEAD~
  • Undo most recent commit
git rebase --onto commit-id^ commit-id
  • Undo a specific commit (removes commit history & all changes from that commit) (use git log to find the commit id)
Photo by Simon Migaj on Unsplash

Learn More

Search around about git, git workflows → branching models & release versioning to learn more about git and git usage.

Personally, I like to use a simple, logical workflow of:

git checkout -b name_of_feature_or_work_or_fix
  • from master branch (latest up to date branch with no broken code) create new branch with obvious name
  • do some work, add + commit + push whenever I am in a good place that works and hasn’t broken anything
  • once my work is done, working and ready I then:
git pull origin master
  • pull in the latest changes and run everything together to again test my code to ensure it is still working even with latest updates on master
  • may need to fix merge conflicts, which I do my best to minimize by keeping code and file structure as modular as possible and being aware of who else may be working on the same thing
  • working? now I am ready to create a pull request, in GitHub GUI
  • delete your branch after pull request is approved and work is finished

Of course, your particular workflow will depend on your company, your team, your project layout, etc., so this may be a bit simplified depending on your situation 🤷‍♀

Not Git But…

diff -rq directory1 directory2
  • Compare 2 folders to each other (file structure and file differences) (Linux & Mac Terminal)
Photo by Jan Tinneberg on Unsplash

P.S. My goal with this cheat sheet is to provide one place to quickly glance at various commands when a particular word or syntax slips your mind. It’s not necessarily meant to be a comprehensive overview nor deep dive into git. It’s also not meant to be full of jargon, as my aim is to communicate clearly, effectively, and concisely, to any developer regardless of level. I started putting this list together about a year ago when I realized it would be very helpful and useful to me to have something like this. May it assist you too! Thank you for visiting 😻

--

--