Refer

Reset commits

Using rebase with exec option.

Refer

Lang: sh
# check if git user info is correct and then use this
git rebase -r <some commit before your bad commits> \
  --exec 'git commit --amend --no-edit --reset-author'

Git log formats

Git log to excel sheet table format:

Lang: sh
$ git log --pretty=format:"%h%x09%cn%x09%ce%x09%cd%x09%an%x09%ae%x09%ad%x09%s"
  --date=format:"%Y/%m/%d%t%T"

#git log pretty format table headers
#Commit id	Committer name	Committer email	Commit date	Commit time
#Author name	Author email	Author date	Author time	Commit message

Git cherry-pick use cases

Merge a range of commits from one branch to another:

Lang: sh
$ git rev-list --reverse --no-merges PERM_BRANCH_1..feature/ABC | git\
  cherry-pick -x --stdin
Lang: sh
# disable these options for git ps1 to speed up prompt in large git repos such
# as linux kernel
git config --local bash.showDirtyState false
git config --local bash.showUntrackedFiles false

Git format-patch

Lang: sh
git format-patch origin/$(git_current_branch).. -o /tmp/
sd 'Mani Kumar D A <mkdev@job.com>' 'Mani Kumar <manid2.work@gmail.com>' \
  /tmp/*.patch
git am /tmp/*.patch --reject

git-filter-repo

Use newren/git-filter-repo to quickly rewrite git repository history.

Move a sub-directory to another repository with history

Lang: bash
# 1. Make clone of the original repository 'repo1'
git clone --no-local /path/to/repo1 /tmp/repo1

# 2. Re-write history to contain only the sub-directory
cd /tmp/repo1

git filter-repo --path dir1/sdir1/ --path-rename dir1/sdir1/:'' \
--commit-callback '
if not commit.message.endswith(b"\n"):
  commit.message += b"\n"
commit.message += b"Original-Commit:\n\n"
commit.message += b"  repo1@%s" % commit.original_id
'

# 3. Fetch this re-written history into 'repo2'
cd /path/to/repo2

git fetch /tmp/repo1
git merge --allow-unrelated-histories FETCH_HEAD

git-lfs

Git LFS is a system for managing and versioning large files in association with a Git repository.

pull a large file

Lang: bash
git lfs pull --include "archives/busybox-1.31.1.tar.bz2"

git_ps1

Disable these git_ps1 features per repo for faster git prompt when the repository is large e.g. linux kernel and slows shell prompt:

Lang: bash
git config bash.showDirtyState false
git config bash.showUntrackedFiles false