Git 代码片段

统计行数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Usage: 
# git_lines
# git_lines -a author -d dir
#
# Install `gawk` first
function git_lines {
local name=""
local dir=""
while getopts "a:d:" arg
do
case $arg in
a)
name=$OPTARG
;;
d)
dir=$OPTARG
;;
?)
echo "unkonw argument"
exit 1
;;
esac
done
if [ -z "$name" ]; then
name=$(git config user.name)
fi
if [ -z "$dir" ]; then
dir="."
fi
echo "User [\033[33m$name\033[0m] at [\033[32m$dir\033[0m]"
git log --author="$name" --pretty=tformat: --numstat -- $dir | gawk '{ add += $1 ; subs += $2 ; loc += $1 + $2 } END { printf "added lines: \033[34m%d\033[0m, removed lines: \033[31m%d\033[0m, total lines: \033[32m%d\033[0m\n", add, subs, loc }'
}

第一行代码

1
2
3
4
5
6
7
8
9
10
11
function git_first {
echo "Git First"
first_commit=$(git rev-list --max-parents=0 HEAD)
echo "Hash: [\033[33m$first_commit\033[0m]"
echo "Date: [\033[32m$(git show -s --format=%ci $first_commit)\033[0m]"
files_changed=$(git show --pretty="" --name-only $first_commit)
first_file=$(echo "$files_changed" | sed -n 1p)
echo "File: [\033[34m$first_file\033[0m]"
first_line=$(git show $first_commit:$first_file | head -1)
echo "Line: [\033[35m$first_line\033[0m]"
}

将某个文件夹恢复到某个版本

1
git checkout <commit_id> -- <folder_path>

要查看特定文件的编辑次数

1
git log --oneline <file_path> | wc -l

在这个命令中,<file_path> 是你想要查看的文件的路径。git log --oneline <file_path> 会列出所有修改过这个文件的提交,每个提交占一行。然后 | wc -l 命令会计算这些行的数量,给出编辑次数。