1.基本操做[平常工做中經常使用操做] 2.分支策略[工做中GIT分支管理規範] 3.管理腳本[常規操做提煉] 4.參考資料及教程[網上優秀文檔資料]
大部分場景下只須要熟悉下面的命令便可
git config --global user.name "biby" git config --global user.email "zhangbibin@tapinpet.com"
mkdir biby_tapin cd biby_tapin git init git remote add origin {遠程倉庫地址} git remote -v //覈實遠程倉庫URL
// clone 遠程分支 git clone // 添加到倉庫[添加到暫存區] git add -m 提交說明 // 提交到倉庫[把暫存區的全部內容提交到當前分支] git commit // 拉取 git pull // 推送 git push // 工做區狀態 git status // 切換分支 git checkout // 查看全部本地分支 git branch // 查看素有遠程分支 git branch -a // 查看工做區和版本庫裏面最新版本的區別 git diff <file>
熟悉這部分操做能夠更好的使用GIT
// 刪除本地分支 git branch -d feature_biby_xxx git branch -D feature_biby_xxx //強制刪除,當分支有修改未提交到主分支時須要使用強制刪除參數才能刪除 // 刪除遠程分支 git push origin :feature_biby_xxx
git checkout -b develop origin/master //git checkout命令加上-b參數表示建立並切換 至關於如下兩條命令 git branch develop git checkout develop
// 工做區 git checkout -- <file>能夠丟棄工做區的修改 // 暫存區 //用命令git reset HEAD <file> 或者 git reset -- 能夠把暫存區的修改撤銷掉 從新放回工做區 git reset HEAD <file> git reset -- file
git log git log --pretty=oneline//格式化 comimt id && commit 提交說明 git reset --hard HEAD^ git reset --hard <commit id> //指定回退到某個版本 git reflog //記錄每一次命令
git stash //把當前工做現場「儲藏」起來 git stash list //存儲列表 git stash apply //恢復存儲 git stash drop //刪除存儲 git stash pop //恢復同時刪除存儲 git stash apply stash@{0} //指定恢復stash
// 兩個版本間全部變動的文件列表 git diff --name-status HEAD~2 HEAD~3 // 兩個分支之間修改的文件 git diff feature_biby_01 feature_biby_02 --stat
for branch in `git branch -r | grep -v HEAD`;do echo -e `git show --format="%ci %cr" $branch | head -n 1` \\t$branch; done | sort -r
享受GIT帶來方便的同時,若是不加註意,極可能會留下一個枝節蔓生、四處開放的版本庫,處處都是分支,徹底看不出主幹發展的脈絡。(摘自阮一峯博客)
(摘自阮一峯博客)html
遵循分支策略的好處是,使得版本庫的演進保持簡潔,主幹清晰,各個分支各司其職、層次分明git
1.gitflow 的流程api
可優化:分支切換失敗 合併失敗等異常處理
#!/bin/bash echo "請選擇發佈分支" read release_name release=$(git branch | grep $release_name) if [ -n "$release" ]; then echo "發佈分支存在..." else git checkout -b $release_name origin/master echo "發佈分支建立完成..." fi echo "請選擇合併分支" read branch_name branch=$(git branch | grep $branch_name) if [ -n "$branch" ]; then echo "合併分支存在,更新分支..." git pull origin $branch_name else echo "合併分支不存在,拉取遠程分支..." git fetch origin $branch_name git checkout $branch_name git pull origin $branch_name git checkout $release_name fi echo "合併分支..." git merge $branch_name echo "合併分支完成..."
#!/bin/bash echo "請選擇刪除分支" read branch_name del_branch_name=$(git branch | grep -w $branch_name) if [ -z "$branch_name" ]; then echo "本地分支不存在" else git branch -d $branch_name > /home/biby/www/haiji/git_operate.log fi del_origin_branch_name=$(git branch -a | grep -w $branch_name) if [ -z "$del_origin_branch_name" ]; then echo "遠程分支不存在" else git push origin :$branch_name > /home/biby/www/haiji/gitt_operate.log fi echo "刪除分支完成..."
#!/bin/bash for branch in `git for-each-ref --sort=committerdate --format='%(HEAD) %(refname:short)-%(committerdate:raw)'`; do branch_name=$(echo $branch | cut -d "-" -f 1); branch_last_commit_time=$(echo $branch | cut -d "-" -f 2); str="+08" result=$(echo $branch_last_commit_time | grep "${str}") if [ "$result" != "" ]; then continue fi #當前時間戳 timestamp=$(date +%s) branch_last_commit_time=$(echo $branch_last_commit_time | sed 's/ //g') timestamp=$(echo $timestamp | sed 's/ //g') let branch_last_commimt_time_to_second=$timestamp-$branch_last_commit_time #半年大約是 15552000 秒 #五個月大約是 12960000 秒 #三個月大約是 7776000 秒 clear_time=7776000 if [ "$branch_last_commimt_time_to_second" -gt "$clear_time" ]; then branch_name=${branch_name##*/} #從變量branch_name的開頭,刪除最長匹配*/的子串 echo $branch_name; del_branch_name=$(git branch -a | grep -w $branch_name) if [ -z "$del_branch_name" ]; then echo "本地分支不存在" else git branch -d $branch_name > /home/biby/www/haiji/git_operate.log fi del_origin_branch_name=$(git branch -a | grep -w $branch_name) if [ -z "$del_origin_branch_name" ]; then echo "遠程分支不存在" else git push origin :$branch_name > /home/biby/www/haiji/git_operate.log fi echo "刪除分支完成..." fi done
1.阮一峯_GIT分支管理策略
2.廖雪峯GIT教程bash