by jawns · 4y ago · 0 comments
When I'm developing, but before I create a PR, I'll create a bunch of stream-of-consciousness commits. This is stuff like "Fix typo" or "Minor formatting changes" mixed in with actual functional changes.
Right before I create the PR, or push up a shared branch, I do an interactive rebase (git rebase -i).
This allows me to organize my commits. I can squash commits, amend commits, move commits around, rewrite the commit messages, etc.
Eventually I end up with the 2-4 clean commits that your coworkers have. Often I design my commits around "cherry-pick" suitability. The commit might not be able to stand on its own in a PR, but does it represent some reasonably contained portion of the work that could be cherry-picked onto another branch if needed?
Granted, all of the advice above requires you to adhere to a "prefer rebase over merge" workflow, and that has some potential pitfalls, e.g. you need to be aware of the Golden Rule of Rebasing:
https://www.atlassian.com/git/tutorials/merging-vs-rebasing#...
But I vastly prefer this workflow to both "merge only," where you can never get rid of those stream-of-consciousness commits, and "squash everything," where every PR ends up with a single commit, even if it would be more useful to have multiple commits that could be potentially cherry-picked.
(This works for me because auditing commit history is not important where I work, if it were I would organize commits better.)
*glances and $corp git repo and sees 'updates' 'fix' 'updates'. Sigh.
edit: googled, "Never rebase while on a public branch" i.e. a shared branch
Such an annoying mess it leaves otherwise. And CI is building 'merge branch master', on the master branch, great.
How does one set this up in GitHub actions?
I see some people whose projects (Furnace Tracker, PipeWire, previously Famitudio) seem to make progress very quickly without getting noticeably slowed down by technical debt, despite sloppy programming and unorganized commit logs (push-to-head). Meanwhile I move slowly, dread reviewing hundreds of lines of my own code, and produce technical debt (regrets) anyway, not as many surface-level lintable errors but plenty of entrenched mistakes. I wish I could move faster, but instead struggle to make progress.
Only the keybindings are a bit weird if you're not accustomed to Vim bindings:
- Open tig
- Change into the staging view with `s`
- Select your file using the arrow or `j` and `k` keys
- Press Return to show the diff
- Navigate to the line(s) in question with `j` and `k` (arrow keys will switch files)
- Stage parts with `1` (single line), `2` (chunk parts), `u` (chunks) or split chunks with `\`
- "Leave" the diff with `q`
- You can find the keybindings with `h` in the help screen, which also uses Vim keys -- like manpages usually do
"c" is staging view.
> - Stage parts with `1` (single line), `2` (chunk parts), `u` (chunks) or split chunks with `\`
You can also revert a chunk or file by using "!". Sometimes this is very useful.
1. I `git checkout PUBLIC -b CLEANUP` to a new branch.
2. Do a `git difftool CHANGES`, which opens each changed file in vimdiff one at a time.
3. For each file, I use :diffput/:diffget or just edit in changes I want.
4. Commit these changes on the CLEANUP branch.
5. Use `git difftool CHANGES` again to see the remaining diff.
6. Repeat until the diff comes back empty!
My unstructured changes tend to contain a handful of small typo fixes, white spacing, localized refactors, and 1 or 2 larger refactors and a behavioral change. Once they're all broken out, It's usually easy enough to use `git rebase -i` and reorder the smaller changes first, put out PRs for just those first, etc.
I've tried most Git clients on Mac over the years and kept gravitating back to Sourcetree.
I only tend to use it for this particular workflow (picking out very granular changes on a line-by-line basis). Otherwise, 90% of my git stuff is via IDE integrations or command line.
Edit: looks like it's back from the dead again :) https://github.com/gitx/gitx
Additionally, git itself comes with a simple `git gui` command that allows you to do partial commits on a line by line basis. It also has a nice "amend last commit" mode.
> git gui
git: 'gui' is not a git command. See 'git --help'.
The most similar commands are
gc
grep
init
pull
push