I came across this talk a little while back with some great tips for Git (YouTube). I’m a sucker for Git tips (see also Vim). Here are a few of the ones that jumped out at me.
git log -L
The -L
flag (which also works with
git blame
) will limit the commits shown to just the
specified lines, or even some function in the file. So
git log -L 15,26:path/to/file
will show only commits
where any of the lines between line 15 and 26 have been modified in
the file. Similarly
git log -L :funcname:path/to/file
will show only commits
where changes to the function or class listed.
git branch --column
This is a nice flag that tells Git to list the branches in multiple columns, instead of in a single column. Might save you some scrolling.
git config --global branch.sort -committerdate
I also hadn’t realized that you can change the sort order for branches. The above config will cause Git to list branches with the most recently modified branches first.
git push --force-with-lease
This is, I think, a better behavior for force pushing. Git will
attempt to force push, but unlike plain --force
it will
fail if the remote ref is not what it expects. In a nutshell this
means if someone else has pushed to main and then you try to force
push over that, Git won’t let you. If you’re in the habit of rebasing
to clean things up, this is a nice flag to ensure you don’t clobber
someone else’s work.
git maintenance start
This command will add some options to your project’s
.git/config
that tells Git to run some maintenance tasks
as cron
jobs once an hour to keep your repository nice
and speedy.