Live data from Hacker News

How to commit part of file in Git

newbeelearn.com

21–30 of 104 posts

Re: How to commit part of file in Git

#21
post #5
post #3

The only sane way to do this is with a proper IDE. Doing it via the command line is pure masochism.

Why? I use git add -p absolutely all the time, its really simple to learn

It's not the only sane way, it's just what IDE zealots say... even though you don't even need an IDE to do it the "IDE way". There are plugins to highlight and stage diffs in most text editors.

Regardless I also really like `git add -p`. I don't use it often but it really makes me focus on and consider each hunk. It was the defacto way to do it at an old company I worked at and no, it was not a small startup "hipster" company, we built enterprise ERP software.

Re: How to commit part of file in Git

#22
post #18

Earlier quoted context omitted.

Relatively common practice where I work. What do you do instead?

Not the GP but we tend to use decently small and meaningful commits to make PRs easier to review. They then get squashed, so you can only (easily) revert the whole PR.

Personally I’m happy with that. If the thing merged caused an issue, I’d want to revert the entire thing that was merged, not a subpart of it which would result in a state of the codebase that nobody reviewed.

If you’re very diligent about making good atomic commits that always pass all test that can work, but I find that squashing PRs (and PRs being relatively small) is a very good trade off

Re: How to commit part of file in Git

#23
post #5

Earlier quoted context omitted.

Why? I use git add -p absolutely all the time, its really simple to learn

I use `git add -p` regardless if the whole file gets committed or not since I use it as a visual review tool to verify what I've changed.

TIL from another story on HN:

commit.verbose true This adds the whole commit diff in the text editor where you’re writing your commit message, to help you remember what you were doing.

https://jvns.ca/blog/2024/02/16/popular-git-config-options/#...

Re: How to commit part of file in Git

#25
post #18

Earlier quoted context omitted.

Relatively common practice where I work. What do you do instead?

Not the GP but we tend to use decently small and meaningful commits to make PRs easier to review. They then get squashed, so you can only (easily) revert the whole PR.

You're not the only ones, but I can't understand this approach. Do people never then read the version history? It must be impossible to understand commits' diffs with the changes all squashed together.

Re: How to commit part of file in Git

#26
post #18

Earlier quoted context omitted.

Not the GP but we tend to use decently small and meaningful commits to make PRs easier to review. They then get squashed, so you can only (easily) revert the whole PR.

Personally I’m happy with that. If the thing merged caused an issue, I’d want to revert the entire thing that was merged, not a subpart of it which would result in a state of the codebase that nobody reviewed. If you’re very diligent about making good atomic commits that always pass all test that can work, but I find that squashing PRs (and PRs being relatively small) is a very good trade off

Sounds reasonable. Also, when squashing a PR would conflate unrelated commits then maybe those commits should have been separate PRs instead.

I've recently started to like squashing after usually preferring rebase merges, because with squashes I have an easy option to rewrite the commit message and edit out all of those meaningless "fix typo", "improve" and "now actually working" lines...

Re: How to commit part of file in Git

#27
post #5
post #3

The only sane way to do this is with a proper IDE. Doing it via the command line is pure masochism.

Why? I use git add -p absolutely all the time, its really simple to learn

Also use git add -p frequently. I can't understand why people are even discussing using other tools.

Re: How to commit part of file in Git

#28
This article should mention `lazygit`. That's one of the best ways to customize hunks without committing to `emacs`.

Magit's USP is that it's a great interface for those who already use (or are willing to use) `emacs`. `lazygit` is a great interface in its own right.

Re: How to commit part of file in Git

#29
the only reason I do some git stuff in vim and not _always_ in the shell, is because tpope is very thoughtful and fugitive.vim provides nice ways to deal with hunks or hunk partials (visually selecting a range within a hunk, for i.e.)

https://github.com/tpope/vim-fugitive/blob/master/doc/fugiti...

Re: How to commit part of file in Git

#30
post #3

The only sane way to do this is with a proper IDE. Doing it via the command line is pure masochism.

I do hg split (which I think this is based on? Or at least it's very similar) all the time and it's fine. I guess an IDE makes it a little easier to select regions that don't line up with hunks?

Is there a git equivalent?
Post reply on HN