News for hackers. Stuff that matters.
Re:Idiot Proof Git by dec0dedab0de on Wednesday November 09, 2022 @10:59AM (#33533465)
When I say "the evolution of the product" I really mean "the "evolution of the code". When a small feature branch with 5 commits - four of which say "wip" and the last one says "added color support" - gets merged as is, and all relevant information is held hostage by whatever Git platform the company is using this week and not inside the repository itself, the log is not useful to me regardless of any strategy.

Yes, it can be annoying if your developers are committing nonsense, but then just tell them to not do that, or to rebase locally before pushing.

If you find yourself troubleshooting a bunch of nonsense commits, you can just do a diff to the merge commit, and it will show you all the changes. But you also have the option of figuring out exactly which commit caused the problem, and seeing it in context. If I see an error in the middle of a bunch of commits that look like "trying x with y." Then I know that this is a tricky problem, and the developer was lucky to get it to work at all. If it is in the middle of a standard looking commit, then the developer didn't struggle with this. So maybe they didn't put enough effort into it, or maybe it is a rare corner case.

When I'm troubleshooting other peoples problems, every bit of information helps. Especially when the developer who introduced the problems is no longer with the company. Squashing commits removes some of that information, without providing anything that I can't approximate by using merge commits in logging/diffs.

Re:Idiot Proof Git by Quekid5 on Wednesday November 09, 2022 @11:41AM (#33534136)
> If I see an error in the middle of a bunch of commits that look like "trying x with y." Then I know that this is a tricky problem, and the developer was lucky to get it to work at all.

Just an aside, but if there problem being solved is harder than it looks then this deserves a comment explaining why in either the commit message or in the code itself.

Re:Idiot Proof Git by idop on Wednesday November 09, 2022 @11:11AM (#33533663)
To be clear, what I'm advocating for is that feature branches get rebased regularly by the developer until PR-time and a clean merge into the mainline. I usually recommend squashing to one commit but do not insist.

I can definitely see how those intermediate commits can provide more information, but there's a tradeoff. More often than not, they do not provide me much value, and instead give me bloat, so I prefer to keep things simple.

Telling developers not to do something is like telling a kid not to push that red button. The average developer chooses what's easiest _right now_ and thinks it's someone else's job to fix the mess at PR time. And they're afraid, because that one time five years ago they ran a rebase without knowing what it does, and lost some code without knowing it's actually right there in the reflog, and since then they are deathly afraid of Git. I know how to use log and diff and all the others quite well, but most don't. So I'm trying to make things easier on everyone in the long term, not the short term.

Re:Idiot Proof Git by P5fRxh5kUvp2th on Wednesday November 09, 2022 @07:16PM (#33540263)
"Why doesn't git bisect work?"

"well, it landed on this rebased commit that's huge. I guess it was a kind of useful, just not as useful as we'd like".

Re:Idiot Proof Git by firesloth on Wednesday November 09, 2022 @07:42PM (#33540541)
Haha, true! On the other hand, is that better or worse than running into a string of "wip" commits that had the code in a broken state.
Re:Idiot Proof Git by Dylan16807 on Wednesday November 09, 2022 @10:37PM (#33541885)
But if the alternative is that it's ten commits and most of them don't work anyway, the bisect takes longer to give you the same lousy information.
Re:Idiot Proof Git by P5fRxh5kUvp2th on Thursday November 10, 2022 @05:13AM (#33544488)
That's not the alternative, who develops like that?

It's like the first time I saw the essay calling ORM's the "vietnam of the software industry". I remember reading it and wondering who the hell would use ORM's in that manner?

Apparently a lot of people, but if you're using rebase because you don't know how to create commits that build and are functional then I submit the issue is with you.

Re:Idiot Proof Git by idop on Thursday November 10, 2022 @11:06AM (#33548468)
> That's not the alternative, who develops like that?

As an independent contractor working with many different companies, unfortunately, virtually everybody.

Re:Idiot Proof Git by firesloth on Wednesday November 09, 2022 @07:39PM (#33540511)
I've been recommending making a git tag before rebasing. eg:

  git tag -f pre-rebase
Re:Idiot Proof Git by krinchan on Wednesday November 09, 2022 @12:35PM (#33534995)

    git add .; and git commit --amend --no-edit; git push origin --force
Rarely, I have to do pipeline work on repositories. You'd normally see twenty "Fix Jankins Issue" commits on the main branch because of some nonsense that only happens when you deploy UAT or whatever. Once I learned this little gem, this is also how I manage my feature branches mostly. But also my employer's fleet of laptops has been aging and I've had to do 3 swap outs this year, so I like to keep my in progress work pushed up just in case.
Re:Idiot Proof Git by sixstringtheory on Wednesday November 09, 2022 @01:29PM (#33535839)
I did this for a while but have moved on to

    git commit --fixup HEAD
and you can tack an -e on the end to add more notes in the commit message body. You can fixup prior commits by supplying their short hash, which is how I originally discovered this: I essentially wanted to amend a commit farther back in my branch.

This makes a commit with a “fixup!” prefix that works with

    git rebase --interactive --autosquash
you can also form that kind of commit directly, and there is also a “squash!” directive. Now you don’t have to force push amended commits. And it helps sometimes when you accidentally amend something you didn’t mean to–now you can just soft reset to HEAD~1 and try again.

I don’t even bother locally rebasing to autosquash it all anymore since we use squash-to-merge/rebase in github PRs now.

Re:Idiot Proof Git by firesloth on Wednesday November 09, 2022 @07:30PM (#33540423)
I did something very similar with a bash function:

  function git-commit-fixup() {
    git commit --fixup ":/$*"
  }
  alias gcf="git-commit-fixup"
  # Looks for the most recent commit that matches its arg
  # eg: we have three commits with messages: 
  #   "fix: the thing" 
  #   "feat: 5 percent cooler"
  #   "test: test coolness"
  # then we do some work and git add, then do:
  gcf cooler"
  # now we have 4 commits: "
  #   "fix: the thing" 
  #   "feat: 5 percent cooler"
  #   "test: test coolness"
  #   "fixup! feat: 5 percent cooler"
  # And autosquash will combine the fixup commit with the appropriate semantic commit as you say.
Sadly I've stopped using it as someone introduced a bunch of commit lint git hooks that choke horribly on the "fixup!" part. And you can't pass --no-verify while rebasing.
Re:Idiot Proof Git by tkot on Wednesday November 09, 2022 @04:31PM (#33538168)
I'd probably suggest --force-with-lease just to be sure ;)
Re:Idiot Proof Git by firesloth on Wednesday November 09, 2022 @07:36PM (#33540494)
Me too. I made an alias that was _shorter_ than --force to make it easier to type (and hence more likely for me to use by default).
Re:Idiot Proof Git by tremon on Wednesday November 09, 2022 @05:41PM (#33539119)
git add .

We have had multiple security incidents because some developer left a credential file inside the local git clone (no, not all tooling supports out-of-tree stored credential). Blind 'git add .' is the first thing I teach my developers not to do.

Re:Idiot Proof Git by ratorx on Wednesday November 09, 2022 @08:16PM (#33540861)
git add . is very useful though. Surely, the first thing to teach here is to always git status before committing?

My typical workflow is to git add . to see the mess I’ve made then decide how to clean it up. If I’ve mistakenly added a credentials file, the fix is to add it to the gitignore and unstage it, not JUST unstage it.

Not saying that you shouldn’t do both, but maintaining a gitignore and completely removing the potential problem for other people seems better than pretending your tool is more limited than it is.

Re:Idiot Proof Git by notpushkin on Thursday November 10, 2022 @02:31AM (#33543357)
If you can tolerate a GUI, Git Cola might be a solution. I'm using it exclusively for some 5 years now – it's lightweight enough, but still makes you think about what you're about to commit. You can add things to .gitignore directly from there, too.

https://git-cola.github.io/

Default layout is a bit weird IMO, here's what I'm doing instead: https://u.ale.sh/my-git-cola-screenshot.png

Re:Idiot Proof Git by krinchan on Thursday November 10, 2022 @09:43AM (#33547145)
You don't use .gitignore? All our projects use dotenv for local credentials with a .gitignore that covers build, log, and any *.env file.

Sounds like you need better tooling, tbh.

Re:Idiot Proof Git by ernestns on Tuesday November 15, 2022 @10:49AM (#33610655)
You can combine

  git add .; git commit --amend --no-edit;
with the `-a` option

  git -a --amend --no-edit;
Re:Idiot Proof Git by xenomachina on Wednesday November 09, 2022 @01:25PM (#33535792)
> Yes, it can be annoying if your developers are committing nonsense, but then just tell them to not do that, or to rebase locally before pushing.

I'm pretty sure rebasing locally is exactly what the person you're arguing with is arguing for. The original comment in this thread was saying you should never rebase, always just merge.