Git & GitHub Cheat Sheet

10/27/2023

The Entire Git Workflow. Creating a Custom Branch and Merging Back Into Master

Be sure you are currently on the master branch and your master branch is up to date.

bash
# tells you your current branch
git status    
# updates your master branch to the most recent version
git pull      

Create a new feature branch while you’re on the master branch and switch to that branch.

bash
git branch feature-name   
git checkout feature-name
# `git checkout -b feature-name` does the above in one line

Make the changes your feature needs. When you reach a good stopping point, commit locally then push to your remote branch. Repeat this step as many times as needed until you finish your feature.

bash
git add -A
git commit -m "Commit message"
# `git push --set-upstream origin feature-name` on first push
git push      

When your feature is finally finished, Create a pull request to merge your branch into master.

  • Go to the repo on GitHub
  • Click on the Pull requests tab
  • Click the New pull request button
  • Make “base” the master branch. Make “compare” the feature-name branch
  • Click the Create pull request button
  • Make a relevant title and comment for your pull request
  • Add reviewers from right-sidebar as needed
  • Click Create pull request
  • After review, click the merge pull request button to merge into master
  • You can now delete this branch freely.

Delete the newly merged feature-name branch

If you didn’t in the step above, go to GitHub and click the Delete Branch button for the feature-name branch you just merged

Go back to your local project directory, checkout master, pull new merge changes, and delete the old branch locally

bash
# we can't delete the old branch if we're still on it
git checkout master                         
# pull the updated master branch that now has feature-name's work
git pull                                    
# delete local branch and remote branch
git branch --delete feature-name            
git branch --delete -r origin/feature-name 

Catching a Feature Branch Back up With Master by Rebasing

Check out this article on merging vs rebasing for an in depth overview on strategies for updating your feature branch and what not to do.

In a situation where you’ve created a feature branch off of the master branch with the following

bash
# create the new branch off of the master branch
git checkout -b feature-name    
# If you made some changes
git add -A
git commit -m "Commit message"
# `git push --set-upstream origin feature-name` on first push
git push      

Then the master branch receives some new changes and you want these changes from master to exist on your feature branch. Do the following:

bash
# checkout master and pull down latest changes
git checkout master
git pull
# switch back to the feature branch
git checkout feature-name   
# incorporate new commits from master into the feature branch
git rebase master           

Comparing Differences Between Two Branches

If there are differences between your branches, the differences will be logged to the console. No differences between branches will produce no output to the console.

Explicitly name the two branches to compare

bash
git diff master feature-name

OR checkout one of the branches you want to compare and name the other

bash
git checkout feature-name
git diff master

Switching to a New Branch After Accidentally Making Changes on the Wrong Branch

I use this most often when I’ve accidentally started making changes on the master branch before checking out a new feature branch. This lets me preserve the state of the master branch before the changes, while moving the changes to their own branch.

bash
# starting from master branch (or a branch with uncommited changes)
git switch -c feature-name

Rename Local Branch

If you want to rename a branch while pointed to any branch, do:

bash
git branch -m oldname newname

If you want to rename the current branch, you can do:

bash
git branch -m newname

Stashing Branch Changes

I use this when I have work in progress on a branch and I’d like to save that work, but I need to switch branches and I am not ready to commit that work.

Stash Changes

bash
# on a branch with uncommitted changes
git stash save "A descriptive name of your stash"
# view your list of stashes. I like to keep just a single stash for simplicity
git stash list

We are now free to swap to any branch

bash
git checkout a-different-branch

We can then return to the branch with stashed changes and bring the stash back in

bash
git checkout original-branch
# brings the stashed changes at the top of the stash list back in AND removes that stash from the list
git stash pop

Remove File From git Tracking

Just a file

bash
git rm --cached my-file.txt

Folder and contents

bash
git rm -r --cached my-folder

Undo Commit That Has Not Been Pushed to Remote

bash
# preserves file changes and undoes the commit!
git reset HEAD~

Cleanup Deleted Branches

I try to do this every time I close a pull request to keep my list of branches clean.

List all merged branches to see what can be deleted

bash
# List merged branches
git branch --merged

Delete a local branch

bash
git branch -d my-branch
# capital D if you want to delete an unmerged branch
git branch -D my-branch

Delete a remote tracking branch

bash
# typically you won't need to do this and can instead use the prune command below
git branch -d -r origin/my-branch

Delete remote tracking branches in bulk

bash
# assumes you delete your remote branches after completing pull requests on GitHub
git remote prune origin

Completely Remove File From Git History

Do this in a scenario where you want all git tracking history of the file to be permanently deleted.

I did this on a project where I included my phone number on a contact page and decided I wanted to remove it from both my website and my git history. This command fully deleted my contact file. If you make a backup of the file you delete beforehand, you can paste it back into your project, edit it to remove the data you wanted gone, then add and commit it normally. Note: anyone who cloned your project prior to these commands will still have access to the old files, so this is by no means a security fix for something like a publicly leaked password.

This is a copy paste of this Stack Overflow answer. I don’t exactly understand what it’s doing but it does work.

bash
# remove the file from history
git filter-branch --force --index-filter "git rm --cached --ignore-unmatch PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA" --prune-empty --tag-name-filter cat -- --all
# test push?
git push --force --verbose --dry-run
# do it for real this time
git push --force