Skip to content

Instantly share code, notes, and snippets.

@akochepasov
Last active February 7, 2024 20:48
Show Gist options
  • Save akochepasov/a06de506fe9c3de3eab1934be2e4adf3 to your computer and use it in GitHub Desktop.
Save akochepasov/a06de506fe9c3de3eab1934be2e4adf3 to your computer and use it in GitHub Desktop.

Fetch only a known branch

git fetch --depth=5 origin feature/BSD-745-db-simulation

Switch-rename existing branch

git switch feature/BSD-745-db-simulation
git switch -c BSD-745 --track origin/feature/BSD-745-db-simulation

Branch from current

git checkout -b feature/BSD-713

Push the branch to remote with a long name

git push -u origin HEAD:users/username/BSD-241-long-full-feature-name

Pull only the current branch

git pull origin $(git branch --show-current)

Branch from stash

git stash branch feature/BSD-133
git stash branch feature/BSD-133 stash@{2}

Graphic log

git log --graph --oneline

Show branch names

git rev-parse --abbrev-ref --symbolic-full-name @{u} # Remote
git branch --show-current # Local

Squash last 3 commits

git reset --soft HEAD~3 && git commit

Do big pull faster

git pull --depth=5 --update-shallow

Shallow full repository

git rev-parse HEAD~20 > .git/shallow # Shallow to last 20 commits
git fsck --unreachable # Will show you the list of what will be deleted
git gc --prune=now # Will actually delete your data

Prune full repository

git fsck --unreachable # Prepare the list of what will be deleted
git prune --progress # Delete all the objects w/o references
git gc --prune=now --aggressive # Aggressively collect garbage; may take a lot of time

Prune history

git fetch --depth=1 # Prune old commits. This makes the old commits and their objects unreachable.
git reflog expire --expire-unreachable=now --all # Expire all old commits and their objects
git gc --prune=now --aggressive # Remove objects older than two weeks

Decrease git size

git gc
git repack -a -d --depth=250 --window=250 # kills in-pack garbage
git prune # kills loose garbage

Clone last 10 commits

git clone --progress -v --depth 10 git@github.com:akochepasov/repo123.git

After cloning get access to all branches (--depth implies single branch, wtf?)

git remote set-branches origin '*'
git remote set-branches origin master # To get back

Try this pruning scenario later

git remote prune origin
git repack
git prune-packed
git reflog expire --expire=1.month.ago
git gc --aggressive

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