Skip to content

Instantly share code, notes, and snippets.

@greggman
Last active April 9, 2024 02:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greggman/3211b96213495cc7a1be958b49f87b34 to your computer and use it in GitHub Desktop.
Save greggman/3211b96213495cc7a1be958b49f87b34 to your computer and use it in GitHub Desktop.
Git Notes

Git Notes

stash stuff

A stash is represented as a commit whose tree records the state of the working directory, and its first parent is the commit at HEAD when the stash was created.

So

git diff stash@{0}^1 stash@{0} -- <filename>

compares a stash to the state when it was stashed

show diff with working directory

git stash show -p

get a file from stash

git checkout stash@{0} -- <filename>

remove file from commit

git reset HEAD^ -- path/to/file
git commit --amend --no-edit

delele all remote tracking branches that are no longer on remote

git remote prune origin

note: All this does is remove local pointers. In other words, if you type git fetch origin git will download the refs for all the branches on "origin". The command above removes any refs that no longer exist on origin

cherry-pick a range

git cherry-pick A~1..B

The ~1 is required because A is not included.

delete all local branches that have been merged into main

git checkout -q main && git for-each-ref refs/heads/ "--format=%(refname:short)" | while read branch; do mergeBase=$(git merge-base main $branch) && [[ $(git cherry main $(git commit-tree $(git rev-parse "$branch^{tree}") -p $mergeBase -m _)) == "-"* ]] && git branch -D $branch; done

list which branches would be deleted

git checkout -q main && git for-each-ref refs/heads/ "--format=%(refname:short)" | while read branch; do mergeBase=$(git merge-base main $branch) && [[ $(git cherry main $(git commit-tree $(git rev-parse "$branch^{tree}") -p $mergeBase -m _)) == "-"* ]] && echo "$branch is merged into main and can be deleted"; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment