7 ms·
How to commit part of file in Git
- torblerone 3y agoSplitting up commits is totally underrated and seen rarely. I see colleagues over and over again plumbing 4 kinds of changes into the same commit. Good luck reverting the one change that caused an outage.
- cedws 3y agoCrafting good commits is a skill that nobody seems to care about. In fact, nobody really seems to care about learning git at all despite it being one of the main tools in software development.
- palata 3y agoI would even go as far as "crafting good software is a skill that nobody seems to care about". It feels like people care a lot about "being productive" though, hence tools like all the Copilots.
- williamcotton 3y agoThose are mutually exclusive.
- palata 3y agoCrafting good software and being productive? I guess it depends. I am much slower at producing code than most of my coworkers, but as a result they spend very little time maintaining/debugging it. But I spend most of my time debugging their crap (and they do, too). I think there is a point to make that better code is often more profitable in the longer term. But the norm (at least where I work) is to swim in crappy code all day and complain about it while producing more of it.
- ollysb 3y agoThe incentives are to get the code out the door then improve things later, if it's worth it. There's a lot of risk in product development, it iften doesn't make sense to craft the perfect software when there's a chance that it won't see the adoption that was hoped for.
- palata 3y agoThere is a difference between "crafting the perfect software" and "crafting good software". It is way too common to not realize it and settle on "crafting crappy software" instead. The problem being that crappy software is often very hard to improve later, if it's worth it. I have lived through multiple examples of this, the biggest one being a bad protocol (that was provably badly designed at the beginning) that got adoption, many tools built around it, and ten years later everybody hates it and keeps complaining but it's so hard to replace because of all the tooling built around it.
- fuzzy2 3y agoPeople will actually revert commits? For real? At work? Where do I get to sign up?
- spinningarrow 3y agoRelatively common practice where I work. What do you do instead?
- shric 3y agoNot 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.
- williamdclt 3y agoPersonally 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
- matrss 3y agoSounds 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...
- palata 3y ago> 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 why couldn't you rewrite the commit messages while rebasing? EDIT: oh, you mean from the GitHub webUI, I presume. Got it.
- chipweinberger 3y agojust don’t spend more time splitting up commits than you would have reverting change usually you should err on the side of keeping the commits together, unless you’re close to release, patching a bug in release, etc.
- jerpint 3y agoMostly it’s just that git makes it unnecessarily hard to do atomic commits across a file
- quectophoton 3y agoAnd on this topic, another underrated feature that everyone seems to hate is merge commits. If you use rebase on your own branch, but merge commits on the stable branch (whatever name you use for it), you get both things: * Small individual commits with the size and ordering that the original author intended on their branch. * A "commit group" that describes that set of changes for the sake of future maintainers' sanity, and that can be atomically reverted. More so in PR-based flows. You can have `git log --first-parent` display only the PR merge commits[1], and `git log --no-merges` display the linear history. [1]: Assuming you actually put useful info on those commits. If you use GitHub, those merge commits can be set to default to PR title and description.
- dayjaby 3y agoI also set up recently the policy to only use merge commits on stable branch, as otherwise the path filter^1 in the workflows would not detect correctly which files changed after merging a PR. [1] https://github.com/dorny/paths-filter https://github.com/dorny/paths-filter
- yjftsjthsd-h 3y ago> Good luck reverting the one change that caused an outage. Surely you'd revert to the last good release? I'm sure you could revert a single commit and re-release, but it seems slower (you have to find the right commit to revert, and make sure that the result works correctly) and riskier (since now you're effectively rolling forward to a combination of commits that's never been deployed before)
- IshKebab 3y agoThe only sane way to do this is with a proper IDE. Doing it via the command line is pure masochism.
- dgellow 3y agoWhy? I use git add -p absolutely all the time, its really simple to learn
- arcanemachiner 3y agoUse lazygit, it's faster.
- mstade 3y agoMe too, in fact I find it much easier to do with the terminal than a GUI.
- werdnapk 3y agoI 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.
- mksybr 3y agoTIL 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/#commit-verbose-true https://jvns.ca/blog/2024/02/16/popular-git-config-options/#...
- sodapopcan 3y agoIt'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.
- wilg 3y agoGitHub Desktop does this very well. Highly recommend!
- hallman76 3y agoAs does SourceTree. Also recommended.
- ggregoire 3y agoVSCode handles this very well too: select the lines you want to commit and do "Git: Stage selected ranges"
- ukuina 3y agoVSCode's interface to Git is superior to Git-native. Adding the concept of Sync, simplifying branching, so many niceties.
- jazzyjackson 3y agoi'm going to have to try vscode again, is this Sync you mention something other than the usual sync'ing of the user profile? what does vscode do differently with branches?
- ukuina 3y agoA Sync is a push+pull, changing branches is graphical.
- from-nibly 3y agoyou can add the concept of sync yourself in git. [alias] sync = "git squash && git pull --rebase && git pop && git push" # YMMV Git GUIs are always changing the interface to git. It gives you this weird lockin and islandification. Knowing how to actually use git can give you super powers.
- DavidVoid 3y agoI'd be careful with that feature though because it used to have an absolutely awful bug (and it might still tbh, I haven't had it confirmed that it's fully fixed yet). https://github.com/microsoft/vscode/issues/96104 https://github.com/microsoft/vscode/issues/96104 If you staged selected ranges it could change the line endings of the whole file from CRLF to LF. It's very easy to miss since it doesn't show in the normal diff, but then you end up with merge conflicts galore later on.
- 3y ago
- poidos 3y agomagit with vim mode is awesome for this. just visually select the lines, commit, done. so good.
- petepete 3y agoI've used tig to do this for 15+ years. It's effortless. I know lazygit is more modern and has more features, but tig does this so well I have no need to change.
- collinvandyck76 3y agoI kept running into an issue with tig where it couldn't break apart a hunk, or otherwise couldn't stage a part of one. I never figured it out by the time I gave lazygit a shot. Now that I've used lg in anger I only wish I had invested in it sooner.
- mr_mitm 3y agoJust press `1` and you can commit single lines.
- icywonder 3y agogit-cola handles that nicely too.
- RheingoldRiver 3y agodoes anyone use Sublime Merge for this? do you like it?
- lsllc 3y agoYes! I’m pretty happy with Sublime Merge. It’s also cross platform (like Sublime Text) which is very handy.
- parentheses 3y agoThis 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.
- jraph 3y agoI customize hunks without Emacs and with git add -p. I use git rebase and git add/reset -p left and right. I will definitely checkout lazygit though, thank you and others in this HN submission for the mention, looks useful.
- hexfish 3y ago+1 for lazygit, and I say this as someone who has used git exclusively on the cli for years and still continues to do so. It's just that lazygit makes staging hunks, squashing and rebasing such a joy.
- grimgrin 3y agothe 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/fugitive.txt#L281-L299 https://github.com/tpope/vim-fugitive/blob/master/doc/fugiti...
- palata 3y agoI find `git add -p` easy enough. Maybe I learned not to address random stuff while working on a particular thing, though. But that's the beauty of it: git makes it easy for anyone to use their favourite tool (be it CLI, TUI or GUI) :-).
- gooseyard 3y agoI didn't know about git add -p when I started using magit and thought it was an amazing magit-specific feature and went around cluelessly telling everybody about it. Later when I was experimenting with a different editor I realized my folly, and have come to find that I prefer git add -p, since I often want to provide multiple paths or a glob etc when its time to add to the index. Something I found that I like about the approach is that sometimes while working on a change, some unrelated code will catch my eye that I want to fix but not as part of my next commit. Being free to make the edit, knowing that I'll see it again later when staging changes, is nice because I would often otherwise forget to go back and revisit the thing I saw. The git add -p sessions then wind up being an opportunity to decide what changes go into each commit, etc. I do burn myself occasionally by having an unrelated edit that is in a hunk that can't be split- I suspect there are tweaks to diff I could make to help with that but have yet to overcome my apathy wrt research.
- palata 3y ago> I do burn myself occasionally by having an unrelated edit that is in a hunk that can't be split- I suspect there are tweaks to diff I could make to help with that but have yet to overcome my apathy wrt research. `git add -p` allows you to "edit [e]" the patch, meaning that you can really decide exactly what should be added. And that's a great opportunity to learn how to manipulate patches, which is an important skill IMO!
- gooseyard 3y agowell im a dope, I never noticed the edit option. So if I edit the hunk and toggle the subset of changes I don't want, I'll get prompted to accept the diff of the diff? brb I need to go try this.
- pantalaimon 3y ago`git gui` seems to be the easiest solution to me. The interface is not pretty, but it does what one would expect and it’s fast.
- danhorner 3y agoI came here to say this. `git gui` is nearly ubiquitous and has saved me for years from learning how to use `git add -p`. I don't use many graphical git tools but its options "stage hunk" and "stage line" are more intuitive and fine-grained than the hunk-splitting in `git add -p`
- from-nibly 3y agoI'm a native git guy in general, but `git gui` has absolutely won my heart. I don't know what I'd do without it for staging partial commits. That and searching through stashes.
- zilti 3y agoWhy use git gui when you can use magit
- fransje26 3y agoBecause... != emacs?
- rk06 3y agogit etensions do a better job
- gomoboo 3y agoFor those in the JetBrains world, you can do this easily with changelists: https://www.jetbrains.com/help/idea/managing-changelists.html https://www.jetbrains.com/help/idea/managing-changelists.htm...
- jmaker 3y agoTotally, been doing it for a long time in IntelliJ and the other JetBrains IDEs, still stunning colleagues by simply check marking blocks or lines to commit the way it conveys hopefully the most meaning, so there is little if any dependence between the logical units across the commits, so as to be able to revert easily if necessary. And then the PR tells a story rather look like a frightening blob of shuffled ASCII chars.
- 000ooo000 3y agoAlso check out `git add --patch`'s cousins over in `git checkout` and `git reset`. I probably use reset --patch more than add, as I'll usually have a file full of relevant changes with only 1 hunk i want to 'unstage', so I'll stage the whole file and unstage the irrelevant hunk. I use `git checkout --patch` mostly when splitting a big commit into smaller commits, retroactively. I start an interactive rebase, chuck 'break' before the big commit, then I start pulling the changes 'forward' from the big commit into the working directory with `git checkout --patch big-commit-hash */path.ext`. Sometimes this works out easier than popping a commit off and re-committing everything seperately, particularly if I already have a commit with a detailed message.
- andrewshadura 3y agoTry git-crecord.
- amadeuspagel 3y agoVS Code: https://peakd.com/development/@themarkymark/how-to-do-a-partial-commit-with-vs-code https://peakd.com/development/@themarkymark/how-to-do-a-part...
- praash 3y agoI have used patch-adding almost exclusively for a few years. It really helps me stay aware of my workspace and avoid committing temporary tweaks. I can also keep semi-permanent personal tweaks uncommitted in my workspace and get constant reminders if I still have an inline debugging flag activated. It's unlikely to remember this feature's existence and benefit from it without habitual usage.
- lsllc 3y agoSublime Merge does this really well.