Git 特定操作 example

來源:互聯網
上載者:User

標籤:match   ocs   https   www   dde   lin   radius   split   撤銷   

關於Git的使用,《一個小時學會Git》介紹的極其詳細,且通俗易懂,所以相比於提供官網教程,個人覺得這篇部落格更適合入門~

這裡貼個圖,區別下常用操作:

Git Examples從倉庫中拆分子目錄為獨立項目

比如你有一個叫做 big-project 的倉庫,目錄如下:

big-project ├── codes-a ├── codes-b └── codes-eiyo

如何把 codes-eiyo 拆出來做為一個獨立倉庫?又如何把 codes-eiyo 清理掉,只保留剩下的代碼在倉庫中?

function split_to_independence(){sub_dir=$1test -z $2 && tmp_branch="tmp_"${sub_dir} || tmp_branch=$2test -z $3 && path_new=${tmp_branch} || path_new=$3path_old=$(pwd)    # 將 sub_dir 目錄下的所有提交整理為新的分支    git subtree split -P ${sub_dir} -b ${tmp_branch}    # 初始化新倉庫    if [ -d ${path_new} ]; then        echo "There has been a same-name dir. Make sure it‘s empty?"    else        mkdir ${path_new}        git init ${path_new}    fi    # 拉取原倉庫的 tmp_branch 分支到 path_new 的 master 分支    cd ${path_new}    git pull ${path_old} ${tmp_branch}    # 清理原項目中的臨時分支    cd ${path_old}    git branch -D ${tmp_branch}}
從倉庫中清理子目錄 commit 痕迹

情景同上,只是變為:從當前commit列表中清除關於 sub-dir 的所有記錄。

function clean_up_subdir(){sub_dir=$1current_branch=$(git symbolic-ref --short HEAD)  # $(git branch | grep ‘*‘ | awk ‘{print $2}‘)# there are some interesting ways:# git symbolic-ref --short HEAD# git describe --contains --all HEADtest -z $2 && target_branch=${current_branch} || target_branch=$2    # 重寫提交記錄,且不保留空提交    git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch ${sub_dir}" --prune-empty ${target_branch}    # 前步操作會清理 sub_dir 中所有已提交檔案,但會忽略垃圾檔案及檔案夾    # rm -rf ${sub_dir}}

 

 

 

 

下面提供一個常用命令查詢列表,畢竟年紀大了,記性不好~

Git 倉庫配置忽略列表: [.gitignore]

 

  • *.[oa]    # 忽略所有以 .o 或 .a 結尾的檔案
  • *~         # 忽略所有以波浪符 ~ 結尾的檔案
  • *.tmp
  • build/
  • test/
  • !test/save    # 忽略 test/ 目錄但不忽略 test/save 檔案

 

Git 檔案操作查看檔案狀態
git status -s

 

    • ‘‘ = unmodified
    • M = modified
    • A = added
    • D = deleted
    • R = renamed
    • C = copied
    • U = updated but unmerged
撤銷 add 操作
# 從stage中刪除檔案,工作區則保留物理檔案(針對untracked files)git rm --cached <file># 移除所有未追蹤檔案(針對untracked files)# 常用參數-df,-d表示包含目錄,-f表示強制清除。git clean [options]# 針對 modified files,stage區被重寫,但不影響工作區git reset HEAD <file>...

 

Git 核彈級選項 filter-branch

更多使用樣本,參考官網.

 

Git 特定操作 example

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.