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>
or better, use 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
worktree
Duplicates a repo but shares the same .git/ (within legacy directory).
With clone there is a full duplication (also .git/) and full independance.
git worktree ../worktree/new_feature
# then work in new directory, commit
# to merge with legacy directory, cd in
git cherry-pick <feature-commit-hash>
# or to preserve all history from new_feature changes
git merge new_feature
git worktree remove ../worktree/new_feature
# if a commit has been performed from worktree, a branch remains
git branch -a
git branch -d new_feature
cherry-pick
git cherry-pick <commit-hash>
Integrate one (or multiple) commit. If feature branch has commit D, E, F, it's possible to only integrate changes at commit E into the main branch.
Another difference with merge is merge copies whole history (all commits) from feature branch.
Usefull with worktree pattern for instance.
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