Live data from Hacker News

Using 'Git rebase' to Perfect Commits

adamjhawley.github.io

11–20 of 24 posts

Re: Using 'Git rebase' to Perfect Commits

#11
post #9
post #8

I've found that separating work from recording that work is the easiest approach. So what I end up doing is waiting the code and tests and making sure things work as expected. Only then will I start recording that work into a sensible series of commits. To do this, I'll generate the diff of my work relative to the base branch and then stage parts of that work using the git apply command and make a commit out of it. I…

That sounds like too long to wait to commit, by my judgement. There's too much risk of loss of work, or at least annoyance getting it back via editor undo. What about making smaller commits and going back and editing it later? You could either 'git reset --soft' to "undo" some commit(s) and then craft them how you like, or anything else. Seems like your git skills should easily be good enough to handle that. By the w…

> There's too much risk of loss of work

Backups can be made independent of source control.

> What about making smaller commits and going back and editing it later?

In experience, it's actually more time consuming to restructure an existing set of commits (especially if they're making changes to the same files and same lines of code) compared to just making a new set of commits.

> By the way, you know about 'git add -p', right?

I am aware of it, but I'm not a fan of the menu based approach. When I stage code with git apply, I actually use vim's feature of visually highlighting the part of the diff I want to stage and filtering those lines through the git apply command. I can skip things like debug statements I've added to the code when staging. I can also unstage code by using the same command with the -R parameter.

I even commit within vim by reading the output of git status -v, writing my commit message and filtering that message through git commit with the -F parameter to have it read from standard input.

Re: Using 'Git rebase' to Perfect Commits

#12
post #11
post #9

Earlier quoted context omitted.

That sounds like too long to wait to commit, by my judgement. There's too much risk of loss of work, or at least annoyance getting it back via editor undo. What about making smaller commits and going back and editing it later? You could either 'git reset --soft' to "undo" some commit(s) and then craft them how you like, or anything else. Seems like your git skills should easily be good enough to handle that. By the w…

> There's too much risk of loss of work Backups can be made independent of source control. > What about making smaller commits and going back and editing it later? In experience, it's actually more time consuming to restructure an existing set of commits (especially if they're making changes to the same files and same lines of code) compared to just making a new set of commits. > By the way, you know about 'git add -…

> Backups can be made independent of source control.

To be fair there's a middle ground. You can just commit as WIP and force push to save progress. Then undo that commit and start making the individual commits. Everyone's happy.

Re: Using 'Git rebase' to Perfect Commits

#13

Interactive git rebase are a good way to casually flex with Vim to a point and clicker, even if you hardly know anything. "cws" to change "pick" to "s" ("cwf" for fix up), then "j." to repeat for the next line. Also one of my favourite aliases is "rn" for interactively rebase the last n commits. Something like "!git rebase -i HEAD~$1" (from memory).

Thanks to tpope, vim comes with some out of the box editing support for rebase¹. So, you don't even need to use change word. :F[ixup] to change to fixup, which supports ranges too(super useful with V or V).

The standard vim {in,de}crement commands are bound to :Cycle in rebase buffers as well, so you can use from anywhere in the line to twiddle the command for the current commit.

¹ :e $VIMRUNTIME/ftplugin/gitrebase.vim

Re: Using 'Git rebase' to Perfect Commits

#15

What's perfect for this is stgit https://stacked-git.github.io/ You can easily work on, perfect, and re-order hundreds of patches in a single branch.

That looks like it solves my number one annoyance: maintaining a bunch of concurrent patches on a dev branch, including personal patches I don't intend to merge.

Re: Using 'Git rebase' to Perfect Commits

#16

What's perfect for this is stgit https://stacked-git.github.io/ You can easily work on, perfect, and re-order hundreds of patches in a single branch.

That looks like it solves my number one annoyance: maintaining a bunch of concurrent patches on a dev branch, including personal patches I don't intend to merge.

There are several tools for stacking patch series, here is a list: https://github.com/epage/git-stack/blob/main/docs/comparison...

Re: Using 'Git rebase' to Perfect Commits

#17

Interactive git rebase are a good way to casually flex with Vim to a point and clicker, even if you hardly know anything. "cws" to change "pick" to "s" ("cwf" for fix up), then "j." to repeat for the next line. Also one of my favourite aliases is "rn" for interactively rebase the last n commits. Something like "!git rebase -i HEAD~$1" (from memory).

You can also use visual block mode to highlight a range of commands and change in one go. Eg

  C-Ve}c
So that’s set visual highlight block.

Move to the end of the word.

Move down to the end of the paragraph.

Change the highlighted text in one.

Once you think like that it trips off the fingers from muscle memory.

Re: Using 'Git rebase' to Perfect Commits

#18
It feels like there are as many git workflows as developers. Personally, for almost all of my development I work on a single commit at a time. When I'm positive some changed code is going to be in the final commit I `git commit --amend` it to the tip of the main branch. Unstaged changes are what is in progress. I use `git show` to see the stuff I'm done with but haven't pushed and `git diff` to see the stuff that's in progress. Sometimes I want to undo something I've committed... I either do it manually or use aliases to help out. I rebase frequently to bring in changes from other devs.

Re: Using 'Git rebase' to Perfect Commits

#19

Earlier quoted context omitted.

That looks like it solves my number one annoyance: maintaining a bunch of concurrent patches on a dev branch, including personal patches I don't intend to merge.

There are several tools for stacking patch series, here is a list: https://github.com/epage/git-stack/blob/main/docs/comparison...

These look great, I will definitely check some of them out. Thanks!

Re: Using 'Git rebase' to Perfect Commits

#20
post #11
post #9

Earlier quoted context omitted.

That sounds like too long to wait to commit, by my judgement. There's too much risk of loss of work, or at least annoyance getting it back via editor undo. What about making smaller commits and going back and editing it later? You could either 'git reset --soft' to "undo" some commit(s) and then craft them how you like, or anything else. Seems like your git skills should easily be good enough to handle that. By the w…

> There's too much risk of loss of work Backups can be made independent of source control. > What about making smaller commits and going back and editing it later? In experience, it's actually more time consuming to restructure an existing set of commits (especially if they're making changes to the same files and same lines of code) compared to just making a new set of commits. > By the way, you know about 'git add -…

> Backups can be made independent of source control.

They can be, but it's ~hard to do it in a way that lines up with saving all your work, and you miss out on conveniences like the reflog (eg what was the state of _this_ branch half an hour ago?) Zfs snapshots can do it if they're manual or ~minutes apart and automatic, but that's a pretty blunt tool for this IMO.

> In experience, it's actually more time consuming to restructure an existing set of commits [...]

That's fair, but you _can_ just do that. If you have the commits in the first place, you have the choice. 'git reset --soft master' for example is "I don't want those commits on this branch anymore, but I want the final changes to rework into new commit(s)". I'd say about half the time I nuke the commits, half the time I rework them or they were just good enough already.

Sounds like you have a workflow that works for you though, just throwing out ideas in case they help.

> I am aware of it, but I'm not a fan of the menu based approach.

Agreed, it's sometimes tedious. I'll look into the way you do it, seems like for larger ~features your way might be less annoying.

Post reply on HN