- Clean up local branches
- Reset a commit from HEAD
- Edit the changes within a commit without destroying the commit
- Un-staging a file
- Show a condensed log of commits
- Show a log of commits with cool graphs
- Compare the changes between commits
- Further readings
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
git checkout mainswitches the current branch to “main”. This assumes that the “main” branch already exists in the repository.
git fetchdownloads 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.
git branch --mergedlists all the branches that have been merged into the current branch.
grep -vE '(\*|main|master)'filters out the current branch (*), as well as any branches named “main” or “master”. The explicit filtering of
masteris paranoid insurance … just in case the initial checkout didn’t work for some reason.
xargs git branch -Dpasses each of the remaining branch names to the command
git branch -D, which deletes them one by one.
xargsis like the
mapmethod 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>
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:
- you haven’t pushed the changes to the remote repository
- you know you are the only one working on that branch
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:
- verify the changes made with
git diff, or using the vscode source control UI run
git add . OR specify-the-files-to-addto add the changes, and then continue the rebase
git add . OR specify-the-files-to-add(be careful with
git add .)
git commit --amend
- since we did not destroy the commit with
git reset HEAD~, the commit still exists but now we want to amend all these new changes to that commit
- If we did reset the commit (destroy it) DO NOT RUN
git commit --amendas this will amend all your changes to the last previous commit which is NOT the old commit anymore since we blew that up with the reset
- since we did not destroy the commit with
- But if you want all the new changes in their own atomic commit then do the normal flow of
git add .(use carefully)
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