Git Gud, a little Git cheatsheet

Patrick Liang
3 min readApr 2, 2022

Note: This article assumes that the reader knows what Git is.

A brief refresher of Git.

For the longest time, whenever an app is being made, developed, and completed by me, I have always push to main using Git. Now, if you were working on a solo project, this would be convenient to do to track your own changes, after all, why need so many branches of code when you only really need one.

However, this workflow changes when you work with a partner or with a team. Imagine if everybody pushed their code to the main branch, that would be catastrophic. So here I will provide basic Git commands I use:

//This command is used to initiate or start a new git repository, //the starting point of any project, make sure you are in the right //directory in the terminal
$ git init

The following commands are usually run whenever you choose to push to your repository, by default it pushes to the main branch.

//To stage all of your files you want to commit
$ git add .
//To commit all file changes
$ git commit -m "your message here"
//To push to your branch
$ git push

However, the best practice to use Git if working in a team or any professional environment is when everytime you work on a feature, bug fix, or any changes to the codebase of any app you should create and work on a different branch that is specifically for that feature, bug fix, or any other changes. Below is the procedure on how I usually use Git.

//Always use this command if you are unsure. This will show which branch you are in as well as displaying the state of the working directory and the staging area.
$ git status
NOTE: PLEASE RUN THE COMMANDS THE BELOW IN ORDER FROM TOP TO BOTTOM OR ELSE YOU WILL RUN INTO ISSUES.//Always make sure you are on the main branch, can run git status to //check
$ git checkout main
//Before creating a new branch off of main, pull the latest changes //from the origin
$ git pull origin main
//if already in main branch
$ git pull
//Create a new branch off of master, add the -b flag (this command will also switch you to the newly created branch)
$ git checkout -b <branch-name>
After you’ve made some changes and want to push it to the codebase (note: you should be in your newly created branch...ALWAYS RUN GIT STATUS IF UNSURE)://To stage all of your files you want to commit
$ git add .
//To commit all file changes
$ git commit -m "your message here"
//To push to your branch (the terminal will give you a command to copypasta if you use git push so you can use git push)
$ git push origin <branch-name>

So if you do everything right, the Github repository branches should look something like this:

Other useful Git commands that I use:

//Shows a log of all commits and changes made to a repository
$ git log
//Graphic overview of all commits and changes
$ git log --graph
//Takes your uncommitted changes (both staged and unstaged), saves //them away for later use, and then reverts them from your working //copy. (Very useful when you screw up and need to undo everything //and/or if you need those changes back) $ git stash
//Or use this command to undo
$ git revert HEAD
//Reverts to a specific commit in the past. Makes your local code //and local history be just like it was at that commit
$ git reset --hard <copy and past the commit ID found in Github>
//Reverts to last commit
$ git reset --hard
//Displays all branches
$ git branch
//Delete a local branch
$ git branch -d <branch_name>
//Comparison of two different branches
$ git diff <branch_name_1>..<branch_name_2>
//FORBIDDEN DO NOT USE, VERY DANGEROUS. Will push your code to the repository regardless of anything.
$ git push --force

--

--