重寫(xiě)歷史
把新的改動(dòng)加入上一個(gè)commit: git commit --amend
壓縮前n個(gè)commit: git rebase --i HEAD~N
這個(gè)真心好好用
修改某一個(gè)過(guò)去的commit:git rebase --i COMMIT_HASH^ // change pick to edit
詳見(jiàn):
- 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
簡(jiǎn)單說(shuō)林螃,git flow是一個(gè)團(tuán)隊(duì)與產(chǎn)品使用git合作的流程双泪。master永遠(yuǎn)是stable code,開(kāi)發(fā)永遠(yuǎn)發(fā)生在develop西设,每一個(gè)新的feature都有自己的feature/[name-of-feature] branch,review完merge回develop记靡。每一個(gè)新的release都從develop上cut出來(lái)说庭∮8龋可以單獨(dú)修修,然后用git tag來(lái)version完之后publish成release再merge回master齐饮。hotfix分別在每個(gè)release上面開(kāi)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] - 將那些改動(dòng)放入新的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
最近犯了個(gè)錯(cuò)誤祖驱,沒(méi)有看清楚最新一個(gè)release和上一個(gè)release之間的所有commit握恳。為了避免這個(gè)錯(cuò)誤,有適用于git flow的另一個(gè)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]
都會(huì)被問(wèn)一次乡洼,是不是真的要merge這些commit。