huikai

生于忧患,死于安乐


目录

配置

  1. # 设置全局换行
  2. git config --global core.autocrlf input
  3. # 设置全局日志日期格式
  4. git config --global log.date iso8601
  5. # 设置当前库提交模板
  6. git config --local commit.template <file>

git重写历史

删除历史

删除文件历史

  1. git filter-branch --tree-filter 'rm -f common/common-jar/pom.xml' HEAD

删除目录历史

  1. git filter-branch --tree-filter 'rm -rf common/common-jar' HEAD

清理缓存

  1. git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin
  2. git reflog expire --expire=now --all
  3. git gc --prune=now

推送远程仓库

  1. git push origin --force --all
  2. git push origin --force --tags

修改作者

  1. git filter-branch --commit-filter '
  2. if [ "$GIT_AUTHOR_EMAIL" = "huikai@test.com" ];
  3. then
  4. GIT_AUTHOR_NAME="huikai";
  5. GIT_AUTHOR_EMAIL="huikai@itith.com";
  6. git commit-tree "$@";
  7. else
  8. git commit-tree "$@";
  9. fi' HEAD

修改提交人

  1. git filter-branch --commit-filter '
  2. if [ "$GIT_COMMITTER_EMAIL" = "huikai@test.com" ];
  3. then
  4. GIT_COMMITTER_NAME="huikai";
  5. GIT_COMMITTER_EMAIL="huikai@itith.com";
  6. git commit-tree "$@";
  7. else
  8. git commit-tree "$@";
  9. fi' HEAD

修改提交信息

  1. git filter-branch --msg-filter '
  2. sed -e "s/原字符串/新字符串/g"
  3. ' HEAD

移动到子目录

  1. git filter-branch --index-filter \
  2. 'git ls-files -s | sed "s-\t\"*-&子目录名称/-" |
  3. GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
  4. git update-index --index-info &&
  5. if test -f "$GIT_INDEX_FILE.new"; then mv $GIT_INDEX_FILE.new $GIT_INDEX_FILE; fi' HEAD

子目录名称如有中划线需要转义

将子目录作为根目录

  1. git filter-branch --subdirectory-filter 子目录 HEAD

签名

提交时签名

git commit -sSm

git commit -sS -m "提交说明"

衍合时签名

git rebase -S 来源分支名

打tag时签名

git tag -s 标签名称 -m '标签说明'

设置签名使用密钥

  • 全局

git config --global user.signingkey <keyid>

  • 本仓库

git config --local user.signingkey <keyid>

默认开启提交签名

git config --global commit.gpgsign true

设置签名程序

git config --global gpg.program <程序>
指定要使用哪个程序进行签名和验证。其命令行必须符合GPG标准。用于选择特定的GPG版本(例如,gpg2 vs gpg)或自定义程序


标签相关

给指定提交打标签

git tag -a 标签名称 9fceb0b

推送单个标签

git push origin 标签名称

推送所有本地标签

git push origin --tags


日志相关

查看某人提交日志

git log --author=<pattern>

git log --grep=<pattern>

查找指定文件中关键字

git log --grep=<pattern> <filepath>

查看2次提交文件变化统计

git diff hash1 hash1 --stat

查看2次提交文件变化全路径

git diff --name-status hash1 hash2

查看2次提交文件变化,忽略指定文件

git diff hash1 hash2 --name-only | grep -v '<file>' | xargs git diff hash1 hash2 --


对比

忽略空格

忽略空格变化
git diff -b <file>

忽略所有空格
git diff -w <file>


svn

clone svn库

如要重新指定用户名和邮箱,需要建立映射文件(users.txt),格式:

  1. huikai = huikai <huikai@itith.com>
  2. huikai = huikai2 <huikai2@itith.com>

git svn clone <svn路径> --authors-file=users.txt --no-metadata -T trunk <目录名>

作为svn客户端

拉取远程更新

git svn fetch

合并更新

git svn rebase

提交到svn

git svn dcommit


合并

不同源合并

git merge <分支名> --allow-unrelated-histories

衍合

原则: 确保要rebase的代码全部都是未提交远程仓库的代码,rebase前最好先将<来源分支>更新到最新

普通衍合

git rebase -S 来源分支

部分衍合

git rebase --onto 来源分支 起始分支 操作分支

直接衍合(不需要切换分支)

git rebase 来源分支 操作分支

更新别人误衍合代码

git pull --rebase

git fetch + git rebase 远程分支


忽略

git上已有文件本地更新忽略和解除

忽略

git update-index --assume-unchanged <文件路径>

解除忽略

git update-index --no-assume-unchanged <文件路径>

查看忽略

git ls-files -v 结果中以h开头的为忽略文件

git ls-files -v | grep ^h


回滚提交

回滚未push提交

git reset <commit id>

回滚已push提交

使用git revert <commit id>来撤销某一次提交

如果想撤销多次提交可以多次调用撤销命令,如果不想看到多次提交的历史记录可以使用参数-n或者--no-commit在撤销时不自动提交,等把所有的撤销操作完以后使用git revert --continue统一提交

git revert -n <commit id>


分支

分支重命名

git branch -m <原分支名> <新分支名>

删除分支

删除本地分支

git branch -d <分支名>
强制删除
git branch -D <分支名>

删除远程分支

git push <远程源> :<分支名>

统计

统计提交代码行数

git log --format='%aE' | 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




 Emacs入门 使用openssl 生成RSA pem格式密钥对