Git
Checkout
git clone without downloading
git clone <repo> --no-checkout
check what are the branches
git fetch && git branch
force sync from origin
git reset --hard origin/main
pull main without checking out main
git fetch origin main:main
fetch only 1 commit
git fetch --depth 1 git@github.com:username/repo.git FULL_SHA_FOR_COMMIT
Config
sane config
git config --global pull.rebase true # rebase on pull conflict
git config --global rebase.autoStash true # stash before rebase
git config --global submodule.recurse true # always recur submodule
Commit
commit with message
git commit -m "<msg>"
stage all changes and commit
git commit -am "<msg>"
revert to the last commit
git reset HEAD~
or
git revert …
change the last commit
--amend
create orphan branch
git switch --orphan <branch>
Bookkeeping
delete all history of a certain file (deprecated)
FILTER_BRANCH_SQUELCH_WARNING=1 git filter-branch --index-filter \
'git rm -rf --cached --ignore-unmatch <path_to_file>' HEAD
delete all history of a certain file using git-filter-repo
git filter-repo --invert-paths --path '<path_to_file>' --use-base-name
ignore all symlink
find * -type l -not -exec grep -q "^{}$" .gitignore \; -print >> .gitignore
git commit sizes
bash -c "git rev-list --objects --all |
git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' |
sed -n 's/^blob //p' |
sort --numeric-sort --key=2 |
cut -c 1-12,41- |
$(command -v gnumfmt || echo numfmt) --field=2 --to=iec-i --suffix=B --padding=7 --round=nearest"
verify no change since commit
git diff --exit-code HEAD
remove uncommitted files
git clean -f
remove ignored files
git clean -fX
go back and clean up history
git rebase -i COMMIT_BEFORE_CHANGES
Multiple repo
add repo as submodule
git submodule add <repo>
pull every repo under the current folder
fd .git -H -t d -x git --git-dir={} pull
fetch and status every repo under the current folder
fd .git -H -t d -x git --git-dir={} fetch \; -x git --git-dir={} --work-tree {}/.. status
Multiple origin
push all branch to all remote
git remote | xargs -L1 git push --all
push main to all remote
git remote | xargs -L1 -I R git push R main