git操做及分支策略

GIT

1.基本操做[平常工做中經常使用操做]
2.分支策略[工做中GIT分支管理規範]
3.管理腳本[常規操做提煉]
4.參考資料及教程[網上優秀文檔資料]

基本操做

經常使用命令/常規操做
大部分場景下只須要熟悉下面的命令便可
  • 配置用戶名,Email地址
git config --global user.name "biby"
git config --global user.email "zhangbibin@tapinpet.com"
  • 建立版本庫,初始化GIT倉庫並提交遠程倉庫
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 checkout -- <file>能夠丟棄工做區的修改

// 暫存區
//用命令git reset HEAD <file> 或者 git reset -- 能夠把暫存區的修改撤銷掉  從新放回工做區
git reset HEAD <file>
git reset -- file
  • 日誌
    顯示從最近到最遠的提交日誌
    已經提交了不合適的修改到版本庫時,想要撤銷本次提交[版本回退]
    在Git中,用HEAD表示當前版本,上一個版本就是HEAD^,上上一個版本就是HEAD^^,再往上的版本用格式:HEAD~100,表示往上100個版本
git log
git log --pretty=oneline//格式化 comimt id && commit 提交說明
git reset --hard HEAD^
 git reset --hard <commit id> //指定回退到某個版本
git reflog //記錄每一次命令
  • 衝突解決
    推薦使用PhpStorm/WebStorm等IDE自帶工具 Resolve Conflicts
  • stash
    工做進行到一半沒法提交須要修改其餘緊急問題時使用
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
擴展操做
  • 列出全部遠程分支及最後commit時間並按時間排序
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

一些管理腳本

建立release分支併合並分支
可優化:分支切換失敗 合併失敗等異常處理
#!/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

相關文章
相關標籤/搜索