Skip to content

Instantly share code, notes, and snippets.

@brainbrian
Last active February 28, 2023 00:10
Show Gist options
  • Save brainbrian/d13cdf4f92cc97108d00 to your computer and use it in GitHub Desktop.
Save brainbrian/d13cdf4f92cc97108d00 to your computer and use it in GitHub Desktop.
Git Cheat Sheet

Git Cheat Sheet

Handy Git Commands

Configuration

git config --global push.default current

same as running

git push origin head

Rebasing

Rebasing is a nice friend.

git pull --rebase

Can also set your default to always rebase (I don't)

git config --global pull.rebase true

Interactive rebasing on a specific amount of commits (X) in current branch

git rebase -i HEAD~X

Logging

Clean 1 line commit log

git log --pretty=oneline

Display log without merges displayed

git log --no-merges

Deleting branches

Delete remote

git push -d <remote_name> <branch_name>

Delete local

git branch -d <branch_name>

Delete local w/ force delete if branch has not been merged

git branch -D <branch_name>

Resetting/reverting a branch

Checkout branch

git checkout <branch_name>

Reset branch to specific commit and remove untracked files

git reset --hard <commit-hash>

Update the remote to reflect the local reset

git push -f origin <branch_name>

Maintenance

Clean your repo of untracked files and directories

git clean -f -d

Delete local branches that have been merged in the remote

git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d

Delete all local branches that are not master. Use -D for a force deletion.

git branch | grep -v "master" | xargs git branch -d

Tagging

git tag -a v1.4.0 -m 'my version 1.4.0'

Even better

git tag -a v1.4.0 (launches editor for annotated message)

Tagging a specific commit

git tag -a v1.4.0 9fceb02

Pushing a specific tag to a remote

git push origin v1.4.0

Pushing all tags to a remote

git push origin --tags

Show all tags with messages

git tag -n

Show details of tag

git show v1.4.0

Stashing

Stash modified files

git stash

List stashes

git stash list

Apply and remove last stash

git stash pop

Apply last stash

git stash apply

Apply a specific stash

git stash apply stash@{2}

Remove/clear all stashes

git stash clear

Show contents of a stash

git stash show #

Show contents of file in a specific stash

git show stash@{#}:<relative path to file>

Rewriting History

Deleting the last commit (XXXXXXXX = ID of last commit)

git push origin +XXXXXXXX^:<branch_name>
git reset HEAD^ --hard
git push origin -f

Removing a specific commit (XXXXXXXX = ID of commit to delete)

git rebase -i XXXXXXXX^

In VIM press dd to delete the commit line. Then :wq to write and quit.

git push <remote_name> <branch_name> -f

Miscellaneous

Ignore changes to a specific file, both local and upstream

git update-index --skip-worktree file-to-ignore.txt

Until you decide to allow them again with

git update-index --no-skip-worktree file-to-ignore.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment