GIT常用命令笔记
统计代码行数
统计个人代码行数
$ git log --author="wii" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -
added lines: 41935, removed lines: 41485, total lines: 450
统计每个代码行数
$ git log --format='%aN' | sort -u | while read name; do echo -en "$name\t"; git log --author="$name" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -; done
郑树新 added lines: 15388, removed lines: 5179, total lines: 10209
akzi added lines: 2003, removed lines: 289, total lines: 1714
Artamonov Evgenyi added lines: 2, removed lines: 1, total lines: 1
centos6.9-32 added lines: 112, removed lines: 22, total lines: 90
Charlie Root added lines: 108, removed lines: 34, total lines: 74
Chunyang Xu added lines: 28, removed lines: 27, total lines: 1
dep added lines: 349, removed lines: 70, total lines: 279
Dep user added lines: 6, removed lines: 6, total lines: 0
fuwangqin added lines: 3971, removed lines: 1107, total lines: 2864
fuwq added lines: , removed lines: , total lines:
Li Qiang added lines: 1, removed lines: 1, total lines: 0
liqiang01 added lines: 9, removed lines: 14, total lines: -5
markmeson added lines: 41, removed lines: 0, total lines: 41
meihouhao2017 added lines: 11, removed lines: 2, total lines: 9
moehuster added lines: 147, removed lines: 39, total lines: 108
root added lines: 1924, removed lines: 1299, total lines: 625
ruki added lines: 493, removed lines: 71, total lines: 422
shuxin zheng added lines: 144533, removed lines: 34161, total lines: 110372
SorataKasugano added lines: 19, removed lines: 18, total lines: 1
ubuntu14 added lines: 326628, removed lines: 122145, total lines: 204483
unknown added lines: 8595, removed lines: 197, total lines: 8398
wanghaibin added lines: 689, removed lines: 211, total lines: 478
wfeii1980 added lines: 1, removed lines: 1, total lines: 0
wii added lines: 2237, removed lines: 2144, total lines: 93
Wii added lines: 4, removed lines: 3, total lines: 1
zhangdongchang added lines: 367, removed lines: 2, total lines: 365
Zhang Qiang added lines: 646, removed lines: 247, total lines: 399
Zhang QIang added lines: , removed lines: , total lines:
zhengshuxin added lines: 2931594, removed lines: 2027464, total lines: 904130
Zheng shuxin added lines: 1607, removed lines: 551, total lines: 1056
Zheng Shuxin added lines: 246, removed lines: 162, total lines: 84
zsx added lines: 3086543, removed lines: 2095158, total lines: 991385
zsxxsz added lines: 2526034, removed lines: 1818847, total lines: 707187
submodule 子模块
1 add
$ git submodule add http://mygit/wii/thirdparty-library.git 3rd
$ git status
位于分支 master
您的分支与上游分支 'origin/master' 一致。
要提交的变更:
(使用 "git restore --staged <文件>..." 以取消暂存)
新文件: .gitmodules
新文件: 3rd
$ git commit -am"added 3rd submodule"
$ git push
2 clone
方法一:
$ git clone http://mygit/wii/client.git
$ cd client
$ git submodule init
$ git submodule update
方法二:
$ git clone http://mygit/wii/client.git
$ cd client
$ git submodule update --init
方法三:
$ git clone http://mygit/wii/client.git --recursive
3 主库中操作submodule
$ git submodule foreach git status
导出代码
$ git archive --prefix [父目录]/ --format tar.gz --output [输出文件路径].tar.gz [导出分支master]
–prefix用于设置压缩包中的文件父目录,注意
/
。
不支持 tar.bz2 格式
不同分支设置不同的远程仓库
1 创建分支
$ git branch dev
2 切换分支
$ git checkout dev
# 以上两条命令可合并为以下,创建并切换到新分支
$ git checkout -b dev
# 切换到某一次commit Id
$ git checkout b5acca87893f699761fe1ee2209f9f4ce1935bbc
# 基于某一次commit Id创建分支
$ git checkout -b dev b5acca87893f699761fe1ee2209f9f4ce1935bbc
# 查看当前分支
$ git branch
# 删除指定分支
$ git branch -d dev
3 设置远程
$ git remote add [新的远程主机名] [url]
4 提交远程
$ git push --set-upstream [新的远程主机名] [远程分支名]
缩写
$ git push -u [新的远程主机名] [远程分支名]
远程分支名不能是master,不然会同步本地的master分支。
5 push到多个远程地址
此方法对git pull不友好,建议酌情使用。
$ git remote set-url --add origin http://mygit.hll:3000/test.git
[remote "origin"]
url = http://gitee.com/test.git
fetch = +refs/heads/*:refs/remotes/origin/*
url = http://mygit.hll:3000/test.git
分支合并与冲突处理
$ git merge master
分支合并到主分支时,去掉分支的冗余提交
使用场景: 我建立一个平时开发的分支,这个支持上我会经常提交以保存代码历史,待一个功能彻底开发完成或bug修改完成后,再同步到主分支并过滤掉分支上的大量提交。可使主分支提交更简洁一些。
$ git checkout master
$ git merge --squash dev
$ git commit -m 'xxx版'
--ff
When the merge resolves as a fast-forward, only update the branch pointer, without creating a merge commit. This is the default behavior.
--no-ff
Create a merge commit even when the merge resolves as a fast-forward. This is the default behaviour when merging an annotated (and possibly signed) tag.
--squash
--no-squash
Produce the working tree and index state as if a real merge happened (except for the merge information), but do not actually make a commit, move the HEAD, or record $GIT_DIR/MERGE_HEAD (to cause the next git commit command to create a merge commit). This allows you to create a single commit on top of the current branch whose effect is the same as merging another branch (or more in case of an octopus).
With --no-squash perform the merge and commit the result. This option can be used to override --squash.
统一提交文档中的编码问题
当前IDE普遍使用utf-8编码,但如果原始代码使用的是gbk或其它编译,修改后再提交会出现编码不统一的问题。
我写了个脚本,将修改过的文件编码格式进行统一化转换。
#!/bin/bash
for i in `LANG=en_US git status -uno | grep modified | awk '{print $2}'`
do
utf8=$(file $i | grep UTF-8)
if [ "$utf8" != "" ];then
`iconv -c -s -f UTF-8 -t GBK $i -o $i`
fi
done