Useful Git Commands
samconibear
Posted on May 25, 2022
Git is a fantastic technology that not only provides historical tracking of source code throughout development, but also enables multiple developers to work on the same project without stepping on each others feet.
I wanted to create a sort of cheat sheet for git commands that I find myself using often. I will continue to update this post as I discover other handy git commands.
Starter Git Commands:
1. Create and checkout a new branch
git checkout -b <branch>
2. Discard changes to a file.
git checkout -- <file>
3. Rename a local branch
git branch -m <oldName> <newName>
4. Merge a specified branch into the currently checked out branch.
git merge <branch>
More Git Commands:
1. Stage all changes
git add -A
stage all new, modified or removed files to the working tree. Note this is different to git add .
which only stages new and existing files.
2. Review all changes
git diff HEAD
The same as git diff
but reviews all changes rather than just those unstaged.
3. Remove a file from the repo but keep it in your project directory
git rm --cached <filename>
Very useful when you have files you don't want tracked that you forgot to specify in .gitignore
that have previously committed. After adding them to .gitignore run this command to ensure they are removed from the repo without deleting them from your local project directory.
4. Reset all local files to the last commit
git reset --hard
DANGER - exercise caution when using this command as it has the potential to throw away hours of work.
I often find I have uncommitted changes in a branch I've been messing around in (whitespace, print statements) git doesn't let you checkout another branch with uncommitted changes so this is a quick way to reset your project directory to the HEAD.
5. Undo the last commit
git reset --hard HEAD^
DANGER - Can also cause unintentional loss of work.
We've all made commits we immediately regret... Use this command to destroy the changes from the last commit.
6. Modify the previous commit
git commit --amend -m <newMsg>
Adding the -m
option allows you modify the previous commit message. You can also stage new changes to the commit and the previous commit will inherit them.
7. Remove all branches that no longer exist on remote
git branch --merged | egrep -v "(^\*|master|main|<branchToSave>)" | xargs git branch -d
DANGER - this will delete any newly created branches that have not yet been pushed to the remote repo. be sure to specify special cases as in <branchToSave>
.
This can be used to clean up your local repository if you have lots of old branches that have since been merged. If you want to learn how this works see this great stack overflow post.
8. Cache git credentials for a set amount of time
git config credential.helper 'cache --timeout 28800' # 8hrs
This saves you entering your git user/password every time you want to push/pull
The --global
flag can be added to do this across all repositories.
9. Remove a committed file from a branch / PR
git checkout <main_branch> -- <path_to_file>
Posted on May 25, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.