News for hackers. Stuff that matters.
Re:Git-absorb: Git commit –fixup, but automatic by AndrewHampton on Wednesday September 25, 2024 @08:50PM (#41653389)
FWIW, I've been using this alias for the past couple years for fixup commits, and I've been happy with it:

> gfx='git commit --fixup $(git log $(git merge-base main HEAD)..HEAD --oneline| fzf| cut -d" " -f1)'

It shows you the commits on the current branch and lets you select one via fzf. It then creates the fixup commit based on the commit you selected.

Re:Git-absorb: Git commit –fixup, but automatic by nickspain on Wednesday September 25, 2024 @08:55PM (#41653419)
Nice! I use git revise[^1] a lot which does a similar thing but without the fixup commit. I I’ll try using fzf to make it interactive though. Thanks!

[^1]: https://github.com/mystor/git-revise

Re:Git-absorb: Git commit –fixup, but automatic by kccqzy on Wednesday September 25, 2024 @09:23PM (#41653575)
If it only lets you select one, that's strictly less powerful. What if I want some parts of it into one commit and another parts into another? The `hg absorb` works for this case.
Re:Git-absorb: Git commit –fixup, but automatic by k3vinw on Wednesday September 25, 2024 @11:02PM (#41654117)
Might be able to use the multimode flag in the fzf command above and it should let you select more than one using Tab and Shift+Tab.
Re:Git-absorb: Git commit –fixup, but automatic by kadoban on Wednesday September 25, 2024 @10:41PM (#41654003)
That sounds like what `git add -p` is for, stage part of the current changes.
Re:Git-absorb: Git commit –fixup, but automatic by kccqzy on Thursday September 26, 2024 @11:00AM (#41659177)
That still requires you to manually select hunks. The point of `hg absorb` is to automatically select hunks even if these hunks are to be squashed into different commits.
Re:Git-absorb: Git commit –fixup, but automatic by account42 on Monday September 30, 2024 @05:27AM (#41695192)
"Automatic" sound like it would fail about as often as it would help.
Re:Git-absorb: Git commit –fixup, but automatic by atq2119 on Thursday September 26, 2024 @04:56AM (#41656106)
Then you use `git gui`, which is part of the git distribution itself, or `tig` if TUIs are your thing. I have a key binding for `git commit --squash=%(commit)` in my tig config, so I can interactively select lines or hunks to stage and then the target commit for the squash.
Re:Git-absorb: Git commit –fixup, but automatic by AndrewHampton on Thursday September 26, 2024 @07:30AM (#41657120)
Yeah, it's definitely less powerful that what absorb is doing. I wasn't trying to argue that it was equivalent. I just wanted to share a bash one-liner that I've had success with in case others find it helpful.

> What if I want some parts of it into one commit and another parts into another?

Looks like absorb will automatically break out every hunk into a separate fixup commit. My one-liner will create 1 fixup commit for everything that's staged. That's typically what I need, but on the occasions it's not, I use `git add -p`, as kadoban mentioned, to stage exactly what I want for each commit.

Re:Git-absorb: Git commit –fixup, but automatic by mst on Thursday September 26, 2024 @09:07AM (#41657951)
Oh, hrm, looking at this description and the one liner, I rather like.

Once you mentioned `git add -p` I realised that this is pretty much what I do already, except with a far more efficient way of selecting the relevant commit to do it to.

Muchas gracias.

Re:Git-absorb: Git commit –fixup, but automatic by AndrewHampton on Thursday September 26, 2024 @10:13AM (#41658649)
Yeah, I use about a dozen git aliases in my normal workflow. In case it's helpful, here are the relevant ones for this flow:

  alias git_main_branch='git rev-parse --abbrev-ref origin/HEAD | cut -d/ -f2'
  alias gapa='git add --patch'
  alias grbm='git rebase -i --autosquash $(git_main_branch)'
  alias gfx='git commit --fixup $(git log $(git_main_branch)..HEAD --oneline| fzf| cut -d" " -f1)'
Another favorite is:

  alias gmru="git for-each-ref --sort=-committerdate --count=50 refs/heads/ --format='%(HEAD) %(refname:short) | %(committerdate:relative) | %(contents:subject)'| fzf | sed -e 's/^[^[[:alnum:]]]*[[:space:]]*//' | cut -d' ' -f1| xargs -I _ git checkout _"
gmru (git most recently used) will show you the branches you've been working on recently and let you use fzf to select one to check out.
Re:Git-absorb: Git commit –fixup, but automatic by account42 on Monday September 30, 2024 @05:19AM (#41695127)
Couldn't you just use --patch with the alias to achieve that?
Re:Git-absorb: Git commit –fixup, but automatic by mckn1ght on Wednesday September 25, 2024 @09:26PM (#41653590)
sounds like how magit lets you create fixup commits in emacs
Re:Git-absorb: Git commit –fixup, but automatic by masklinn on Thursday September 26, 2024 @12:08AM (#41654459)
Worse in fact, since magit lets you fixup, squash, or instafix.
Re:Git-absorb: Git commit –fixup, but automatic by AndrewHampton on Thursday September 26, 2024 @07:38AM (#41657176)
This was 100% my inspiration. I used emacs+magit for years. After switching away from emacs for dev work, I still cracked it open for git interactions for another year or so. Eventually, I moved entirely to the shell with a bunch of aliases to replicate my magit workflow.
Re:Git-absorb: Git commit –fixup, but automatic by johnnypangs on Thursday September 26, 2024 @12:47AM (#41654633)
I’ve been using this:

alias gfixup="git commit -v --fixup HEAD && GIT_SEQUENCE_EDITOR=touch git rebase -i --stat --autosquash --autostash HEAD~2"

From what I understand it does the same thing as this crate for the most part. All I do after is:

git push —force-with-lease

Not sure what you get from the crate otherwise

Re:Git-absorb: Git commit –fixup, but automatic by masklinn on Thursday September 26, 2024 @01:30AM (#41654837)
Your alias seems like a completely unecessary complexity. If you want to meld new changes into your branch head you can just alias “git commit --amend”, you don’t need that mess.

Absorb will find the commits to fix up for each change in the working copy, it doesn’t just merge everything into the head.

Re:Git-absorb: Git commit –fixup, but automatic by johnnypangs on Thursday September 26, 2024 @05:42AM (#41656391)
I see, the reason it’s that long complicated alias was that I didn’t want to open up the editor to change the commit every time I updated. “git commit —amend” does that.

I read the rough how it works and it now makes sense. I might give it a try. Thanks!

Re:Git-absorb: Git commit –fixup, but automatic by johnnypangs on Thursday September 26, 2024 @05:43AM (#41656400)
Seems like you can add —no-edit and get the same behavior, now I can delete that alias. Thanks again :)

(Edit: typo)

Re:Git-absorb: Git commit –fixup, but automatic by masklinn on Thursday September 26, 2024 @08:07AM (#41657386)
That is correct, and there is a `--edit` to revert that, so my personal alias is to `git ci --amend --no-edit` such that by default it just merges the staging into the HEAD, and then tacking on `-e` will open the commit message in an editor to expand it.
Re:Git-absorb: Git commit –fixup, but automatic by cryptonector on Thursday September 26, 2024 @12:43PM (#41660529)
You can also set `EDITOR=true` for that `git commit --amend` if you forget about `--no-edit`.
Re:Git-absorb: Git commit –fixup, but automatic by johnnypangs on Thursday September 26, 2024 @12:52AM (#41654657)
I guess the crate version is easier to soft reset?
Re:Git-absorb: Git commit –fixup, but automatic by psadauskas on Thursday September 26, 2024 @12:02PM (#41660029)
I have this one in mine: https://github.com/paul/dotfiles/blob/master/git/.gitconfig#...

    # make a fixup commit for the last time the file was modified
    cff   = "!f() { [ -n $@ ] && git add $@ && git commit --fixup $(git last-sha $@); }; f"
    # Get latest sha for file(s)
    last-sha = log -n1 --pretty=format:%h --grep 'fixup!' --invert-grep
Given a file like `git cff path/to/file.rb`, It'll find the last commit that touched that file, and make a fixup commit for it. Its great for the try-this-change-on-CI cycle: Change the Dockerfile, git cff Dockerfile, push, repeat, without it screwing up because you changed a different file while you were working on it.
Re:Git-absorb: Git commit –fixup, but automatic by mdaniel on Thursday September 26, 2024 @01:15PM (#41660844)
It won't matter until it does, but $@ expands arguments into separate words but those expansions are themselves only word-preserved if the $@ is itself quoted. The [ will probably get away with what you want, but its use in $(git add) and $(git last-sha) almost certainly will not

  $ cat > tmp.sh <<'FOO'
  for a in $@;   do echo "a=$a"; done
  echo
  for b in "$@"; do echo "b=$b"; done
  echo
  for c in $*;   do echo "c=$c"; done
  echo
  for d in "$*"; do echo "d=$d"; done
  echo
  FOO

  $ bash tmp.sh 'alpha beta' $'charlie\ndelta'
  a=alpha
  a=beta
  a=charlie
  a=delta

  b=alpha beta
  b=charlie
  delta

  c=alpha
  c=beta
  c=charlie
  c=delta

  d=alpha beta charlie
  delta
Re:Git-absorb: Git commit –fixup, but automatic by psadauskas on Thursday September 26, 2024 @04:00PM (#41662603)
Yeah, you're probably right. I guess I haven't run it on any files with spaces in the 6 years since I added it to my dotfiles.
Re:Git-absorb: Git commit –fixup, but automatic by emersion on Thursday September 26, 2024 @12:40PM (#41660500)
With a shell such as fish, one can "git commit --fixup <tab>" and a list of commits will be displayed.
Re:Git-absorb: Git commit –fixup, but automatic by jonS90 on Thursday September 26, 2024 @05:23PM (#41663553)
Funny that I've been doing something nearly identical, but with way more boilerplate.

    fzfCommit() {
      local FZF_PROMPT="${FZF_PROMPT:=Commit: }"
      git log --oneline | fzf --border --prompt="$FZF_PROMPT" --height=10 --preview="git show {+1} --color=always" --no-sort --reverse | cut -d' ' -f1 | tr '\n' ' ' | sed 's/[[:space:]]$//';
    }
    function gfixup {
      local commit=$(FZF_PROMPT='Fixup Commit: ' fzfCommit)
      if [[ -z "$commit" ]]; then
        return 1
      fi
      set -x
      git commit --fixup "$commit" --allow-empty > /dev/null || return 1
      git rebase --interactive "$commit"~ --autosquash || return 1
    }