注意,请不要master 或者重要branch使用这招
假设我们要给commit2 添加 feature A
1.查看commit2的commit id
git log --oneline
如
my-Mac:you$ git log --oneline
01420kk commit4
03fc6kk commit3
2c413kk commit2 # this one
06d4kkk commit1
2.保存featureA
3.stage feature A
4.使用特殊Commit
git commit --fixup commitID
如
my-Mac:you$ git commit --fixup 2c413kk
此时feature A 已经被commit ,如果你再次使用
git log --oneline
你会发现在新增一条commit
如
my-Mac:you$ git log --oneline
7800kkk fixup commit2 #this is your feature A
01420kk commit4
03fc6kk commit3
2c413kk commit2
06d4kkk commit1
5 将featureA 与 commit2 合并
首先你要确定commit2之前的commitID
my-Mac:you$ git log --oneline
7800kkk !fixup commit2
01420kk commit4
03fc6kk commit3
2c413kk commit2
06d4kkk commit1 #this one 06d4kkk
将此ID 带入下面的语句中
git rebase -i --autosquash 前一个commitID
例如
git rebase -i --autosquash 06d4kkk
之后会进入vim,确定合并位置
通常会是这样的
pick 2c413kk commit2
^fixup 7800kkk commit2 #this is feature A
pick 06d4kkk commit1
# Rebase 93cf9cb..8626a82 onto 93cf9cb (13 commands)
#
# 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
一般fixup会加在要修改的commit下面,
如果没问题的就可以保存退出。(:wq)
如果看错了就不保存退出,再做一遍rebase(q!)
一般这样就完成了!
如果你输入
git branch
显示 (no branch rebasing)之类的
输入
git status
查看下一步该做什么,通常使用
git rebase --continue
即可
6.push到远程branch
注意,这里必须要覆盖,如果是普通的push会产生merge和混乱。
git push -f origin yourbranch
如果你一个不小心,看错了commit id ,可以回滚到之前的操作。
git reflog
git reset --hard Obfafd
用reflog来检查id。
用reset来 回滚。