The Forever Git Cheat Sheet
Reference
June 22, 2018
Git is great at throwing wrenches in the work. For reference here is everything I’ve learned how to fix.
Table Of Contents
- Helpful Git Resources
- I want to match HEAD back to the origin
- I want to drop all unstaged edits and go back to my last commit
- I want to go back to my last commit but keep all file edits
- Delete Remote & Local Branch
- Remove File/Dir From Remote & Keep It Locally
- Update Local Repo To See All New Remote Branches
- Rename directories in GitHub (i.e. fix casing mismatch)
- Check out a Pull Request on the original repo when I have a fork on my local machine
- Keep Forked Repo Up To Date With Original Repo
- Amend The Commit Message Of The Most Recent Local Commit
- Rename Master Branch To Main
- Commit Selected Lines In A File (Patch)
- Clear git Credentials To Enter New Password (Mac)
Helpful Git Resources
I want to match HEAD back to the origin
git fetch origin |
I want to drop all unstaged edits and go back to my last commit
git checkout . |
I want to go back to my last commit but keep all file edits
git reset HEAD^ |
Delete Remote & Local Branch
# DELETE REMOTE BRANCH |
Remove File/Dir From Remote & Keep It Locally
git rm -r --cached some-file-or-directory |
Update Local Repo To See All New Remote Branches
git remote update origin --prune |
Rename directories in GitHub (i.e. fix casing mismatch)
This is a manual process if the directories are already cased properly locally. The problem is that by default git ignores case changes for filenames and directory names. Here is an answer explaining how to change that (though not recommended, read comments), and here’s a potential fix if the files/folders are not cased properly locally.
Check out a Pull Request on the original repo when I have a fork on my local machine
The GitHub PR page includes instructions for checking out a PR on the command line, but if you are running a fork locally, you need to work with the upstream branch instead of the origin branch.
First make sure the original repo has been added as an upstream source
git fetch upstream |
Keep Forked Repo Up To Date With Original Repo
First make sure the original repo has been added as an upstream source
git checkout master |
Amend The Commit Message Of The Most Recent Local Commit
Note: don’t do this for commits that have already been pushed to a remote repo, especially in a collaborative repo!
git commit --amend -m "new commit message" |
To edit the commit body in addition to the message subject, omit the -m
flag.
Rename Master Branch To Main
$ git branch -m master main |
Commit Selected Lines In A File (Patch)
The patch flag -p
or --patch
lets you pick selected lines (aka “hunks”) from a file to stage & commit.
$ git add FILENAME.ext -p |
This brings up an interactive menu which will ask about which hunks to stage. The main options are:
- y stage this hunk for the next commit
- n do not stage this hunk for the next commit
- s split the current hunk into smaller hunks
- q quit; do not stage this hunk or any of the remaining hunks
- ? print hunk help
Clear git Credentials To Enter New Password (Mac)
Now that GitHub no longer allows account passwords for authentication, you have to authenticate with expirable personal access tokens. In order to get prompted for a new password, clear the existing credentials:
- Use Spotlight to open Keychain Access
- Under Category, make sure ‘All Items’ is selected
- Type ‘git’ in the search field
- Right-click ‘github.com’ and delete it
Next time you attempt any GitHub actions from the command line, you’ll get prompted to log in. Use the personal access token as the password with your normal username.