Git tricks

February 27, 2019

Tips

From twitter.com/zeuxcg

git rebase —autostash stashes and unstashes local changes for you

git commit -v adds diffs to commit draft so that you can inspect them as you write commit msg

git rebase -i Also, you should configure autostash to be on by default, and configure rebase to be default on pull. Pull without -r will screw you every time!

You probably know already but commit messages allow to autosquash commits. fixup! <hash>, squash! <hash> Works best with git rebase —interactive —autostash —autosquash or simply git commit --fixup <hash> (or git commit --squash <hash>) … easier than typing the relevant commit msgs.

I didn’t know those. Thanks. I have 3 aliases to avoid typing: git fixup <hash> git squash <hash> git oops. git oops creates a fixup commit to the current HEAD commit. I use it when I don’t want to amend to keep the history until a later rebase. Used countless times already.

git config rebase.autostash true git config pull.rebase true git add —patch git gui (allows to stage by line)

I’m using git add -i, it’s a superset of -p, allowing to revert changes and add untracked files as well. Oh and git checkout -p, the destructive inverse of add -p.

Misc

git rebase has --committer-date-is-author-date

git rebase -i <commit-to-rebase-on-top-of>
# set all to "edit"
git commit --amend --date=$(date -I) --no-edit
git rebase --continue
# finally
git rebase <commit-to-rebase-on-top-of> --committer-date-is-author-date

split to new folder(not subtree):
from GitHub help

git filter-branch --prune-empty --subdirectory-filter \
  FOLDER-NAME  BRANCH-NAME

cherry-pick commit while ignoring line endings:
e.g. after dos2unix runs

git cherry-pick -X ignore-all-space <commit-id>

Rewrite commit date to author date while rebasing

git filter-branch \
    --env-filter 'export GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"' \
    <SHA>..HEAD

Find commits in all branches that touch a path

git log --all -- <path>`

Find commits in all branches that contain message

git log --all --grep <msg-or-regex>`

Find code in all branches that matches regex

git grep <regex> $(git rev-list --all)

Might be slow though!