git
usage
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]
These are common Git commands used in various situations:
start a working area (see also: git help tutorial)
clone Clone a repository into a new directory
init Create an empty Git repository or reinitialize an existing one
work on the current change (see also: git help everyday)
add Add file contents to the index
mv Move or rename a file, a directory, or a symlink
reset Reset current HEAD to the specified state
rm Remove files from the working tree and from the index
examine the history and state (see also: git help revisions)
bisect Use binary search to find the commit that introduced a bug
grep Print lines matching a pattern
log Show commit logs
show Show various types of objects
status Show the working tree status
grow, mark and tweak your common history
branch List, create, or delete branches
checkout Switch branches or restore working tree files
commit Record changes to the repository
diff Show changes between commits, commit and working tree, etc
merge Join two or more development histories together
rebase Reapply commits on top of another base tip
tag Create, list, delete or verify a tag object signed with GPG
collaborate (see also: git help workflows)
fetch Download objects and refs from another repository
pull Fetch from and integrate with another repository or a local branch
push Update remote refs along with associated objects
'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.
最常用的 git 命令有:
add 添加文件内容至索引
bisect 通过二分查找定位引入 bug 的变更
branch 列出、创建或删除分支
checkout 检出一个分支或路径到工作区
clone 克隆一个仓库到一个新目录
commit 记录变更到仓库
diff 显示提交之间、提交和工作区之间等的差异
fetch 从另外一个仓库下载对象和引用
grep 输出和模式匹配的行
init 创建一个空的 Git 仓库或重新初始化一个已存在的仓库
log 显示提交日志
merge 合并两个或更多开发历史
mv 移动或重命名一个文件、目录或符号链接
pull 获取并整合另外的仓库或一个本地分支
push 更新远程引用和相关的对象
rebase 本地提交转移至更新后的上游分支中
reset 重置当前 HEAD 到指定状态
rm 从工作区和索引中删除文件
show 显示各种类型的对象
status 显示工作区状态
tag 创建、列出、删除或校验一个 GPG 签名的标签对象
常用功能
初始化一个新本地仓库
git init gitTest
初始化一个新本地仓库,它在工作目录下生成一个名为.git的隐藏文件夹。
查看分支
git branch -a
查看提交记录
git log
重置回某次提交记录
git reset --hard xxxxxx
把代码添加到本地仓库
git add
查看本地仓库状态
git status
把代码加入到本地仓库
git commit - m
把代码推送到远程仓库
git push
切换分支
git checkout
git clone 拉去代码
git clone git://github.com/schacon/grit.git
建立开发分支
git checkout -b dev
查看分支
git branch
将本地分支与远程服务器进行关联(仅第一次需要)
git push --set-upstream origin dev
修改代码并提交本地dev分支
git status # 查看变更文件,检查文件有没有遗漏
git add -A
git commit -m "提交说明"
开发分支合并到master分支
git checkout master
git merge dev
git add -A
git commit -m "提交说明"
git push
切回develop开发分支,并拉取远程master分支最新代码
git checkout dev
git pull origin master
git中查看一个文件在两个不同的分支中的差异
git diff branch1 branch2 -- index.html
查看完整历史修改记录
git blame [filename]
取回误删除的文件
知道在哪个提交中有
git checkout xxxxx lost_file
其他
git 命令
mkdir: XX (创建一个空目录 XX指目录名)
pwd: 显示当前目录的路径。
git init 把当前的目录变成可以管理的git仓库,生成隐藏.git文件。
git add XX 把xx文件添加到暂存区去。
git commit –m “XX” 提交文件 –m 后面的是注释。
git status 查看仓库状态
git diff XX 查看XX文件修改了那些内容
git log 查看历史记录
git reset –hard HEAD^ 或者 git reset –hard HEAD~ 回退到上一个版本
(如果想回退到100个版本,使用git reset –hard HEAD~100 )
cat XX 查看XX文件内容
git reflog 查看历史记录的版本号id
git checkout — XX 把XX文件在工作区的修改全部撤销。
git rm XX 删除XX文件
git remote add origin https://github.com/tugenhua0707/testgit 关联一个远程库
git push –u(第一次要用-u 以后不需要) origin master 把当前master分支推送到远程库
git clone https://github.com/tugenhua0707/testgit 从远程库中克隆
git checkout –b dev 创建dev分支 并切换到dev分支上
git branch 查看当前所有的分支
git checkout master 切换回master分支
git merge dev 在当前的分支上合并dev分支
git branch –d dev 删除dev分支
git branch name 创建分支
git stash 把当前的工作隐藏起来 等以后恢复现场后继续工作
git stash list 查看所有被隐藏的文件列表
git stash apply 恢复被隐藏的文件,但是内容不删除
git stash drop 删除文件
git stash pop 恢复文件的同时 也删除文件
git remote 查看远程库的信息
git remote –v 查看远程库的详细信息
git push origin master Git会把master分支推送到远程库对应的远程分支上
统计代码行数
1.根据用户名时间段统计
git log --author="username" --since=2018-01-01 --until=2019-12-31 --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 }' -
2.查看提交者排名前N位
git log --pretty='%aN' | sort | uniq -c | sort -k1 -n -r | head -n 5
3.提交数统计
git log --oneline | wc -l
4.根据用户名统计
git log --author="username" --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 }' -
5.根据时间段统计
git log --since=2018-01-01 --until=2018-12-31 --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 }'
6.统计每个人的增删行数
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 ", add, subs, loc }' -; done
7.贡献者统计
git log --pretty='%aN' | sort -u | wc -l
单独统计每个人的增删行数
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
指定用户名版
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 }'
获取某人某段时间内的提交次数
git log --author="userName" --since="2019-12-01" --until="2019-12-19" --oneline | wc -l
获取某人提交详情(新增行数,删除行数,总计)
git log --author="userName" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "新增行数: %s, 移除行数: %s, 总行数: %s\n", add, subs, loc }'
其他代码统计
find命令
find demo/ -name “*.java” |xargs cat|wc -l
1.统计demo目录下,js文件数量:
find demo/ -name “*.js” |wc -l
2.统计demo目录下所有js文件代码行数:
find demo/ -name “*.js” |xargs cat|wc -l 或 wc -l find ./ -name "*.js"
|tail -n1
3.统计demo目录下所有js文件代码行数,过滤了空行:
find /demo -name “*.js” |xargs cat|grep -v ^$|wc -l
cloc命令
cloc 路径
错误处理
git push报错
Delta compression using up to 4 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (10/10), 1.87 KiB | 637.00 KiB/s, done. Total 10 (delta 1), reused 0 (delta 0) remote: Resolving deltas: 100% (1/1) remote: Processing changes: refs: 1, done
remote: ERROR: [5c59d6e] missing Change-Id in commit message footer remote: remote: Hint: To automatically insert Change-Id, install the hook: remote: gitdir=$(git rev-parse --git-dir); scp -p -P 29418 wangjp0712@192.168.11.88:hooks/commit-msg ${gitdir}/hooks/ remote: And then amend the commit: remote: git commit --amend
gitdir=$(git rev-parse --git-dir); scp -p -P 29418 wangjp0712@192.168.11.88:hooks/commit-msg ${gitdir}/hooks/
git commit --amend
命令详解
git blame
显示文件的每一行最后修改的版本和作者
usage: git blame [<options>] [<rev-opts>] [<rev>] [--] <file>
<rev-opts> are documented in git-rev-list(1)
--incremental Show blame entries as we find them, incrementally
-b Show blank SHA-1 for boundary commits (Default: off)
--root Do not treat root commits as boundaries (Default: off)
--show-stats Show work cost statistics
--progress Force progress reporting
--score-debug Show output score for blame entries
-f, --show-name Show original filename (Default: auto)
-n, --show-number Show original linenumber (Default: off)
-p, --porcelain Show in a format designed for machine consumption
--line-porcelain Show porcelain format with per-line commit information
-c Use the same output mode as git-annotate (Default: off)
-t Show raw timestamp (Default: off)
-l Show long commit SHA1 (Default: off)
-s Suppress author name and timestamp (Default: off)
-e, --show-email Show author email instead of name (Default: off)
-w Ignore whitespace differences
--indent-heuristic Use an experimental heuristic to improve diffs
--minimal Spend extra cycles to find better match
-S <file> Use revisions from <file> instead of calling git-rev-list
--contents <file> Use <file>'s contents as the final image
-C[<score>] Find line copies within and across files
-M[<score>] Find line movements within and across files
-L <n,m> Process only line range n,m, counting from 1
--abbrev[=<n>] use <n> digits to display SHA-1s
a | b |
---|---|
-L <n,m> | 只标注给定的行范围。 和 采取形式: 数字 如果 或 是数字的话,意味着从一个绝对行号开始(行号从1开始计算) 正则表达式 这个形式将使用与给定的POSIX regex匹配的第一行。如果 是正则, 它将从给出的行开始搜索。 · +offset or -offset 仅支持 并且需要指定给出的行之前或之后的行数。 |
-l | 展示long rev (Default: off). |
-t | 展示 raw timestamp (Default: off). |
-s | 从输出中取消作者名和时间戳。 |
–show-stats | 在blame输出的末尾包括附加统计数据。 |