重写历史
把新的改动加入上一个commit: git commit --amend
压缩前n个commit: git rebase --i HEAD~N
这个真心好好用
修改某一个过去的commit:git rebase --i COMMIT_HASH^ // change pick to edit
详见:
- https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History
- https://blog.jacius.info/2008/06/22/git-tip-fix-a-mistake-in-a-previous-commit/
Git Flow
简单说,git flow是一个团队与产品使用git合作的流程。master永远是stable code,开发永远发生在develop,每一个新的feature都有自己的feature/[name-of-feature] branch,review完merge回develop。每一个新的release都从develop上cut出来。可以单独修修,然后用git tag来version完之后publish成release再merge回master。hotfix分别在每个release上面开branch,不需要牵扯到develop或master。
git flow init
git checkout develop
git pull origin develop
git flow release start <version>
git flow release finish # ensure you have no untracked files in your tree
./gradlew distTar
其他比较有用的指令
git stash - 保存你的unstaged changes
git stash branch [branchname] - 将那些改动放入新的branch
git clean -fd [--dry-run] - 删掉所有的uncommitted change
dry-run gives you a preview and then you can actually run it.
pre-commit
避免commit console.log
的pre-commit hook:
#!/bin/sh
count=`git diff HEAD | grep '+.*console\.log' | wc -l | awk '{print $1}'`
if [[ "$count" -ge 1 ]]; then
echo "Please remove console.log() statements from your changes before committing."
echo "Refusing to commit changes."
git diff HEAD | grep '+.*console\.log'
exit 1
fi
prepare-commit-msg
最近犯了个错误,没有看清楚最新一个release和上一个release之间的所有commit。为了避免这个错误,有适用于git flow的另一个prepare-commit-msg的git hook:
#!/bin/sh
# Pre-merge hook that lists all commits and asks you if you really want to
# merge them in. For release branches only.
case $2 in
merge)
branch=$(grep -o "release/[0-9]\.[0-9]" $1)
if [[ ! -z $branch ]]
then
echo "Merging the following commits from branch $branch:"
git log ..$branch
exec < /dev/tty
while true; do
read -p "Are you sure you want to merge? (Y/n) " yn
if [ "$yn" = "" ]; then
yn='Y'
fi
case $yn in
[Yy] ) exit;;
[Nn] ) exit 1;;
* ) echo "Please answer y or n for yes or no.";;
esac
done
exec <&-
fi
;;
esac
酱紫的话,每次git flow release finish [myversion]
都会被问一次,是不是真的要merge这些commit。