Skip to content

Instantly share code, notes, and snippets.

@jonathan-beebe
Last active January 21, 2024 18:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathan-beebe/412fc61da10ad988a545e99884f34293 to your computer and use it in GitHub Desktop.
Save jonathan-beebe/412fc61da10ad988a545e99884f34293 to your computer and use it in GitHub Desktop.
git snippets

Cleanup

Run this command periodically to clean loose objects and compress stuff.

git gc --aggressive

General

List all local branches sorted by last commit date, oldest last

for k in `git branch|perl -pe s/^..//`;do echo -e `git show --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $k|head -n 1`\\t$k;done|sort -r

List all local branches sorted by last commit date, oldest first

for k in `git branch|sed s/^..//`;do echo -e `git log -1 --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" "$k" --`\\t"$k";done|sort

List all branches that are fully merged

git branch -a --merged

Sync local git branches with remote

git remote prune origin

Delete local branches already merged into master (source)

git branch --merged master | grep -v "\* master" | xargs -n 1 git branch -d

Delete remote branch

git push origin --delete <branch_name>

Tagging

Lightweight Tag

git tag v0.1.19

Pushing tags

git push origin --tags

Deleting tags

git tag -d v0.1.19
git push origin :refs/tags/v0.1.19

Resetting

Reset to the previously fetched HEAD of the corresponding upstream branch

git reset --hard @{upstream}

Or the shorthand git reset --hard @{u}.

git fix

I had accidentally committed and pushed changes to a branch. I wanted to undo that commit and return the branch to the previous commit, both locally and on git, while applying the changes to a new branch.


// Undo the commit
git reset --soft HEAD~1

// Unstage the added changes
git reset HEAD --

// Force-push the branch which will return it to the previous commit
// This effectively returns the branch to the previous commit
git push origin {{branch_name}} -f

// Now make a new branch and commit like normal
git checkout -b {{new_branch_name}}
git add -A
git commit -m "commit message"

Cherry Pick

Given this tree:

* 49c57f4 (origin/fix/crash, fix/crash)
| * 561be79 (origin/update/settings-urls, update/settings-urls)
| * …
| * 1207637
|/
* ee50e14 (HEAD, tag: v1.3.0.47, origin/BETA, update/settings-urls-beta, BETA)
* …

Let’s say you want to move the update/settings-urls onto history of the BETA branch.

  1. git checkout -b update/settings-urls-beta
  2. git cherry-pick 561be79
  3. git push origin update/settings-urls-beta -u

Now the git history looks like this:

* 9342ea0 (HEAD, origin/update/settings-urls-beta, update/settings-urls-beta)
| * 49c57f4 (origin/fix/crash, fix/crash)
|/
| * 561be79 (origin/update/settings-urls, update/settings-urls)
| * …
| * 1207637
|/
* ee50e14 (tag: v1.3.0.47, origin/BETA, BETA)
* …

Then you can delete the old branch.

git branch -D update/settings-urls
# delete on github, then
git remote prune origin
* 9342ea0 (HEAD, origin/update/settings-urls-beta, update/settings-urls-beta)
| * 49c57f4 (origin/fix/crash, fix/crash)
|/
| * …
| * 1207637
|/
* ee50e14 (tag: v1.3.0.47, origin/BETA, BETA) Bump build to 47
* …

Stashing

Stash a single file:

git stash -- filename.ext

Save a stash:

git stash save "stash name"

List stashes

git stash list

> stash@{0}: stash name

To apply a stash and remove it using stash@{n}

git stash pop stash@{0}

Diff

View a diff without a pager:

git --no-pager diff

View a diff between two branches:

git --no-pager diff older..newer

# example, using branch names:
git --no-pager diff main..HEAD

# example using commit hashes:
git --no-pager diff 9972d366..4e207bda

Logging

List all changes between two branches, commits, etc.

git log --pretty=format:"%h %s" 5d20289...DEV

where you can place any hash, tag, or branch name on either side of the ...

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