关于Git的使用,《一个小时学会Git》介绍的极其详细,且通俗易懂,所以相比于提供官网教程,个人觉得这篇博客更适合入门~
这里贴个图,区别下常用操作:
Git Examples
从仓库中拆分子目录为独立项目
比如你有一个叫做 big-project 的仓库,目录如下:
big-project
├── codes-a
├── codes-b
└── codes-eiyo
如何把 codes-eiyo
拆出来做为一个独立仓库?又如何把 codes-eiyo
清理掉,只保留剩下的代码在仓库中?
function split_to_independence(){ sub_dir=$1 test -z $2 && tmp_branch="tmp_"${sub_dir} || tmp_branch=$2 test -z $3 && path_new=${tmp_branch} || path_new=$3 path_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=$1 current_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 HEAD test -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
更多使用示例,参考官网.