Top
Best
New

Posted by spencerldixon 15 hours ago

I found a useful Git one liner buried in leaked CIA developer docs(spencer.wtf)
621 points | 216 commentspage 5
stabbles 14 hours ago|
Missed opportunity to call it `git ciao`
devy 12 hours ago||
I needed that exact functionality and Claude code and ChatGPT consistently showing this same exact combo CLI receipt with the simple prompt "how to do use CLI to remove merged branch locally."
SoftTalker 12 hours ago|
It's hardly a profound insight. If you're fluent at the command line, xargs enables all sorts of conveniences.
password4321 14 hours ago||
I don't delete branches, I just work with the top several most recently modified.
the_real_cher 13 hours ago|
How to list those? Is there a flag for git branch to sort by recently modified?

(not on my computer right now to check)

rpozarickij 13 hours ago|||
This isn't exactly the same but I've been using git-recent [0] (with `gr` alias) for many years. It sorts branches based on checkout order (which is what I usually need when switching between branches) and allows to easily choose a branch to checkout to.

[0] https://github.com/paulirish/git-recent

embedding-shape 13 hours ago|||
I do `gb` (probably "git branch" when I set that up) which apparently is an alias to `git for-each-ref --sort=-committerdate refs/heads/ --format='%(refname:short)' | tac`, displays a list with the latest changed branch at the bottom. Remove the `| tac` for the reverse order.
password4321 7 hours ago||
Yes, I run something like this (PowerShell borks out unless there are double quotes inside the single quotes) in a short script when I need to review available branches:

  git fetch --all --prune --quiet
  git log origin/main --date=format:%m%d%H%M --pretty=format:'%C(yellow)%ad-%h%C(auto)%d %s (%cn)' -n1 
  git log --tags --date=format:%m%d%H%M --pretty=format:'%C(yellow)%ad-%h%C(auto)%d %s (%cn)' -n1
  echo ''
  git for-each-ref --count=10 --sort=-committerdate refs/heads/ --format='%(color:yellow)%(committerdate:format:%m%d%H%M)-%(objectname:short) (%(color:green)%(refname:short)%(color:yellow)) %(color:white)%(contents:subject) (%(authorname))'
I include a [month][day][hour][minute]-[git hash] prefix as enough info to see when branches were last updated and grab them when I make a mistake creating the branch from the wrong parent or want to cherry-pick.
microflash 11 hours ago||
I’ve been using something similar for years with Nushell.

git branch | lines | where ($it !~ '^*') | each {|br| git branch -D ($br | str trim)} | str trim

bobabob 12 hours ago||
Using grep and xargs is worth a whole blog post now? hmmmmm
allthetime 9 hours ago|
bash knowledge is probably a quickly dying thing.

hey, gemini, how do I...

trashymctrash 14 hours ago||
If you squash your PR before merging, then this alternative worked really well for me:

  git fetch --prune && git branch -vv | awk '/: gone]/{print $1}' | xargs git branch -D
lkbm 12 hours ago|
Almost identical to mine, but you've got smarter awk use: `git prune origin && git branch -vv | grep 'origin/.*: gone]' | awk '{print $1}' | xargs git branch -D`

I think I probably copied this from Stack Overflow close to a decade ago. Seems like a lot of people have very similar variations.

ihsoy 14 hours ago||
Dont most git instances, like github, delete branch after a PR was merged, by default?

I am not sure under what usecases, you will end up with a lot of stale branches. And git fetch -pa should fix it locally

nightpool 14 hours ago||
`--prune` will delete your local copies of the origin's branches (e.g. `origin/whatever`). But it won't delete your local branches (e.g. `whatever` itself). So PRs that you've worked on or checked out locally will never get deleted.
plqbfbv 14 hours ago|||
In Github it needs to be explicitly configured (Settings > General > Delete head branches after merging), Gitlab is the same.

A lot of my developer colleagues don't know how git works, so they have no idea that "I merged the PR" != "I deleted the feature branch". I once had to cleanup a couple repositories that had hundreds of branches spanning back 5+ years.

Nowadays I enforce it as the default project setting.

embedding-shape 13 hours ago||
> Dont most git instances, like github, delete branch after a PR was merged, by default?

By default, I don't think so. And even if the branch is deleted, objects can still be there. I think GitLab has a "Clean stale objects" thing you can trigger, I don't seem to recall ever seeing any "Git Maintenance" UI actions on GitHub so not sure how it works there.

mrbonner 13 hours ago||
> Since most projects now use main instead of master…

I see that even the CIA, a federal government office, has not fully used DEI approved, inclusive language yet :-)

jen20 13 hours ago|
The leaked material from which this came was described as being from 2017, which makes that the latest this could have been written - GitHub only changed the default for new repos in October of 2020, and there had only been consensus building around the switch for a couple of years beforehand.
micw 14 hours ago|
I recently let copilot create a document with a few helpful git commands and that particular one was the one it came with as solution for exactly this case.
More comments...