News for hackers. Stuff that matters.
Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by bloopernova on Tuesday February 13, 2024 @09:30AM (#39357918)
You probably already know these bits & bobs, but I wanted to share:

  [diff]
    external = difft
Use the fantastic difftastic instead of git's diff. https://difftastic.wilfred.me.uk/

  [alias]
    fza = "!git ls-files -m -o --exclude-standard | fzf -m --print0 | xargs -0 git add"
    gone = "!f() { git fetch --all --prune; git branch -vv | awk '/: gone]/{print $1}' | xargs git branch -D; }; f"
    root = rev-parse --show-toplevel
Those are the most used aliases in my gitconfig.

"git fza" shows a list of modified/new files in an fzf window, and you can select each file with tab plus arrow keys. When you hit enter, those files are fed into "git add". Needs fzf: https://github.com/junegunn/fzf

"git gone" removes local branches that don't exist on the remote.

"git root" prints out the root of the repo. You can alias it to "cd $(git root)", and zip back to the repo root from a deep directory structure. This one is less useful now for me since I started using zoxide to jump around. https://github.com/ajeetdsouza/zoxide

Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by keysersoze33 on Tuesday February 13, 2024 @11:18AM (#39359167)
Thanks for the difftastic & zoxide tips!

However, I've been using this git pager/difftool: https://github.com/dandavison/delta

While it's not structural like difft, it does produce more readable output for me (at least when scrolling fast through git log -p /scanning quickly)

Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by pdimitar on Tuesday February 13, 2024 @02:35PM (#39361797)
Yep, I tried at least 3 other differs (difft included) but delta was perfect and still is. Using it all the time.
Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by bloopernova on Tuesday February 13, 2024 @09:50AM (#39358100)
This one I'm less sure about. I haven't yet gotten it to the point where I really like using it, but I'm sharing since someone might find it useful as a starting point:

  [alias]
    brancherry = "!f() { git checkout -b $(git rev-parse --abbrev-ref HEAD)-$(git rev-parse --short \"$1\") $1; }; f"
It's intended to be used for creating a cherry-picking branch. You give it an branch name, let's say "node", and it creates a branch with that as its parent, and the short commit hash as a suffix. So running "git brancherry node" creates the branch "node-abc1234" and switches to it.

The intended workflow being you cherry pick into that branch, create a PR, which then gets merged into the parent.

Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by ulope on Tuesday February 13, 2024 @10:16AM (#39358353)
difftastic - amazing!

I've been wanting something like this for years...

Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by bloopernova on Tuesday February 13, 2024 @10:25AM (#39358441)
Be prepared to hand out the difftastic URL and install instructions a lot :) I get asked "what git setting is that?" when I do diffs while sharing my screen.
Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by js2 on Tuesday February 13, 2024 @01:35PM (#39361043)
meta-tip: you can also put your aliases that start with '!' into stand-alone shell scripts named `git-fza` (e.g.) and then call it as `git fza` which will search your PATH for `git-fza` and invoke it as if it's built-in.

I do this for some of my more complicated aliases because I generally think it's poor form to embed shell scripts into configuration languages. (Looking at you, yaml.)

Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by svieira on Tuesday February 13, 2024 @02:30PM (#39361727)
I have several scripts like that in my PATH, but Git on Mac can never find them. What am I doing wrong?
Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by js2 on Tuesday February 13, 2024 @06:00PM (#39364043)
The scripts have to be in your `PATH` and be executable from wherever you're running `git`.

Say you have a script named `git-foo`. At the shell prompt, all of these should work:

  $ which git-foo
  $ git-foo
  $ git foo
If the first or second commands fail, then `git-foo` is not in your PATH or is not executable. If those both work but the third command fails, I have no explanation. Here's the code which runs commands:

https://github.com/git/git/blob/2996f11c1d11ab68823f0939b646...

It basically prefixes your `PATH` (or a suitable default if `PATH` isn't set) with `GIT_EXEC_PATH` (defaulting to a compiled in value if not set) and then uses the normal Unix execvp machinery to run the command.

You can try:

  $ GIT_TRACE=1 git foo
But I'm not sure that will tell you anything helpful.
Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by svieira on Wednesday February 14, 2024 @02:39PM (#39374184)
Well, this is fun:

    $ which git-foo
    $ type git-foo
    git-foo is /Users/my.user/.local/bin/git-foo

    $ git-foo --help
    Help output from git-foo

    $ GIT_TRACE=1 git foo
    14:26:18.849078 git.c:749               trace: exec: git-foo
    14:26:18.849815 run-command.c:657       trace: run_command: git-foo
    git: 'foo' is not a git command. See 'git --help'.
I can't say I've ever seen `which` and `type` disagree before ...

And today-I-learned that while bash expands `~` in PATH entries other programs do not. The fix was changing my PATH from:

    PATH=~/.local/bin:other:paths
to:

    PATH=$HOME/.local/bin:other:paths
Thanks so much for the help!
Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by bloopernova on Tuesday February 13, 2024 @06:15PM (#39364210)
What version of git do you have? If it came with macOS and wasn't installed via homebrew, it's possible that it's old enough to cause problems.
Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by js2 on Tuesday February 13, 2024 @10:55PM (#39366272)
git has had this behavior for at least a decade. As well, macOS does not ship with git - it's installed as part of either the Command Line Tools package and/or Xcode and is reasonably up to date.

For many years now, macOS has included what are effectively wrappers in /usr/bin for the various development tools and that use the xcode-select mechanism to run the actual command. If neither Xcode nor the CLT package are installed, you'll get a prompt to install the CLT package.

Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by bloopernova on Tuesday February 13, 2024 @02:00PM (#39361342)
Oh nice, I didn't know that!

For git fza I override an oh-my-zsh alias "ga" to run it.