News for hackers. Stuff that matters.
Re:Julia Evans: Git rebase, what can go wrong by mablopoule on Monday November 06, 2023 @12:16PM (#38165421)
I actually hate squash merge because of all the noise it adds. Sure, the commit graph looks nicer, but it come with a terrible loss of information when doing git blame.

I'm a big proponent of rebase and squash if it helps to make a commit more coherent, but we use squash merges by default in the current project I'm working on, and I die a little bit each time I try to understand what changes were related to a line when tracking down a bug.

Re:Julia Evans: Git rebase, what can go wrong by nerdponx on Monday November 06, 2023 @12:19PM (#38165464)
This is the big one for me. destroying Commit information just to keep the graph tidy is a bad idea in my opinion. It would be better if Git provided better tools for filtering the log, e.g. providing some mechanism to elide commits from parents of any merge commit other than the 1st.
Re:Julia Evans: Git rebase, what can go wrong by diek on Monday November 06, 2023 @12:42PM (#38165848)
> destroying Commit information just to keep the graph tidy is a bad idea in my opinion

The commit information I see when telling teams to squash their branches on merge is not valuable.

* "fixing whitespace" * "incorporate review comments" * "fix broken test" * "fix other broken test"

(note, the broken tests were broken by the changes in the PR)

As soon as that PR is merged those commits are worthless. And there are branches with dozens of those "fixing X" commits that would otherwise pollute the commit graph.

Re:Julia Evans: Git rebase, what can go wrong by trevor-e on Monday November 06, 2023 @01:09PM (#38166273)
Yep, intermediate commits on a branch tend to be completely worthless. I'd much rather have "git blame" point to the commit that contains the entire change together.
Re:Julia Evans: Git rebase, what can go wrong by dieselgate on Monday November 06, 2023 @01:21PM (#38166474)
Agree strongly, it's nice in theory to view the intermediate commits but in practice have never needed to look at them
Re:Julia Evans: Git rebase, what can go wrong by gray_-_wolf on Monday November 06, 2023 @01:00PM (#38166116)
> * "fixing whitespace" * "incorporate review comments" * "fix broken test" * "fix other broken test"

Things like this should not be standalone commits though, they should be incorporated into the previous branch by amending the original work. It takes some effort to have a useful git history, it does not just happen on its own.

Re:Julia Evans: Git rebase, what can go wrong by noahtallen on Monday November 06, 2023 @01:40PM (#38166801)
You can very easily rewrite your commit message on GitHub when squash merging. Since the organizations I work exclusively use squash merge, I often just update the commit to be more valuable, listing the important changes it contains. (And of course the PR in GitHub will contain the commit history of the branch that was squashed, as well as any discussion.)

IMO, this is a lot simpler and easier to do than rebasing your branch to have a flawless history.

Re:Julia Evans: Git rebase, what can go wrong by lesuorac on Monday November 06, 2023 @01:09PM (#38166281)
Sounds like six vs half-dozen. Why does it matter if somebody amends vs squashes?
Re:Julia Evans: Git rebase, what can go wrong by gray_-_wolf on Wednesday November 08, 2023 @04:06PM (#38197020)
It does not matter if you have one commit. If your change is split into few commits for increased readability, in that case it does matter.

Do you really believe that if, for example, this change to btrfs filesystem https://lore.kernel.org/linux-btrfs/cover.1699470345.git.jos... would be squashed, nothing of value would be lost?

Re:Julia Evans: Git rebase, what can go wrong by sodapopcan on Monday November 06, 2023 @02:14PM (#38167343)
I rather strongly disagree here.

Having whitespaces mucks up commit, causing you to lose focus of what's actually important.

I have `git blame` aliased to `git blame -w` which ignores whitespace-only changes.

You can also reblame when you come across this formatting commits.

Re:Julia Evans: Git rebase, what can go wrong by 26fingies on Monday November 06, 2023 @01:12PM (#38166328)
You can usually see that in whatever tool youre using anyway. Blame -> find the PR -> see commit history.
Re:Julia Evans: Git rebase, what can go wrong by lelandfe on Monday November 06, 2023 @12:58PM (#38166084)
Those commits would be the bathwater one casts out alongside the useful commits in using squash merges.
Re:Julia Evans: Git rebase, what can go wrong by diek on Monday November 06, 2023 @01:10PM (#38166294)
If the useful commits are the "baby" in your bathwater analogy, all the useful information in those commits is in the squashed commit.

This assumes a branch being merged in represents one logical change (a feature/bugfix/etc) that is "right sized" to be represented by one commit.

Re:Julia Evans: Git rebase, what can go wrong by mablopoule on Monday November 06, 2023 @02:02PM (#38167151)
Yes, but now it's mixed with the bathwater, and now morph into another metaphor as it become the needle in the haystack.

It's okay to have 'low information' commits one can easily ignore in your history, as long as the 'high information' ones stay readable and coherent.

Re:Julia Evans: Git rebase, what can go wrong by gray_-_wolf on Monday November 06, 2023 @12:55PM (#38166045)
You mean like for example `git log --first-parent`?
Re:Julia Evans: Git rebase, what can go wrong by nerdponx on Monday November 06, 2023 @02:03PM (#38167159)
TIL, thank you! Now I know for a fact that squash-mergers have no excuse and can brandish the man page at them.
Re:Julia Evans: Git rebase, what can go wrong by WorldMaker on Monday November 06, 2023 @03:16PM (#38168324)
`--first-parent` also today works for blame and bisect.
Re:Julia Evans: Git rebase, what can go wrong by Izkata on Monday November 06, 2023 @07:16PM (#38171252)
People in general just have no idea how much version control is able to do. For one example try running "git help log" and just tap page-down a few (dozen) times to get an idea what's in there.

For another example, you know how people hate aligning code vertically so much that linters don't allow it nowadays, the primary reason being that if you have to change the spacing then the diffs will identify far too many lines as having changed? Both git and svn have options to ignore whitespace changes:

  git diff -w
  svn diff -x -w
Re:Julia Evans: Git rebase, what can go wrong by diek on Monday November 06, 2023 @12:37PM (#38165763)
> I die a little bit each time I try to understand what changes were related to a line when tracking down a bug

A change/feature/bug is a branch, which is squashed into a commit on your main branch, right? So your main branch should be a linear history of changes, one change per commit.

How does that impact the ability to git blame?

Re:Julia Evans: Git rebase, what can go wrong by joshribakoff on Monday November 06, 2023 @01:30PM (#38166634)
Because sometimes a PR touches more code than a single commit, and you lose the more granular context surrounding the more granular changes. You can always ask git to make the log more coarse, but once you “destroy” the granular history it is for all intents and purposes gone.
Re:Julia Evans: Git rebase, what can go wrong by js2 on Monday November 06, 2023 @01:31PM (#38166660)
Because unless it's the most trivial of features, you'll break it up into smaller commits which each explain what they are doing and make reviewing the change easier.

As a simple example, I recently needed to update a json document that was a list of objects. I needed to add a new key/value to each object. The document had been hand edited over the years and had never been auto-formatted. My PR ended up being three commits:

1. Reformat the document with jq. Commit title explains it's a simple reformat of the document and that the next commit will add `.git-blame-ignore-revs` so that the history of the document isn't lost in `git blame` view.

2. Add `.git-blame-ignore-revs` with the commit ID of (1).

3. Finally, add the new key/value to each object.

The PR then explains that a new key/value has been added, mentions that the document was reformatted through `jq` as part of the work, a recommends that the reviewer step through the commits to ignore the mechanical change made by (1).

A followup PR added a pre-commit CI step to keep the document properly linted in the future.

Re:Julia Evans: Git rebase, what can go wrong by diek on Monday November 06, 2023 @02:00PM (#38167101)
In general I agree with you, there are absolutely times where you want to retain commit history on a particular branch (although I try to keep the source tree from knowing about things like commit IDs).

I would argue that those are by far the minority of PRs that I see. As I mentioned in another comment, _most_ PRs that I see have a ton of intermediary commits that are only useful for that branch/PR/review process (fixing tests, whitespace, etc). Generally the advice I give teams is, "squash by default" and then figure out where the exceptions to that rule are. That's mainly because, in my opinion, the downsides of a noisy commit graph filled with "addressing review comments" (or whatever) commits are a much bigger/frequent issue than the benefits you talk about. It really depends on the team.

Re:Julia Evans: Git rebase, what can go wrong by js2 on Monday November 06, 2023 @02:47PM (#38167882)
> As I mentioned in another comment, _most_ PRs that I see have a ton of intermediary commits that are only useful for that branch/PR/review process (fixing tests, whitespace, etc).

Right, but that's only because developers don't amend and force push their commits to the PR branch as they receive feedback. Which is largely encouraged by GitHub being a terrible code review tool.

To me, git is part of the development process, it's not an extra layer of friction on top. So I compose my commits as I go. I find it helpful for recording what I'm thinking as I write the code. If I wait till the very end, I'll have forgotten some important bit of context I wanted to include. So during the day I may use the commits like save points. But before I push anything I'll often check out a new branch and create and incremental set of commits that have the change broken down into digestible pieces. And if I receive feedback, I'll usually amend those changes into the PR and force push it.

I'd like to add that I spend a lot of time cleaning up tech debt. And I deal with a ton of commits and PRs that don't explain themselves. So I'm really biased toward a clean development workflow because I hope to make the lives of those who come after me easier.

I was also trained on this workflow by being an early git contributor and it had extremely high standards for documenting its work. There's a commit from Jeff King that's a one line change with about six paragraphs of explanation.

There's no right answer here. I value the "meta" part of writing code. Not everyone does and that's okay.

Re:Julia Evans: Git rebase, what can go wrong by throw555chip on Monday November 06, 2023 @04:52PM (#38169549)
When the word "force" is involved, it's time to take a step back and re-evaluate things.
Re:Julia Evans: Git rebase, what can go wrong by js2 on Monday November 06, 2023 @07:07PM (#38171182)
It's due to GitHub lacking change set support. With Gerrit, force pushing isn't required.
Re:Julia Evans: Git rebase, what can go wrong by Izkata on Monday November 06, 2023 @07:20PM (#38171292)
> only useful for that branch/PR/review process (fixing tests, whitespace, etc).

I have had bugfix cases where, digging through the repo history, both of those examples accidentally introduced the bug (the first because the person who made the original change didn't completely understand a business rule so it changed both the code and the test, the second because of a typo in python that only affected a small subset of the data). Keeping the commit separate let me see very quickly what happened and what the intent actually was.

Re:Julia Evans: Git rebase, what can go wrong by mablopoule on Monday November 06, 2023 @01:43PM (#38166858)
Because now instead of having a line changed within a granular level of changes, it's lost with the other changes from the same feature branch, which is a more macro level. So if a change in config is needed for the feature, the part when this config change actually need to be handled, or would impact the data-flow is harder to evaluate now that you mix it with template changes, style changes, new interactions needed for the users, etc...

EDIT: On top of that, there's usually a bit of 'related' work you need for a task, by example when you find an edge case related to your feature, and now you also needed to fix a bug, or you did a bit of refactoring on a related service, or needed to change the data on a badly formatted JSON file.

Unbeknownst to you, you added a bug when refactoring the related service, a bug that is spotted a few months after, only on a very specific edge case. If the cause is not obvious, you might want to reach for git bisect, but that won't be very useful now that everything I've talked about is squashed into a single commit.

Re:Julia Evans: Git rebase, what can go wrong by diek on Monday November 06, 2023 @02:04PM (#38167185)
> EDIT: On top of that, there's usually a bit of 'related' work you need for a task, by example when you find an edge case related to your feature, and now you also needed to fix a bug, or you did a bit of refactoring on a related service, or needed to change the data on a badly formatted JSON file.

I agree that's related work, but I'd argue that work doesn't belong in that branch. If you find a bug in the process of implementing a feature, create a bugfix branch that is merged separately. If you need to refactor a service, that's also a separate branch/PR.

That's actually the most common pushback I get from people when I talk about squashing. They say "but then a bunch of unrelated changes will be lumped together in the same commit", to which I respond, "why are a bunch of unrelated changes in the same branch/PR?"

Re:Julia Evans: Git rebase, what can go wrong by mablopoule on Monday November 06, 2023 @02:42PM (#38167789)
I agree with you in principle, but it's usually because of process and friction. In the place I'm working right now, that would result in days lost as I need to create a new Jira ticket, which obviously require a team meeting for grooming (because Agile!), and then going after colleagues so that the PR is accepted, which best case still need for CI/CD pipeline to finally deploy, and then merge it to the dev branch, and finally rebase the current feature branch... and all this multiple times.
Re:Julia Evans: Git rebase, what can go wrong by johnsbrayton on Monday November 06, 2023 @12:28PM (#38165627)
I do squash merges but keep the feature branches. So after determining that I made a change as part of a big pull request, I can then look at the commit/blame history for the pull request source branch if necessary.
Re:Julia Evans: Git rebase, what can go wrong by matijsvzuijlen on Monday November 06, 2023 @12:45PM (#38165879)
This means having to keep these branches around cluttering up everything, and makes git bisect a lot more complicated.
Re:Julia Evans: Git rebase, what can go wrong by mvdtnz on Monday November 06, 2023 @01:02PM (#38166159)
I'm guessing you don't work on large projects. This would create an outrageous amount of noise in a busy repository.
Re:Julia Evans: Git rebase, what can go wrong by skybrian on Monday November 06, 2023 @12:29PM (#38165658)
This all depends on the project. Sometimes you don’t look at history all that much. Sometimes the loss of information is acceptable.
Re:Julia Evans: Git rebase, what can go wrong by Izkata on Monday November 06, 2023 @01:19PM (#38166452)
If you don't look at history much, why would you care about keeping it "clean"? Just keep the truth of the changes in the history for those of us who do use it, and you can continue ignoring it.
Re:Julia Evans: Git rebase, what can go wrong by WirelessGigabit on Monday November 06, 2023 @12:52PM (#38165987)
Me too. I care a LOT about provenance. And squash merge completely breaks that.

When my branch is up to date with `main` I can build an artifact, fast forward merge that branch into `main` and RETAIN the artifact, and merely update its tags to mark it as `merged` in.

With a squash I lose that information.

Now, GitHub does not allow me to do a fast-forward merge but I can still trace the 2 commits that are the parent of the resultant merge, and find the artifact based on that, and retag.

Re:Julia Evans: Git rebase, what can go wrong by Lio on Monday November 06, 2023 @02:15PM (#38167361)
Git blame confuses people even without squash merges.

I've seen people forget to go back more than one commit and then blame the person who last indented a file instead of going back to the commit that actually wrote the code many times.

Re:Julia Evans: Git rebase, what can go wrong by erik_seaberg on Monday November 06, 2023 @02:53PM (#38167950)
We shouldn’t tamper with code we don’t actually need to fix, it’s not a good use of time and it makes history less useful. Just because it doesn’t look like I wrote it doesn’t make it wrong.
Re:Julia Evans: Git rebase, what can go wrong by Lio on Monday November 06, 2023 @03:07PM (#38168177)
I’m thinking of situations where the surrounding structure of the code has been changed to correct a problem.

That’s done by an automated tool. Correction of indentation is just a byproduct.

I don’t consider that “tampering”.

Re:Julia Evans: Git rebase, what can go wrong by WorldMaker on Monday November 06, 2023 @03:22PM (#38168411)
You can deal with that through tooling.

In a lot of my work I call those types of automated tool commits "wrench" commits personally and even have a simple shell script to help automate committing them. In my case I prefix the command line with a wrench emoji. At that point it's very obvious in git blame that if a line starts with a wrench it was last touched by an automated tool of some sort.

You can also very easily at that point grep your git log for wrenches to dump commit hashes into a git-ignore-revs file and automate that part too so that those commits don't even show up in git blame at all.

Re:Julia Evans: Git rebase, what can go wrong by sodapopcan on Monday November 06, 2023 @03:52PM (#38168826)
> I've seen people forget to go back more than one commit and then blame the person who last indented a file instead of going back to the commit that actually wrote the code many times.

I default my `git blame` to `git blame -w` which ignores whitespace commits. Though knowing how to jump back commits should be required knowledge.