git 使用笔记

本文介绍如何通过使用git pull --rebase命令来优化团队开发中的提交记录,使其更加整洁,便于后续版本合并。文章详细解释了rebase命令的工作原理,并提供了交互式更改commit消息及撤销amend的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

团队作战时,如何让提交记录更干净,让后期做版本合并的同学更省力?

只需要一个简单的原则:

git pull –rebase

详解如下:
开发过程中,为了提交review,需要不时地commit当前的修改;为了跑测试,需要不时地把服务端的代码pull到本地。在这种情况下,如果一个feature开发持续一两周,就会形成这样一个commit log:

HEAD <– My bugfix <– merge log <– team code … <– My bugfix … <– My submit <– merge log <– team code … <– My init sumit

非常丑!如果用好了rebase,则最终的commit log是这样子:

HEAD <– My bugfix <– My bugfix … <– My submit <– <– My init sumit <– team code … <– team code ..

所有的本地提交log都集中在一起。

为了达成这样的效果,你只需要每次pull都执行:

git pull –rebase
git pull –rebase
git pull –rebase
git pull –rebase
git pull –rebase
git pull –rebase

放心执行吧,不会出什么鬼。


update @2017.2.22

git rebase -i 交互式更改commit消息
git commit –amend 提交和上一条合并

撤销amend的方法:


What you need to do is to create a new commit with the same details as the current HEAD commit, but with the parent as the previous version of HEAD. git reset --soft will move the branch pointer so that the next commit happens on top of a different commit from where the current branch head is now.

# Move the current head so that it's pointing at the old commit
# Leave the index intact for redoing the commit.
# HEAD@{1} gives you "the commit that HEAD pointed at before 
# it was moved to where it currently points at". Note that this is
# different from HEAD~1, which gives you "the commit that is the
# parent node of the commit that HEAD is currently pointing to."
git reset --soft HEAD@{1}

# commit the current tree using the commit details of the previous
# HEAD commit. (Note that HEAD@{1} is pointing somewhere different from the
# previous command. It's now pointing at the erroneously amended commit.)
git commit -C HEAD@{1}

reference:

https://www.atlassian.com/git/tutorials/merging-vs-rebasing/

http://stackoverflow.com/questions/1459150/how-to-undo-git-commit-amend-done-instead-of-git-commit

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值