News for hackers. Stuff that matters.
Re:Ask HN: What is your Git commit/push flow? by jawns on Thursday March 17, 2022 @11:39AM (#30712327)
Here's how I address this problem.

When I'm developing, but before I create a PR, I'll create a bunch of stream-of-consciousness commits. This is stuff like "Fix typo" or "Minor formatting changes" mixed in with actual functional changes.

Right before I create the PR, or push up a shared branch, I do an interactive rebase (git rebase -i).

This allows me to organize my commits. I can squash commits, amend commits, move commits around, rewrite the commit messages, etc.

Eventually I end up with the 2-4 clean commits that your coworkers have. Often I design my commits around "cherry-pick" suitability. The commit might not be able to stand on its own in a PR, but does it represent some reasonably contained portion of the work that could be cherry-picked onto another branch if needed?

Granted, all of the advice above requires you to adhere to a "prefer rebase over merge" workflow, and that has some potential pitfalls, e.g. you need to be aware of the Golden Rule of Rebasing:

https://www.atlassian.com/git/tutorials/merging-vs-rebasing#...

But I vastly prefer this workflow to both "merge only," where you can never get rid of those stream-of-consciousness commits, and "squash everything," where every PR ends up with a single commit, even if it would be more useful to have multiple commits that could be potentially cherry-picked.

Re:Ask HN: What is your Git commit/push flow? by elpakal on Thursday March 17, 2022 @12:48PM (#30713208)
I do this (kind of too), but instead of an interactive rebase I just do a `git reset --soft <target_branch>`, where target_branch is the local (and up-to-date) copy of my target branch. That gets me one clean commit that I can force push up to replace my branch @ remote.

(This works for me because auditing commit history is not important where I work, if it were I would organize commits better.)

Re:Ask HN: What is your Git commit/push flow? by dopidopHN on Thursday March 17, 2022 @02:12PM (#30714201)
I first did rebase, and after a while I realized I was not getting much of it since I mostly wanted to merge everything down to a single commit.
Re:Ask HN: What is your Git commit/push flow? by andrewzah on Thursday March 17, 2022 @02:14PM (#30714226)
This is the ideal workflow for me since I think merge commits clutter up the log, but it falls apart if people aren't consistent in following it.

*glances and $corp git repo and sees 'updates' 'fix' 'updates'. Sigh.

Re:Ask HN: What is your Git commit/push flow? by Consultant32452 on Thursday March 17, 2022 @04:11PM (#30715388)
I've always been fond of the "WIP" commits.
Re:Ask HN: What is your Git commit/push flow? by aerovistae on Thursday March 17, 2022 @11:40AM (#30712344)
...what is the golden rule of rebasing

edit: googled, "Never rebase while on a public branch" i.e. a shared branch

Re:Ask HN: What is your Git commit/push flow? by notapenny on Thursday March 17, 2022 @11:44AM (#30712391)
It usually works out fine when you do a `git pull --rebase`, but not everyone does this or has it setup so pulling might have some nasty effects. Generally helps to consider a feature branch as a private branch. Don't push to other people's features without asking, don't fuck up other people's work.
Re:Ask HN: What is your Git commit/push flow? by OJFord on Thursday March 17, 2022 @11:57AM (#30712569)
Everyone absolutely should configure that. (Git config pull.rebase true.)

Such an annoying mess it leaves otherwise. And CI is building 'merge branch master', on the master branch, great.

Re:Ask HN: What is your Git commit/push flow? by lambic on Thursday March 17, 2022 @01:26PM (#30713648)
Are there any downsides to doing this? Why isn't it on by default?
Re:Ask HN: What is your Git commit/push flow? by OJFord on Thursday March 17, 2022 @01:38PM (#30713793)
I suppose just the usual 'rebase vs. merge' - i.e. if there are conflicts to resolve they will be dealt with commit-by-commit in the usual way when rebasing, whereas without the option set they will be dealt with all at once 'merge-style'. I happen to think that's a feature, but I know some don't like it.

I think there's less argument in favour of merge than usual with pulls though - since it's much less like a semantic merge to begin with for git-merging to preserve a history of.

I'm certainly not aware of any objective downside/gotcha/'oh but it doesn't work when...', no.

The docs only add that you might further want to set it to `interactive` or `merges` rather than `true`, for the effect of rebasing with those options: https://git-scm.com/docs/git-config#Documentation/git-config...

Re:Ask HN: What is your Git commit/push flow? by formerly_proven on Thursday March 17, 2022 @02:49PM (#30714559)
> Why isn't it on by default?

Because git was written primarily for the Linux kernel and the defaults reflect that. The workflow of the Linux kernel is completely different from what most people outside of it do with git. "git pull" is used for opposite purposes in both worlds.

Re:Ask HN: What is your Git commit/push flow? by heurisko on Thursday March 17, 2022 @02:53PM (#30714604)
I think it's the usually public vs private branch issue.

If you're merging a public branch with another public branch, then if you a rebase, rather than a merge commit, then you mess up the history for anyone has pulled that branch.

For private branches that isn't an issue.

Re:Ask HN: What is your Git commit/push flow? by mkesper on Thursday March 17, 2022 @12:16PM (#30712791)
Combine with git config --global rebase.autostash for your own protection.
Re:Ask HN: What is your Git commit/push flow? by nyanpasu64 on Thursday March 17, 2022 @04:14PM (#30715417)
FWIW I set pull.ff to only, since I don't want merge commits or rebases happening without explicitly calling pull --rebase or --merge.
Re:Ask HN: What is your Git commit/push flow? by cpcallen on Thursday March 17, 2022 @04:11PM (#30715386)
> And CI is building 'merge branch master', on the master branch, great.

How does one set this up in GitHub actions?

Re:Ask HN: What is your Git commit/push flow? by OJFord on Friday March 18, 2022 @07:47AM (#30720953)
By default it shows the commit message doesn't it? At least I'm not aware I've done anything for e.g. https://github.com/OJFord/terraform-provider-wireguard/actio...

The annoyance I'm describing is that when the commit message is 'merge branch master' (and especially if, as the label next to it shows, it is the master branch) this is crap and useless, and hiding the 'real' commits behind it that the committer had locally while behind the remote. If they had `git pull --rebase`d (or `git pull` with the config option set) the commit message would be that of the latest 'real' one.

Re:Ask HN: What is your Git commit/push flow? by SnowingXIV on Thursday March 17, 2022 @01:55PM (#30714017)
Yes, if you're going to rebase, rebase your feature branch. Do not do it on the shared staging/dev etc.
Re:Ask HN: What is your Git commit/push flow? by fatnoah on Thursday March 17, 2022 @03:29PM (#30714989)
This is very close to my approach. I work in a private branch and make many commits along the way, but then organize it to tell a coherent story for the actual PR.
Re:Ask HN: What is your Git commit/push flow? by spacechild1 on Thursday March 17, 2022 @05:04PM (#30715863)
I do the same. A clean and logical history helps other people understand your code and might consequently increase the chances of getting it merged.
Re:Ask HN: What is your Git commit/push flow? by nyanpasu64 on Thursday March 17, 2022 @04:11PM (#30715390)
I do the same thing generally. In fact I generally find multiple mistakes or changes to be made, reading over my own code before merging, requiring figuring out which commit to merge my changes into (git absorb helps in the easy cases) and even more rebasing, or giving up and adding an extra commit at the end.

I see some people whose projects (Furnace Tracker, PipeWire, previously Famitudio) seem to make progress very quickly without getting noticeably slowed down by technical debt, despite sloppy programming and unorganized commit logs (push-to-head). Meanwhile I move slowly, dread reviewing hundreds of lines of my own code, and produce technical debt (regrets) anyway, not as many surface-level lintable errors but plenty of entrenched mistakes. I wish I could move faster, but instead struggle to make progress.

Re:Ask HN: What is your Git commit/push flow? by matticusrex on Thursday March 17, 2022 @12:24PM (#30712896)
The only problem I have with this workflow in the command line is that I would like to be able to split changes to the same file across multiple commits. I think some GUI tools enable this, anyone know about it?
Re:Ask HN: What is your Git commit/push flow? by tuckerpo on Thursday March 17, 2022 @12:28PM (#30712957)
I do this via git add -p which breaks your changeset down into atomic patches that you can either add, skip or delete before making a commit. You can turn one file change into many commits this way, if need be.
Re:Ask HN: What is your Git commit/push flow? by btschaegg on Thursday March 17, 2022 @12:36PM (#30713057)
Have a look at `tig`. It's even included in Git for Windows now and does this reasonably well.

Only the keybindings are a bit weird if you're not accustomed to Vim bindings:

- Open tig

- Change into the staging view with `s`

- Select your file using the arrow or `j` and `k` keys

- Press Return to show the diff

- Navigate to the line(s) in question with `j` and `k` (arrow keys will switch files)

- Stage parts with `1` (single line), `2` (chunk parts), `u` (chunks) or split chunks with `\`

- "Leave" the diff with `q`

- You can find the keybindings with `h` in the help screen, which also uses Vim keys -- like manpages usually do

Re:Ask HN: What is your Git commit/push flow? by mariusor on Thursday March 17, 2022 @12:50PM (#30713227)
"s" is status view, which you can also get at by running "tig status" directly.

"c" is staging view.

> - Stage parts with `1` (single line), `2` (chunk parts), `u` (chunks) or split chunks with `\`

You can also revert a chunk or file by using "!". Sometimes this is very useful.

Re:Ask HN: What is your Git commit/push flow? by btschaegg on Thursday March 17, 2022 @12:51PM (#30713251)
Ah, thanks for the correction :)
Re:Ask HN: What is your Git commit/push flow? by mikewhy on Thursday March 17, 2022 @01:13PM (#30713481)
I used tig for a bit because it was the nicest way I could find to do line-level staging in a terminal. But was really impressed with gitui https://github.com/extrawurst/gitui so I've switched to that
Re:Ask HN: What is your Git commit/push flow? by kjeetgill on Thursday March 17, 2022 @02:35PM (#30714428)
If you're a vim guy, I use `git difftool` setup with `vimdiff` for this. Let's say you have your changes in a branch CHANGES on top of public branch PUBLIC.

1. I `git checkout PUBLIC -b CLEANUP` to a new branch.

2. Do a `git difftool CHANGES`, which opens each changed file in vimdiff one at a time.

3. For each file, I use :diffput/:diffget or just edit in changes I want.

4. Commit these changes on the CLEANUP branch.

5. Use `git difftool CHANGES` again to see the remaining diff.

6. Repeat until the diff comes back empty!

My unstructured changes tend to contain a handful of small typo fixes, white spacing, localized refactors, and 1 or 2 larger refactors and a behavioral change. Once they're all broken out, It's usually easy enough to use `git rebase -i` and reorder the smaller changes first, put out PRs for just those first, etc.

Re:Ask HN: What is your Git commit/push flow? by distortedsignal on Thursday March 17, 2022 @02:28PM (#30714360)
I use the interactive flag on git add for this. It lets you add parts of a file, commit, and then do it all again. I want to say it's git add -i, but I'm not 100% on that. My fingers just do the right thing when I want it to happen.
Re:Ask HN: What is your Git commit/push flow? by Izkata on Thursday March 17, 2022 @03:50PM (#30715189)
It's "p" for "partial"
Re:Ask HN: What is your Git commit/push flow? by tailspin2019 on Thursday March 17, 2022 @07:50PM (#30717136)
I use Sourcetree [0] for this (on Mac but Win version available)

I've tried most Git clients on Mac over the years and kept gravitating back to Sourcetree.

I only tend to use it for this particular workflow (picking out very granular changes on a line-by-line basis). Otherwise, 90% of my git stuff is via IDE integrations or command line.

[0] https://www.sourcetreeapp.com

Re:Ask HN: What is your Git commit/push flow? by erdo on Thursday March 17, 2022 @04:27PM (#30715516)
Gitx is great for that on a mac, but every time I set up a new MacBook I have to hunt down the latest repo / build. It keeps getting abandoned and then forked and continued, and then abandoned again...

Edit: looks like it's back from the dead again :) https://github.com/gitx/gitx

Re:Ask HN: What is your Git commit/push flow? by yodsanklai on Thursday March 17, 2022 @12:52PM (#30713256)
The way I deal with this is 1. try to make commits small enough so you are less likely to split them after the fact. 2. when I need to split a commit, I use VSCode UI and apply patch by patch. This is one of the rare case were I use a GUI for git. For most other things the command line is fine.
Re:Ask HN: What is your Git commit/push flow? by arilotter on Thursday March 17, 2022 @12:27PM (#30712949)
When you're doing your interactive rebase, find the commit you'd like to split - choose "edit" for that one. Then when you reach that point, you can do `git reset HEAD^1` to bring those changes back onto your staging stack, and make as many commits as you need.
Re:Ask HN: What is your Git commit/push flow? by atq2119 on Thursday March 17, 2022 @01:54PM (#30713997)
I can second the recommendation of tig in the sibling comment.

Additionally, git itself comes with a simple `git gui` command that allows you to do partial commits on a line by line basis. It also has a nice "amend last commit" mode.

Re:Ask HN: What is your Git commit/push flow? by bentcorner on Thursday March 17, 2022 @01:40PM (#30713820)
sublime merge handles this really nicely. You can do this in VS code too but the UI is a little more fiddly (right-click->"Stage Selected Range").
Re:Ask HN: What is your Git commit/push flow? by newaccount74 on Thursday March 17, 2022 @03:28PM (#30714977)
If you have a Mac, check out GitUp. It's a simple but fantastic tool for cleaning up git histories: merging commits, splitting commits, reordering...
Re:Ask HN: What is your Git commit/push flow? by matijsvzuijlen on Thursday March 17, 2022 @01:50PM (#30713961)
Git has a built-in GUI for that. Just run 'git gui'. It's not pretty but it works.
Re:Ask HN: What is your Git commit/push flow? by simlevesque on Thursday March 17, 2022 @01:59PM (#30714053)

  > git gui
  git: 'gui' is not a git command. See 'git --help'.
  The most similar commands are
   gc
   grep
   init
   pull
   push
Re:Ask HN: What is your Git commit/push flow? by formerly_proven on Thursday March 17, 2022 @02:50PM (#30714576)
It's a separate package in some distros since it's written in Perl and uses Tk. gitk is often in the same package.
Re:Ask HN: What is your Git commit/push flow? by bmn__ on Friday March 18, 2022 @02:01AM (#30719130)
Both git-gui and gitk are in Tcl, not Perl.