Git进阶

Git进阶

1、根据commit找分支

git branch -a --contains [commit]

2、根据commit找标签

git tag --contains [commit]

3、分支比较

git log

  • 查看在branchA,却不在branchB中的提交。^表示排除。
git log [branchA] ^[branchB]
  • 查看branchA比branchB多提交的commit。
git log [branchA]..[branchB]
  • branchA,branchB有什么不同commit。
git log [branchA]...[branchB]

符号“ < ”表示branchA的commit,符号“ > ”表示branchB的commit。

git log --left-right [branchA]...[branchB]

这里写图片描述

git diff

  • git diff 也有上述的 ^.....功能。

  • 显示出所有有差异的文件列表

git diff branchA branchB --stat

4、查看文件修改记录

查看当前filename文件的每个部分信息(作者等)

git blame [filename]

查看filename相关的commit以及diff

git log -p [filename]

5、合并

  • git merge

这里写图片描述

feature分支合并回develop分支时,最好不要使用fast-forward

git merge --no-ff [branchname]

  • git rebase

这里写图片描述

git rebase过程中如果有冲突,如both modified、deleted by us。
both modified相关文件修改后,直接git add [filename]
deleted by us相关文件执行git rm [filename],否则可能将文件合并到新的节点里。

git rebase过程相当于本地生成一个新的分支,期间不要再从remote拉取,会造成double commit。完成rebase后,执行git push -f强行覆盖远端分支。如果不熟悉流程,最好先准备一个备份分支,防止在git rebase过程丢失提交。

将几个节点移动到其他分支

git rebase [startpoint] [endpoint] --onto [branchname]

6、标签

  • 创建
    新建一个标签,不指定默认为HEAD,也可以指定一个commit。推送时需加--tags
git tag [tagname] [commit]
git push --tags origin [branchname]

tips:
如不指明[branchname],使用git push --tags。只会将tag和commit提交上去,但是commit不会挂靠到对应branch。

  • 查看
git tag
  • 删除
    先删除本地tag,再删除远端tag。
git tag -d [tagname]
git push origin :refs/tags/[tagname]

7、统计

  • 提交数排名
git log --pretty=format:%ae | gawk -- '{ ++c[$0]; } END { for(cc in c) printf "%5d %s\n",c[cc],cc; }' | sort -u -n -r 

提交数前五

git log --pretty=format:%ae | gawk -- '{ ++c[$0]; } END { for(cc in c) printf "%5d %s\n",c[cc],cc; }' | sort -u -n -r | head -n 5
  • 代码提交量
git log --author="$(git config --get user.name)" --pretty=tformat: --numstat | gawk '{ add += $1 ; subs += $2 ; loc += $1 - $2 } END { printf "added lines: %s removed lines : %s total lines: %s\n",add,subs,loc }'
  • Tips

安装gawk

brew install gawk

8、删除已提交大文件和密码等

工具如下,不做具体意译。使用请谨慎阅读文档
https://rtyley.github.io/bfg-repo-cleaner/
https://github.com/rtyley/bfg-repo-cleaner

In all these examples bfg is an alias for java -jar bfg.jar.

  • Delete all files named ‘id_rsa’ or ‘id_dsa’ :
    $ bfg --delete-files id_{dsa,rsa} my-repo.git

  • Remove all blobs bigger than 50 megabytes :
    $ bfg --strip-blobs-bigger-than 50M my-repo.git

  • Replace all passwords listed in a file (prefix lines ‘regex:’ or ‘glob:’ if required) with REMOVED wherever they occur in your repository :
    $ bfg --replace-text passwords.txt my-repo.git

  • Remove all folders or files named ‘.git’ - a reserved filename in Git. These often become a problem when migrating to Git from other source-control systems like Mercurial :
    $ bfg --delete-folders .git --delete-files .git --no-blob-protection my-repo.git

For further command-line options, you can run the BFG without any arguments, which will output text like this.


9、二分查找

  • git bisect

确定区间

git bisect start [endCommit] [startCommit]

执行以上命令,自动二分且切换至中点。

查找

git bisect good,自动切换到后半段的中点。
git bisect bad,自动切换到前半段的中点。
不断重复这个过程,直到成功找到出问题的那一次提交为止。

退出

git bisect reset

10、删除分支

删除本地分支

git branch -D [branch]

删除远端分支

git push origin --d [branch]

11、遴选

  • git cherry-pick

用于拣选某些节点从一个分支到另外一个分支。

git cherry-pick [commit]

一次可遴选多个commit,用空格分隔commit即可。每个commit被拣选都会生成一个新的commit。

如果希望遴选多个commit合成一条,可以用-n来实现。

git cherry-pick  -n  [commit]  [commit]

12、重置

  • git reset

回到当前版本,放弃所有没有提交的修改

git reset --hard HEAD

回到上一个版本

git reset --hard HEAD^

回到之前第3个修改版本

git reset --hard HEAD~3

回到指定版本号的版本

git reset --hard [commit]
  • 撤消添加
git reset (--mixed)
  • 撤消提交并重做
git reset --soft HEAD^
  • 撤消永久提交
git reset --hard HEAD~3

13、查看参考日志

git reflog

摘要:

https://blog.csdn.net/u011240877/article/details/52586664
https://mp.weixin.qq.com/s/Wy72SX7PISUlJiTDA3XWNw
https://rtyley.github.io/bfg-repo-cleaner/
https://github.com/rtyley/bfg-repo-cleaner
http://www.ruanyifeng.com/blog/2018/12/git-bisect.html
https://blog.csdn.net/jxianxu/article/details/79240158
https://www.php.cn/manual/view/34965.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值