git 交互式rebase

交互式的rebase的场景在分支开发时特别有用, 可以减少没有必要的提交

git rebase -i

-i 参数表示互动 interactive,这时 git 会使用你设定的编辑器,让你对 git 历史记录做详细修改。

下面以在master分支和dev分支之间的切换为例,演示git rebase -i的使用

// 从master分支上拉取新的dev 分支用作开发
git checkout -b dev

// 在dev分支上修改一些文件并提交
git commit -a -m "feature: add new file"

// 在dev分支上修改bug, 再提交一次
git commit -a -m "fix: fix some bug"

// 这时看git记录,应该是有两次提交了,我们为了向master合并时代码整洁,打算将后面那次提交合并到上面的feature中,这样就需要我们的rebase -i 命令了

// 进行rebase -i
git rebase -i master
此时会显示
# 这里的两行表示最近的两次提交, pick表示保留这两次提交,我们只需要改成别的就行
pick 1d13e23 feature: add new file
pick f20aba9 fix: fix some bug

# Rebase 888d12f..f20aba9 onto 888d12f (2 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# 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.
#
# Note that empty commits are commented out

将pick 改成 fixup 表示放弃后面这次提交信息

git log查看时,只剩下一次提交信息了,fix那次commit message 被我们清除了

commit e2a543507533208b03f8b5dd84d9ef31703aca95
Author: tianrui1 <tianrui1@sina.com.cn>
Date:   Sat Mar 25 23:42:08 2017 +0800

    feature: add new file

这样就成功清除一次多余的提交信息,保证我们向master合并时,提交信息是最干净的

git rebase -i 其他交互命令

  • squash:将这一行的 commit 与上一个 commit 进行合并

  • fixup:与 squash 相同,只是不会保留这行 commit 的提交 message 信息

  • reword: 重新更改提交信息
  • edit: 拆分提交,使用edit以后,文件或重新放在暂存区,你可以拆分成多次提交

重排提交

你也可以使用交互式的衍合来彻底重排或删除提交。如果你想删除”added cat-file”这个提交并且修改其他两次提交引入的顺序,你将rebase脚本从这个

pick f7f3f6d changed my name a bit
pick 310154e updated README formatting and added blame
pick a5f4a0d added cat-file

改为这个:

pick 310154e updated README formatting and added blame
pick f7f3f6d changed my name a bit

当你保存并退出编辑器,Git 将分支倒回至这些提交的父提交,应用310154e,然后f7f3f6d,接着停止。你有效地修改了这些提交的顺序并且彻底删除了”added cat-file”这次提交。

git commit –fixup & –autosquash

# 自动标记这一次的 commit 为上一个 commit 的 fix
$ git commit --fixup <commit>
# 自动组织合并两个 commit
$ git rebase -i --autosquash

这种方式和上面的第一种原理是一样的,只是形式上不是在编辑器中调整历史记录,而是直接执行命令,结果等同。

撤销过去的 commit 重建一个新的

$ git reset HEAD~2
$ git add .
$ git commit -am "This is the new feature"
$ git push --force

修改上一次 commit 提交的 message

有时我们提交代码,message 写的太匆忙有错字(不是代码里有错字,而是 commit message 写错),就可以用下面的命令来修正

$ git commit --amend

不过只能修正上一次的 commit。如果很多个 commit 之前就有 message 写错,就得用上我们之前说的 git rebase 了

$ git rebase -i HEAD~4

和之前一样,会打开编辑器,显示最近的 4 次提交

pick 07c5abd Introduce OpenPGP and teach basic usage
pick de9b1eb Fix PostChecker::Post#urls
pick 3e7ee36 Hey kids, stop all the highlighting
pick fa20af3 git interactive rebase, squash, amend

将 pick 指令改为 reword 指令,就可以修改这一行的 commit message

pick 07c5abd Introduce OpenPGP and teach basic usage
pick de9b1eb Fix PostChecker::Post#urls
reword 3e7ee36 Hey kids, stop all the highlighting
pick fa20af3 git interactive rebase, squash, amend

其中 HEAD~4 表示前4次提交

git show < commit-id >

查看这次提交修改的具体内容

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
git reset和rebasegit版本控制中的两个重要命令,用于管理代码提交和修改历史。 git reset命令用于将当前分支的HEAD指针移动到指定的提交,同时可选择性地修改工作目录和暂存区的文件状态。它有三种常用的模式: 1. Soft Reset:使用`git reset --soft <commit>`可以将HEAD指针移动到指定提交,但不修改工作目录和暂存区的文件。这意味着之前的提交会被撤销,但相应的更改会保留在暂存区,可以重新提交。 2. Mixed Reset:使用`git reset --mixed <commit>`是默认的reset模式,它将HEAD指针移动到指定提交,并且重置暂存区,但不修改工作目录的文件。这意味着之前的提交和相应的更改都会被撤销,并且文件状态会回到最近一次提交的状态。 3. Hard Reset:使用`git reset --hard <commit>`会彻底重置当前分支的HEAD指针、暂存区和工作目录到指定提交。这意味着之前的提交和相应的更改都会被彻底删除,请谨慎使用。 git rebase命令用于在当前分支上应用另一个分支上的提交,并将其追加到当前分支的提交历史中。它可以实现分支合并的效果,但是与git merge命令不同,rebase会修改提交历史,使得分支合并后的提交看起来更加线性和整洁。 使用`git rebase <branch>`可以将当前分支的提交应用到指定的分支上,也可以使用交互式(rebase -i)的方式对提交进行修改、合并或删除。 需要注意的是,使用git reset和rebase命令都会修改提交历史,所以在公共分支上使用时要谨慎,以免影响其他开发者的工作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值