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.
Create a new feature branch while you’re on the master branch and switch to that branch.
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.
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
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
Then the master branch receives some new changes and you want these changes from master to exist on your feature branch. Do the following:
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
OR checkout one of the branches you want to compare and name the other
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.
Rename Local Branch
If you want to rename a branch while pointed to any branch, do:
If you want to rename the current branch, you can do:
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
We are now free to swap to any branch
We can then return to the branch with stashed changes and bring the stash back in
Remove File From git Tracking
Just a file
Folder and contents
Undo Commit That Has Not Been Pushed to Remote
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
Delete a local branch
Delete a remote tracking branch
Delete remote tracking branches in bulk
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.