13个Git命令提高你工作效率

前言

在我们日常的软件开发过程中,软件版本的管理都离不开使用Git,当然有些朋友的公司可能使用SVN等版本管理系统,但作者使用Git这么多年来,觉得Git是最好用的分布式版本控制系统,作为日常工作中高频使用的工具之一,作者想给大家介绍一些本人日常使用的一些非常实用的Git命令,希望能够帮助大家解决一些使用Git时的问题,帮助大家高效使用Git从而提供工作效率。

1 git rebase

git rebase有两种常用用法,分别为合并当前分支的多个commit记录避免出现分支的交叉合并

1.1 合并多个提交
$ git log --oneline
ced627df (HEAD -> master, origin/master) modify feature A
02b57a84 modify feature A
40891977 modify feature A
0a1058f9 add feature A

比如此时基于master分支有如上四次提交,评估可以合并后三次提交为一次,此时可以使用如下命令将后三次提交合并为一次

git rebase -i HEAD~3

输入该命令后进入交互式界面,如下

pick 40891977 modify feature A
pick 02b57a84 modify feature A
pick ced627df modify feature A

# Rebase 0a1058f9..ced627df onto 0a1058f9 (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.

交互式界面也告知了我们rebase的其他用法,我们可以对多个commit进行重新排序丢弃某个提交,合并多个提交,修改commit内容等操作。

命令缩写解释
pickp保留使用该commit
rewordr使用该commit但需要编辑
edite使用该commit但需要停下来修改该提交
squashs将该commit合并到前一个commit
fixupf将该commit合并到前一个commit,不需要保留该commit的注释
1.2 避免分支交叉

git rebase避免分支交叉,是使用了git rebase的变基功能,比如下图当前工作分支为mywork

image-20220706094048708

如果此时提交代码入库申请合并,执行git merge,分支结构图将会变成如下样子

image-20220706094435646

但是如果提交合并前,使用git rebase命令,分支结构图将会变成如下样子

image-20220706094528906

通过以上两图的对比,可以看出请求合并前执行git rebase可以避免分支交叉,提交分支越少越利于历史提交回溯。因此在开发分支请求合并前,可以执行git pull --rebase 命令,将自己的开发提交变基到主分支上,以减少分支交叉,如果有冲突解决冲突后使用git rebase --continue或者丢弃该操作则使用git rebase --abort。尽管使用git rebase可以避免分支交叉,但一定要记住不能在公共分支上使用rebase操作,否则会修改公共分支上的提交

2 git cherry-pick

该命令也比较常用,参考下图举几个常用的例子

例子1:当前处于mywork节点,别人提交了一个commit,我需要合并过来进行验证,那么此时可以使用git cherry-pick COMMIT_ID,就可以将别人的提交拉取过来

例子2:当前处于mywork节点,我想提交C6提交而不需要提交C5提交,此时可以先创建一个分支,然后将HEAD切换到C4,然后使用git cherry-pick COMMIT_ID_Of_C6就可以拉取C6提交到C4上

image-20220706095100307

3 git commit --amend

如果我们已经push了请求,但是想修改commit注释,此时可以使用该命令修改commit注释。如果是在push之前使用该命令不仅可以修改commit注释还可以添加遗漏的文件。

4 git commit -c

使用 git commit -c COMMIT_ID 可以获取别的分支commit注释,编辑后使用。另外commit注释可以添加模板,大家可以搜索资料研究。

5 git blame file_a

使用该命令可以查询file_a文件每一行的修改信息,查询某一行最终是由谁修改的,例如git blame main.c部分查询结果如下

7d1e4bdea8 main.c (Bob 	   2021-11-12 09:11:57 +0800  46) bool bKey = false;
7d1e4bdea8 main.c (Bob 	   2021-11-12 09:11:57 +0800  47) void printPersonalInfo()
7d1e4bdea8 main.c (Bob 	   2021-11-12 09:11:57 +0800  48) {
b03d88c699 main.c (Bob     2020-10-30 16:14:07 +0800  49)     if (bKey)
b03d88c699 main.c (Bob     2020-10-30 16:14:07 +0800  50)     {
b03d88c699 main.c (Bob     2020-10-30 16:14:07 +0800  51)			print("Name:%s\n", bob.name);
b03d88c699 main.c (Bob     2020-10-30 16:14:07 +0800  52)			print("Name:%d\n", bob.age);
b03d88c699 main.c (Tom     2020-10-30 16:15:09 +0800  53)			print("Name:%s\n", tom.name);
b03d88c699 main.c (Tom     2020-10-30 16:15:09 +0800  54)			print("Name:%d\n", tom.age);
b03d88c699 main.c (Bob     2020-10-30 16:14:07 +0800  55) 	  }  
b03d88c699 main.c (Bob     2020-10-30 16:14:07 +0800  56) }	

6 git stash

当本地几个文件修改后,还没有修改测试完毕,此时需要开发另一个功能,可以使用git stash命令将修改进行缓存,之后可以使用git stash pop命令将缓存的内容应用到当前分支

7 git log过滤

下面第一条条命令将只显示Bob在5月15号到5月25日之间的提交记录,第二条命令会将这个时间段的提交记录导入到git.log文件

git log --author="Bob" --after="2022-05-15" --before="2022-05-25"
git log --author="Bob" --after="2022-05-15" --before="2022-05-25" > git.log

8 git log -p

该命令显示每一次提交之间的差异,方便我们获知最直接的差异信息

9 git log file_a

该命令将显示文件file_a的提交记录

10 git log --grep=“JIRA-224”

该命令将过滤出commit中包含JIRA-224的提交记录,如果需要忽略大小写,可以加上-i

11 git log -SFunction_name

使用该命令可以查看增加或删除字符串Function_name的历史,加上-i参数可以取消大小写敏感

12 git reflog

使用该命令可以查看到HEAD指针及其之前的版本信息,比如版本发生了回退操作,想要查找或找回回退之前的历史提交,使用该命令可以查看回退前的提交

13 git 本地分支与远程分支关联

当你创建一个新的本地分支后,如果使用git pull拉取更新,git检查到你的本地分支并未与远程分支关联,会提示你明确拉取的远程分支或者建立本地分支与远程分支的关联

13.1 明确拉取的远程分支
git pull <remote> <branch>
13.2 关联本地分支与远程分支
git branch --set-upstream-to=origin/<branch> <local-branch>
13.3 创建分支时进行关联
git checkout -b newbranch origin/<branch>
git switch -c newbranch --track origin/<branch>

总结

以上都是作者在日常工作中高频使用的一些Git实用命令。如果您也有类似常用的Git实用技巧,欢迎您分享交流。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

钢琴上的汽车软件

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值