保持清洁的Git提交记录,三招就够了

└── feat1.txt

0 directories, 2 files

假设我们提交 feature 1.3 的时候,忘记了一个配置文件 config.yaml , 不想修改 log,不想添加新的 commit-id,那下面的这个命令就非常好用了

echo “feature 1.3 config info” > config.yaml

git add .

git commit --amend --no-edit

git commit --amend --no-edit 就是灵魂所在了,来看一下当前的 repo 文件:

.

├── README.md

├── config.yaml

└── feat1.txt

0 directories, 3 files

再来看一下 git log

  • 247572e (HEAD -> feature/JIRA123-amend-test) feat: [JIRA123] add feature 1.2 and 1.3

  • 119f86e feat: [JIRA123] add feature 1.1

  • 5dd0ad3 feat: [JIRA123] add feature 1

  • c69f53d (origin/main, origin/feature/JIRA123-amend-test, origin/HEAD, main) Initial commit

知道这个技巧,就可以确保我们的每次提交都包含有效的信息了。一张图描述这个过程就是这个样子了:

保持清洁的Git提交记录,三招就够了

有了 --no-edit 的 buff 加成,威力更大一些

善用 git rebase -i

================

可以看着,上面的 log 都是在开发 feature1,我们在把 feature 分支 merge 到 main 分支之前,还是应该继续合并 log commit 节点的,这就用到了

git rebase -i HEAD~n

其中 n 代表最后几个提交,上面我们针对 feature 1 有三个提交,所以就可以使用:

git rebase -i HEAD~3

运行后,会显示一个 vim 编辑器,内容如下:

1 pick 5dd0ad3 feat: [JIRA123] add feature 1

2 pick 119f86e feat: [JIRA123] add feature 1.1

3 pick 247572e feat: [JIRA123] add feature 1.2 and 1.3

4

5 # Rebase c69f53d…247572e onto c69f53d (3 commands)

6 #

7 # Commands:

8 # p, pick = use commit

9 # r, reword = use commit, but edit the commit message

10 # e, edit = use commit, but stop for amending

11 # s, squash = use commit, but meld into previous commit

12 # f, fixup = like “squash”, but discard this commit’s log message

13 # x, exec = run command (the rest of the line) using shell

14 # d, drop = remove commit

15 # l, label

16 # t, reset

17 # m, merge [-C | -c ]

18 # . create a merge commit using the original merge commit’s

19 # . message (or the oneline, if no original merge commit was

20 # . specified). Use -c to reword the commit message.

21 #

22 # These lines can be re-ordered; they are executed from top to bottom.

23 #

24 # If you remove a line here THAT COMMIT WILL BE LOST.

25 #

26 # However, if you remove everything, the rebase will be aborted.

27 #

28 #

29 # Note that empty commits are commented out

合并 commit-id 最常用的是 squash 和 fixup , 前者包含 commit message,后者不包含,这里使用 fixup, 然后 :wq 退出

1 pick 5dd0ad3 feat: [JIRA123] add feature 1

2 fixup 119f86e feat: [JIRA123] add feature 1.1

3 fixup 247572e feat: [JIRA123] add feature 1.2 and 1.3

我们再来看一下 log, 这就非常清晰了

  • 41cd711 (HEAD -> feature/JIRA123-amend-test) feat: [JIRA123] add feature 1

  • c69f53d (origin/main, origin/feature/JIRA123-amend-test, origin/HEAD, main) Initial commit

善用 rebase

=========

上面的 feature1 已经完整地开发完了,main 分支也有了其他人的更新,再将 feature merge 回 main 分支之前,以防代码有冲突,需要先将 main 分支的内容合并到 feature 中,如果用 merge 命令,就会多处一个 merge 节点,log history 中也会出现拐点,并不是线性的,所以这里我们可以在 feature 分支上使用 rebase 命令

git pull origin main --rebase

保持清洁的Git提交记录,三招就够了

pull 命令的背后是自动帮我们做 merge 的,但是这里以 rebase 的形式,再来看一下 log

  • d40daa6 (HEAD -> feature/JIRA123-amend-test) feat: [JIRA123] add feature 1

  • 446f463 (origin/main, origin/HEAD) Create main.properties

  • c69f53d (origin/feature/JIRA123-amend-test, main) Initial commit

我们的 feature1 功能 on top of main 的提交节点,还是保持线性,接下来就可以 push 代码,然后提 PR,将你的 feature merge 到 main 分支了

简单描述 merge 和 rebase 的区别就是这样的:

保持清洁的Git提交记录,三招就够了

我这里使用 git pull origin main --rebase 省略了切换 main 并拉取最新内容再切回来的过程,一步到位,背后的原理都是上图展示的这样

使用 rebase 是要遵守一个黄金法则的,这个之前有说过,就不再是赘述了
总结

难道这样就够了吗?不,远远不够!

提前多熟悉阿里往年的面试题肯定是对面试有很大的帮助的,但是作为技术性职业,手里有实打实的技术才是你面对面试官最有用的利器,这是从内在散发出来的自信。

备战阿里时我花的最多的时间就是在学习技术上,占了我所有学习计划中的百分之70,这是一些我学习期间觉得还是很不错的一些学习笔记

我为什么要写这篇文章呢,其实我觉得学习是不能停下脚步的,在网络上和大家一起分享,一起讨论,不单单可以遇到更多一样的人,还可以扩大自己的眼界,学习到更多的技术,我还会在csdn、博客、掘金等网站上分享技术,这也是一种学习的方法。

今天就分享到这里了,谢谢大家的关注,以后会分享更多的干货给大家!

阿里一面就落马,恶补完这份“阿里面试宝典”后,上岸蚂蚁金服

阿里一面就落马,恶补完这份“阿里面试宝典”后,上岸蚂蚁金服

image.png

是很不错的一些学习笔记

我为什么要写这篇文章呢,其实我觉得学习是不能停下脚步的,在网络上和大家一起分享,一起讨论,不单单可以遇到更多一样的人,还可以扩大自己的眼界,学习到更多的技术,我还会在csdn、博客、掘金等网站上分享技术,这也是一种学习的方法。

今天就分享到这里了,谢谢大家的关注,以后会分享更多的干货给大家!

[外链图片转存中…(img-vTCdabIB-1718911123192)]

[外链图片转存中…(img-VUKJYKxp-1718911123193)]

[外链图片转存中…(img-IS9g3Q3a-1718911123193)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值