git
config
$ git config --global merge.ff false # defaults to no-fast-forward merge
$ git config user.email "<email>" # set author email for current repo only
hooks
Add tag to last commit with version found in __init__.py:
.git/hooks/post-commit
#!/bin/bash
INIT_FILE="./__init__.py"
if [ ! -f "$INIT_FILE" ]; then
exit 0
fi
VERSION=$(grep -oP '(?<=version__ = ")[^"]+' "$INIT_FILE")
if [ -z "$VERSION" ]; then
echo "Version not found in $INIT_FILE"
exit 0
fi
TAG="v$VERSION"
if git rev-parse "$TAG" >/dev/null 2>&1; then
echo "Tag $TAG already exists. Exiting."
exit 0
fi
git tag "$TAG"
echo "Tag added: $TAG"
usage
cancel last changes
$ git reset --hard ORIG_HEAD # cancel last commit/merge (forget local changes since n-1 commit)
search
check history for 1 file:
git log -p <FILE>
search changes:
git log -L start_line,end_line:<FILE>
git log -S "string" <FILE>
git blame -L start_line,end_line:<FILE>
gitk
: find
then select adding/removing string
stashing:
git stash -m "msg"
git checkout <BRANCH>
git checkout <ORIGIN_BRANCH> # optional: stash can be applied anywhere
git stash list
git stash apply stash@{n} # greater n means older
git stash clear
gitlab
github
Create a Pull Request
- fork repo
- clone repo from fork
- check if there is a secondary branch for dev
- make changes, commit (or squash if multiples commits, with rebase)
- push to fork
- create PR
- when PR approved, pull from upstream and edit docs if needed