git是多数软件工程中常用的版本管理工具,本文只是记录下自己日常会用到git命令。
最基础几个git命令
# 最基础的几个命令
git clone
git add
git commit -m "my commit message"
git pull
git push origin $branch_name
git reset $commit_id
git show $commit_id
git revert $commit_id
git rebase master
日常可能会用到较复杂的git命令
# 应用已经存在的commit
git cherry-pick $commit_id
# 合并两个分支
git merge $branch_name/$commit_id
# 本地有未提交的修改,但是想做git pull时 使用
git stash
# 把stash隐藏掉的东西恢复回来
git stash pop
# -s 或 --signoff 在提交信息的末尾加上一行 Signed-off-by 信息,
# 如 linux中常用的 Signed-off-by: Developer <random@developer.example.org>
git commit -s -m "my commit message"
git revert -s $commit_id
# 查看某一个作者的提交历史信息
git-log --author=Jay
# git log 输出信息格式定制化
git log --pretty=format:"%h %an %ae"
git log --oneline --pretty=format:"%h %an %ae"
# 搜索特定的字符串 -S
git log --oneline -S keyword --pretty=format:"%h %an %ae"
# 搜索正则表达式匹配的行 -G
git log -G "^http"
# 汇总git提交信息(按照作者汇总的)
git shortlog
# 汇总git提交信息 按照作者汇总 只显示commit个数(不显示commit message)
git shortlog -s
# --no-pager 选项,可以让 git log/git show 等不分页,直接展示所有
git --no-pager log
git --no-pager show 111c5c24
# 查看diff统计信息,如 哪个文件增加了几行删除了几行
git diff --stat
# 查看staged的(平时git add 后,git diff 就默认不可见了)修改
git diff --staged
git diff --cached # --staged 与 --cached 是一样的
# 对于已提交过的注释信息,需要修改,可以借助 git commit --amend 来进行。
git commit --amend -m "update my commit message"
# 可以防止引入一个Merge xx 的commit
git pull --rebase
# Show what revision and author last modified each line of a file. 查看当前的文件中的每一行最后修改的作者是谁;可以有bug/或看不懂的某一行找到作者 去沟通。
git blame test.py
一个搜索历史commit中改动的例子
#!/bin/bash
#git_search_keyword.sh
function usage()
{
echo "================================================================"
echo "error! usage: sh $0 your-keyword"
echo "example: sh $0 helloworld"
echo "================================================================"
}
function git_info()
{
echo "=========================== INFO ==============================="
echo "current keyword: $keyword"
echo "current branch: $(git rev-parse --abbrev-ref HEAD)"
echo "current commit id: $(git rev-parse --short HEAD)"
echo "if you want to use another branch, please git checkout to that branch first."
echo "================================================================"
}
function git_find_keyword()
{
commit_ids=$(git log --oneline -S "$keyword" --pretty=format:"%h %an %ae")
if [ "X$commit_ids" == "X" ]; then
echo "error! can NOT find your keyword: $keyword"
exit 1
fi
echo "$commit_ids" | while read line
do
commit_id=$(echo "$line" | cut -d' ' -f1)
author=$(echo "$line" | cut -d' ' -f2)
email=$(echo "$line" | cut -d' ' -f3)
keyword_lines=$(git --no-pager show $commit_id | grep "$keyword" | sort -n | uniq | cut -d"+" -f2)
if [ -n "$keyword_lines" ];then
echo "$commit_id $author $email"
echo "$keyword_lines"
fi
done
}
keyword="$1"
if [ "X$keyword" == "X" ]; then
usage
exit 1
fi
git_info
git_find_keyword