Git Tips
Git style
For Git we follows this guides
When you move large number of files, or run linters, that should be in separate commit (or even separate pull request) and put the command in commit message
git mv test/fixtures spec
git commit -am"git mv test/fixtures spec"
# or
standardrb --fix spec
git commit -am"standardrb --fix spec"
Code review
For code review
When working on some task, for example issue 123 than we usually create a branch
that looks like i#{issue number}_#{description}
and use commit number at the
end git commit -m'#{some description} ##{issue number}
For example
git checkout -b i123_add_test_for_signup
# make changes
git commit -m'Add test for signup #123`
git push -u origin HEAD
Git hooks
We use git hook for commit message so the issue number is automatically added to commit message.
cat > .git/hooks/prepare-commit-msg << 'HERE_DOC'
#!/bin/bash
#
# Source https://dev.trk.in.rs/2023/03/21/git-tips#git-hooks
#
# https://gist.github.com/jimschubert/9073276
# Automatically adds branch name to every commit message.
# Modified from the stackoverflow answer here: http://stackoverflow.com/a/11524807/151445
#
# Get branch name, use sed to strip name and keep only numbers
# i123_my_branch_name -> 123
issueNumber=$(git branch | grep '*' | grep -o '[0-9]*')
# issueNumber=$(git branch --show-current)
# Get the first line, ex: from ammending or mmm from git commit -am'mmm'
firstLine=$(head -n1 $1)
# echo firstLine=$firstLine issueNumber=$issueNumber
if [[ -n $issueNumber && ! $firstLine =~ $issueNumber ]] ; then
# Prepend issueNumber to COMMIT_MSG
# echo "i#$issueNumber"' '$(cat "$1"| sed '/^#.*/d') > "$1"
#
# Append issueNumber to COMMIT_MSG
# echo -n "#$issueNumber" >> "$1" # this will add at the end
echo $(cat "$1" | sed '/^#.*/d')' '"#$issueNumber" > "$1"
# Insert issueNumber at the end of the commit message file
# sed -i "1s@^@\n\n$issueNumber@" $1
fi
HERE_DOC
chmod +x .git/hooks/prepare-commit-msg
Next time you make a commit on a branch i123_add_test_for_signup
than commit
message will include [123]
git commit -am"Add test for signup"
[#123] Add test for signup
Gitflow
For big projects we follow GitFlow
so all PR are done on develop
branch. Only i123_hotfix_something
branch
start from main
and merged back to develop
and main
.
Nice tutorial:
Github cli
We also use gh cli
# create PR and put commit messages as comment
gh pr create --fill
# assign reviewers
gh pr edit
# rebase and delete local and remote branch
gh pr merge -r -d
Remove all branch on which I’m not the author
#!/bin/bash
current_user=$(git config user.email)
# Remove all local branches on which I'm not the author of the tip
for branch in $(git branch --format='%(refname:short)'); do
if [[ "$branch" =~ ^(main|develop|master)$ ]]; then
continue
fi
author=$(git log -1 --format='%ae' $branch)
if [ "$author" != "$current_user" ]; then
echo removing -$branch- $author
git branch -D $branch
echo
fi
done