GIT 后悔药:将一个commit squash到以前的指定commit

原创
2013/08/29 14:48
阅读数 6.9K
ryanel@apt-sz05:~/SDK/git-test$ git log
commit 14b46796f1105a394d2d1bf8bb0c585ab5517cdd
Author: Ryane Luo <ryanel@xxxx.com>
Date:   Thu Aug 29 14:08:25 2013 +0800

    8th commit

commit 7ba550fcabd0a288dded2ad35d7325e3f5fe68ea
Author: Ryane Luo <ryanel@ xxxx.com>
Date:   Thu Aug 29 14:08:14 2013 +0800

    7th commit

commit fdc8afa036ffd74dcb07473a8c16988d666beddc
Author: Ryane Luo <ryanel@ xxxx.com>
Date:   Thu Aug 29 11:53:44 2013 +0800

    6th commit

commit f9a347e257f12bfa3ff1b5021976b0ed1bd4c3d8
Author: Ryane Luo <ryanel@ xxxx.com>
Date:   Thu Aug 29 11:48:02 2013 +0800

    4th commit

commit 2dc4726b22b6716dd35538f494a6b487f8d3b2f5
Author: Ryane Luo <ryanel@ xxxx.com>
Date:   Thu Aug 29 11:47:24 2013 +0800

    2nd and 3rd commit has been merged to this one

commit 373e6ae31cb5648ca8a1d1b51e490733b75c79ea
Author: Ryane Luo <ryanel@ xxxx.com>
Date:   Thu Aug 29 11:45:21 2013 +0800

    init commit

ryanel@apt-sz05:~/SDK/git-test$ cat foo.txt
init line of file
2nd line of file
3rd line of file
4th line of file
5th line of file
6th line of file
7th line of file
8th line of file


#expect that squash 8th to 6th
#and keep 7th


git rebase -i HEAD~2


pick 7ba550f 7th commit
pick 14b4679 8th commit


s 14b4679 8th commit
pick 7ba550f 7th commit


ryanel@apt-sz05:~/SDK/git-test$ git rebase -i HEAD~2
Cannot 'squash' without a previous commit


#squash need an previous commit, abort it 


ryanel@apt-sz05:~/SDK/git-test$ git rebase --abort
#do it again with 6th
ryanel@apt-sz05:~/SDK/git-test$ git rebase -i HEAD~3




pick fdc8afa 6th commit
s 14b4679 8th commit
pick 7ba550f 7th commit


#conflict like that 
#commit 8th include the 7th, why ?


init line of file
2nd line of file
3rd line of file
4th line of file
5th line of file
6th line of file
<<<<<<< HEAD
=======
7th line of file
8th line of file
>>>>>>> 14b4679... 8th commit


#at this commit we don't need 7th appers
#change it like that 
init line of file
2nd line of file
3rd line of file
4th line of file
5th line of file
6th line of file
8th line of file




ryanel@apt-sz05:~/SDK/git-test$ git status
# Not currently on any branch.
# You are currently rebasing.
#   (fix conflicts and then run "git rebase --continue")
#   (use "git rebase --skip" to skip this patch)
#   (use "git rebase --abort" to check out the original branch)
#
# Unmerged paths:
#   (use "git reset HEAD <file>..." to unstage)
#   (use "git add <file>..." to mark resolution)
#
#       both modified:      foo.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
ryanel@apt-sz05:~/SDK/git-test$ git rebase --continue
foo.txt: needs merge
You must edit all merge conflicts and then
mark them as resolved using git add


ryanel@apt-sz05:~/SDK/git-test$ git add foo.txt
ryanel@apt-sz05:~/SDK/git-test$ git rebase --continue
[detached HEAD 4a579b2] 6th commit and
 1 file changed, 3 insertions(+)
error: could not apply 7ba550f... 7th commit


When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".
Could not apply 7ba550f... 7th commit


#we get conflict like this, yes git think 7th and 8th is a conflict


init line of file
2nd line of file
3rd line of file
4th line of file
5th line of file
6th line of file
<<<<<<< HEAD
8th line of file
=======
7th line of file
>>>>>>> 7ba550f... 7th commit


#fix it like that


init line of file
2nd line of file
3rd line of file
4th line of file
5th line of file
6th line of file
8th line of file
7th line of file


ryanel@apt-sz05:~/SDK/git-test$ git add foo.txt
ryanel@apt-sz05:~/SDK/git-test$ git rebase --continue
[detached HEAD 90749c0] 7th commit ,which has been move beyond 8th
 1 file changed, 1 insertion(+)
Successfully rebased and updated refs/heads/master.
ryanel@apt-sz05:~/SDK/git-test$
ryanel@apt-sz05:~/SDK/git-test$
ryanel@apt-sz05:~/SDK/git-test$ git log
commit 90749c0458d7fc43a2f4806976cb52f3231a6fe5
Author: Ryane Luo <ryanel@nvidia.com>
Date:   Thu Aug 29 14:08:14 2013 +0800


    7th commit ,which has been move beyond 8th


commit 4a579b27b98d3f67a0fe81dfe501ca4935beccc2
Author: Ryane Luo <ryanel@nvidia.com>
Date:   Thu Aug 29 11:53:44 2013 +0800


    6th commit and


    the 8th commit have been move and merge to 6th


commit f9a347e257f12bfa3ff1b5021976b0ed1bd4c3d8
Author: Ryane Luo <ryanel@nvidia.com>
Date:   Thu Aug 29 11:48:02 2013 +0800


    4th commit


commit 2dc4726b22b6716dd35538f494a6b487f8d3b2f5
Author: Ryane Luo <ryanel@nvidia.com>
Date:   Thu Aug 29 11:47:24 2013 +0800


    2nd and 3rd commit has been merged to this one


commit 373e6ae31cb5648ca8a1d1b51e490733b75c79ea
Author: Ryane Luo <ryanel@nvidia.com>
Date:   Thu Aug 29 11:45:21 2013 +0800


    init commit


#conclusion 

#it you want squash a commit to a previous commit.
#you should do this :
#git rebase -i HEAD~n
#which n = the previous commit offset of HEAD.
#if these is some other commit between them.
#all conflict you need to solve it manually.


展开阅读全文
git
打赏
0
4 收藏
分享
加载中
更多评论
打赏
0 评论
4 收藏
0
分享
返回顶部
顶部