Live data from Hacker News

A Better Git Flow

render.com

11–20 of 115 posts

Re: A Better Git Flow

#11
I use lazygit[0] to essentially do the same thing. (or even vim-fugitive[1])

Previously I avoided creating messy commits simply because it was "tedious" to reorganize commits. And making overly atomic commits and typing out git commands more frequently didn't appeal to me either.

Once I got used to the above tools, life got so much easier. Lazygit makes it super easy to amend, reword, and even re-position commits in a TUI environment. Real life-changer for me. I can stage hunks super easily too, though that's even easier in neovim.

The only issue I face is my C-j/C-k keys are already bound to tmux, but are needed by lazygit to reposition commits.

[0]: https://github.com/jesseduffield/lazygit [1]: https://github.com/tpope/vim-fugitive

Re: A Better Git Flow

#12
post #10

This to me seems more like a logical separation than anything technical. I use GitHub and always squash commits before merging a PR. This keeps the commit log clean and also has the side effect that you have the PR number in the merge commit. Having said that I also suggest keeping PRs small. If you are going to reformat a code base, make that a separate commit. Updating a library, separate commit. Adding a library y…

+1 to keeping PRs small. This makes it much more logical to me. But I have worked at organizations where this is frowned upon...teams liked the PRs to be one logical unit/fix/improvement and component parts became frustrating or got merged at different times, creating the need for rework.

From reading a lot of feedback on this post one thing that stands out is, the best way to use git just depends on the context. But it doesn't hurt to have commands like this in your toolbox and know how to use the tool well. Plus we all have our private, icky antipatterns that we know we should improve, right?

Re: A Better Git Flow

#13
The use of `git reset` is a little silly and is playing with fire for no absolutely no benefit.

If you want to follow the pattern described here, create a new branch and do `git checkout the_feature_branch -- $filename` and pull the changes from the branch you did the work in into the new branch, making commits as described in the article. You can even do `git diff --numstat $feature_branch $new_branch` to see what has changed.

Re: A Better Git Flow

#14
post #10

This to me seems more like a logical separation than anything technical. I use GitHub and always squash commits before merging a PR. This keeps the commit log clean and also has the side effect that you have the PR number in the merge commit. Having said that I also suggest keeping PRs small. If you are going to reformat a code base, make that a separate commit. Updating a library, separate commit. Adding a library y…

I dislike the rule of "small PRs" because people tend to interpret it as "arbitrary number of lines changed", and end up breaking a bugfix or feature into many PRs that don't make sense to review or release in isolation. I interpret "small PRs" as being "PRs should be about one bugfix/feature".

Re: A Better Git Flow

#15
post #2

My two cents: I just always squash commits when merging to master/main. That way the main branch has clean commit messages and it's easy to revert later. I think cherry-picking commits after the fact can also be very time-consuming, especially if you do a lot of refactoring or "clean as you go" as I like to call it. However, I could see it being worth it for open-source.

When you go grocery shopping, you can A) create a list of dinner ideas and the ingredients that you need to pick up, group them in the order that they appear in the store, and head straight to the aisles to get the items. Or B) go in with the list in mind, but wander through the whole store and you may find something else interesting to pick up. Or C) not have any but the most vague dinner ideas, go without a list, and create a week's worth of dinners as you browse picking up interesting items and figuring out what to do with them.

All depends on if you want the most efficient approach, or if you want serendipity to strike.

Re: A Better Git Flow

#16
Or you can just use staging to bunch up your changes until you are ready for a logical commit....

the problem with your approach is that sometimes, changes are logically grouped but cross file.

Re: A Better Git Flow

#17
The reset part seem superfluous, most of this can be handled via rebasing. In fact, this is exactly what we do at .

Much like the thesis of this article, the goal is to have a set of well organized commits so that when it comes time to do a PR review, you have 1-3 logical units of change and it also improves the ability to do reverts.

But you can very easily do this just by rebasing instead of resetting and re-committing.

In particular, `git commit --fixup ` and `git commit --squash ` are extremely useful for this during the "WIP" stage as well as when handling PR comments, and these are things that I only learned about in the last 6 months. I would recommend doing a google search on "fixup commits" to learn more about them. I enjoyed this article: https://www.mikulskibartosz.name/git-fixup-explained/

Yes, rebasing is scary if you're new to git, but it does everything in this article in a way that's much cleaner and once you learn how to use it effectively you'll feel like you have super powers. It's worth taking the time to learn.

Re: A Better Git Flow

#18
I wrote a similar suggestion in 2011: https://sandofsky.com/workflow/git-workflow/

As far as I could tell, this was the recommended workflow from the earliest users of git. Look at the Linux kernel's public history.

When git gained widespread adoption, it was sold as "Subversion, but with cheap branching." People don't realize that this power requires discipline, otherwise you end up with complicated history that makes change management a nightmare.

In code review systems like Gerrit, you have to approve every single commit, and you can also enforce rules like "fast-forward merges only." It sure makes Github's pull-request model feel like an anti pattern.

Re: A Better Git Flow

#19
I highly recommend tig because it lets you easily stage each line/change separately. Together with interactive rebase and fixup commits, it becomes super easy to group changes where they belong. For fixup commits with tig (in main view go to the commit to fixup and press '='), my .gitconfig has:

    [tig "bind"]
        main = = !git commit --fixup=%(commit)

Re: A Better Git Flow

#20
post #17

The reset part seem superfluous, most of this can be handled via rebasing. In fact, this is exactly what we do at . Much like the thesis of this article, the goal is to have a set of well organized commits so that when it comes time to do a PR review, you have 1-3 logical units of change and it also improves the ability to do reverts. But you can very easily do this just by rebasing instead of resetting and re-commit…

Right, I don't think rebasing is that daunting if you take a few hours to practice. It's time well spent.

Sometimes you can even create empty commits (--allow-empty) up-front (if you already know about all the things that have to be changed) and fill them via fixup/rebase --autosquash

Post reply on HN