Git Cheatsheet

Clean up local branches

You’ve been working hard and now you have a bunch of local feature branches that you don’t need anymore. Instead of running git branch -D on each one.

git checkout main

git fetch

git branch --merged | grep -vE '(\*|main|master)' | xargs git branch -D
  1. git checkout main switches the current branch to “main”. This assumes that the “main” branch already exists in the repository.
  2. git fetch downloads the latest changes from the remote repository to your local repository. This ensures that you have an up-to-date list of branches that have been merged into the “main” branch.
  3. git branch --merged lists all the branches that have been merged into the current branch.
  4. grep -vE '(\*|main|master)' filters out the current branch (*), as well as any branches named “main” or “master”. The explicit filtering of main and master is paranoid insurance … just in case the initial checkout didn’t work for some reason.
  5. xargs git branch -D passes each of the remaining branch names to the command git branch -D, which deletes them one by one. xargs is like the map method in a Unix command line.

Reset a commit from HEAD

You just committed some changes but want to undo that commit for whatever reason. Maybe you’ve committed to the wrong branch, or you forgot to add another change, etc.

git reset HEAD~1 --soft

This will destroy that git commit! But with the --soft flag the changes in that commit will be preserved for you to create a new commit (or set of commits) from.

If you want to reset the last N commits (from the current HEAD) while saving the changes from those 5 commits. Replace N with whatever number you want to go back.

git reset HEAD~N --soft

If you want to reset changes on the branch back to a specific commit from HEAD (while still keeping the changes)

git reset --soft <commit hash>

⚠️ Warning

This action re-writes the git history and should be done with care. As a rule, do not make changes to the git history on work that has already gone public. This breaks things for other devs working on that branch. Only do this if:

Edit the changes within a commit without destroying the commit

You want to add some new content or edit some of the files changed in a specific commit.

git rebase -i main OR SHA_OF_SPECIFIC_COMMIT_TO_REBASH_OFF_OF

This will open up an interactive rebase session where you will see all the commits your branch adds on top of the tip of main or a specified commit SHA.

For each commit, you will have a set of actions you can take. Choosing edit will place you into that commit where you change the state of the files within that commit. You can create new files, change modifications, delete files, etc.

Be careful with the changes, as you may create git conflicts with any following commits.

Once changes have been made:

Once complete, run git rebase --continue to continue the rebase.

Remove the changes made to a file

git status # to get the path names of the files added
git reset -- FILE_PATH_NAME # to remove the file

rm FILE_PATH_NAME # if you need to delete the file all together

Un-staging a file

You yolo’d and used git add . without checking the changes included. But thankfully you have not committed yet.

git status # get the name and path of the file
git reset -- FILE_PATH # unstage the file from staging

Show a condensed log of commits

git log --oneline

Show a log of commits with cool graphs

git log --oneline --decorate --color --graph

Compare the changes between commits

Compare the current commit and n number of commits back

git diff HEAD~n HEAD

Compare the current commit with the tip of main

git diff HEAD main

Further readings

git, cheatsheet