Git
Usage
bash
# switch branch
git switch other-branch
# set a commit as HEAD
git reset --hard <commit_id>
# create local branch from remote branch
git checkout -b test <name of remote>/test
# force push
git push origin <your_branch_name> --force
# visualize lineage
git log --graph --oneline --all
# compare tips of different branches
git diff branch1..branch2
# squash last N commits
git rebase -i HEAD~N
# Verifies the connectivity and validity of the objects in the database
git fsck
# show staged changes
git diff --cached
# clone specific branch with only latest revision
git clone --depth <depth> -b <branch> <repo_url>
# resign previous commits
git rebase $COMMIT_HASH^ --exec "git commit --amend --no-edit -S"
# worktree
git worktree add ../hotfix-workspace -b hotfix-bug mainTime Travel
bash
# set content to <another branch> in <current branch>
git restore --source=<target-branch-name> --staged --worktree .
# checkout file from another branch
git checkout $REVISION -- $FILENAME
# git restore: <https://martinheinz.dev/blog/109>
# Unstage changes made to a file, same as "git reset some-file.py"
git restore --staged some-file.py
# Unstage and discard changes made to a file, same as "git checkout some-file.py"
git restore --staged --worktree some-file.py
# Revert a file to some previous commit, same as "git reset commit -- some-file.py"
git restore --source HEAD~2 some-file.pySubmodule
bash
# add submodule
git submodule add [email protected]:kahnwong/hugo-PaperMod.git themes/PaperMod
git submodule update --init --recursive
# update submodule
git submodule foreach git pull origin master
# remove submodule: https://stackoverflow.com/questions/1260748/how-do-i-remove-a-submodule
git rm <path-to-submodule>
rm -rf .git/modules/<path-to-submodule>
git config --remove-section submodule.<path-to-submodule>.Cookbook
List untracked files
git status --ignored OR git clean -ndX
bash
$ git help clean
git-clean - Remove untracked files from the working tree
-n, --dry-run - Don't actually remove anything, just show what would be done.
-d - Remove untracked directories in addition to untracked files.
-X - Remove only files ignored by Git.Get total additions and deletions on a given branch for an given author in git
bash
git log --author=$USER --shortstat $BRANCH | \
awk '/^ [0-9]/ { f += $1; i += $4; d += $6 } \
END { printf("%d files changed, %d insertions(+), %d deletions(-)", f, i, d) }'Export git log to CSV
https://git-scm.com/docs/pretty-formats
bash
echo sha, contributor, date, message > log.csv
git log --date=local --pretty=format:'%h, %an, %ad, "%s"' >> log.csvShallow clone with fast push
bash
git clone $repo --depth 2- git-cliff - A highly customizable changelog generator ⛰️
- git-filter-repo - Quickly rewrite git repository history (filter-branch replacement).
- git-summary - Summarizes multiple git repository status within a directory.
- git-xargs - git-xargs is a command-line tool (CLI) for making updates across multiple Github repositories with a single command.
- gitmal - A static page generator for repos
- gource - Software projects are displayed by Gource as an animated tree with the root directory of the project at its centre. Directories appear as branches with files as leaves. Developers can be seen working on the tree at the times they contributed to the project.
Visualize
Notes: use python3.7
bash
docker run \
--rm -v $(pwd):/repo \
felix/gitinspector:0.4.4 \
--format=html \
--timeline=TRUE > stats.htmlRemove large files from git history
https://stackoverflow.com/a/61602985
bash
wget https://raw.githubusercontent.com/newren/git-filter-repo/main/git-filter-repo
python3 git-filter-repo --analyze
python3 git-filter-repo --invert-paths --path-match $PATHResources
General
Development flow
Code Review
- How to Write a Git Commit Message
- Tuple’s Pair Programming Guide
- Conventional Commits - A specification for adding human and machine readable meaning to commit messages