Git merge 和 rebase 进一步比较
但是 假如 我不想看到 分支转折点呢
合并的分支始终会存在一个交叉点
1 Microsoft Windows [版本 10.0.17134.345] 2 (c) 2018 Microsoft Corporation。保留所有权利。 3 4 C:\Users\zhangyang\Desktop\b>git init 5 Initialized empty Git repository in C:/Users/zhangyang/Desktop/b/.git/ 6 7 C:\Users\zhangyang\Desktop\b>git commit -m "Init Commit" --allow-empty 8 [master (root-commit) d1be01c] Init Commit 9 10 C:\Users\zhangyang\Desktop\b>git checkout -b branch1 11 Switched to a new branch 'branch1' 12 13 C:\Users\zhangyang\Desktop\b>git checkout -b branch2 14 Switched to a new branch 'branch2' 15 16 C:\Users\zhangyang\Desktop\b>git checkout -b branch3 17 Switched to a new branch 'branch3' 18 19 C:\Users\zhangyang\Desktop\b>git log --oneline --graph --decorate --all 20 * d1be01c (HEAD -> branch3, master, branch2, branch1) Init Commit 21 22 C:\Users\zhangyang\Desktop\b>git commit -m "C1 on branch3" --allow-empty 23 [branch3 ca874a1] C1 on branch3 24 25 C:\Users\zhangyang\Desktop\b>git commit -m "C2 on branch3" --allow-empty 26 [branch3 a21efa5] C2 on branch3 27 28 C:\Users\zhangyang\Desktop\b>git commit -m "C3 on branch3" --allow-empty 29 [branch3 67c01b8] C3 on branch3 30 31 C:\Users\zhangyang\Desktop\b>git checkout branch2 32 Switched to branch 'branch2' 33 34 C:\Users\zhangyang\Desktop\b>git commit -m "C4 on branch2" --allow-empty 35 [branch2 2ca7365] C4 on branch2 36 37 C:\Users\zhangyang\Desktop\b>git commit -m "C5 on branch2" --allow-empty 38 [branch2 5c6f50e] C5 on branch2 39 40 C:\Users\zhangyang\Desktop\b>git commit -m "C6 on branch2" --allow-empty 41 [branch2 4746581] C6 on branch2 42 43 C:\Users\zhangyang\Desktop\b>git checkout branch1 44 Switched to branch 'branch1' 45 46 C:\Users\zhangyang\Desktop\b>git commit -m "C7 on branch2" --allow-empty 47 [branch1 7890d59] C7 on branch2 48 49 C:\Users\zhangyang\Desktop\b>git commit -m "C8 on branch2" --allow-empty 50 [branch1 713a602] C8 on branch2 51 52 C:\Users\zhangyang\Desktop\b>git commit -m "C9 on branch2" --allow-empty 53 [branch1 28c5cc3] C9 on branch2 54 55 C:\Users\zhangyang\Desktop\b>git log 56 commit 28c5cc366857090fec62621a646ec0f93f17d727 (HEAD -> branch1) 57 Author: yqmcu <yqmcu@foxmail.com> 58 Date: Tue Feb 26 08:42:19 2019 +0800 59 60 C9 on branch2 61 62 commit 713a602ef4a45a43fe9b4b62cdd7f970b12e6b30 63 Author: yqmcu <yqmcu@foxmail.com> 64 Date: Tue Feb 26 08:42:16 2019 +0800 65 66 C8 on branch2 67 68 commit 7890d598abfc63c4940faca451abd017d0b0d574 69 Author: yqmcu <yqmcu@foxmail.com> 70 Date: Tue Feb 26 08:42:13 2019 +0800 71 72 C7 on branch2 73 74 commit d1be01c45a906b4325388e465e13aa4269718773 (master) 75 Author: yqmcu <yqmcu@foxmail.com> 76 Date: Tue Feb 26 08:39:11 2019 +0800 77 78 Init Commit 79 80 C:\Users\zhangyang\Desktop\b>git log --oneline --graph --all --decorate 81 * 28c5cc3 (HEAD -> branch1) C9 on branch2 82 * 713a602 C8 on branch2 83 * 7890d59 C7 on branch2 84 | * 4746581 (branch2) C6 on branch2 85 | * 5c6f50e C5 on branch2 86 | * 2ca7365 C4 on branch2 87 |/ 88 | * 67c01b8 (branch3) C3 on branch3 89 | * a21efa5 C2 on branch3 90 | * ca874a1 C1 on branch3 91 |/ 92 * d1be01c (master) Init Commit 93 94 C:\Users\zhangyang\Desktop\b>git reset d1be01c --hard 95 HEAD is now at d1be01c Init Commit 96 97 C:\Users\zhangyang\Desktop\b>git log --oneline --graph --all --decorate 98 * 4746581 (branch2) C6 on branch2 99 * 5c6f50e C5 on branch2 100 * 2ca7365 C4 on branch2 101 | * 67c01b8 (branch3) C3 on branch3 102 | * a21efa5 C2 on branch3 103 | * ca874a1 C1 on branch3 104 |/ 105 * d1be01c (HEAD -> branch1, master) Init Commit 106 107 C:\Users\zhangyang\Desktop\b>git commit -m "C7 on branch1" --allow-empty 108 [branch1 07ace42] C7 on branch1 109 110 C:\Users\zhangyang\Desktop\b>git commit -m "C7 on branch2" --allow-empty 111 [branch1 c85e1c1] C7 on branch2 112 113 C:\Users\zhangyang\Desktop\b>git commit --amend 114 You asked to amend the most recent commit, but doing so would make 115 it empty. You can repeat your command with --allow-empty, or you can 116 remove the commit entirely with "git reset HEAD^". 117 On branch branch1 118 No changes 119 120 C:\Users\zhangyang\Desktop\b>git commit --amend --allow-empty 121 [branch1 96a66a6] C7 on branch1 122 Date: Tue Feb 26 08:43:29 2019 +0800 123 124 C:\Users\zhangyang\Desktop\b>git log --oneline --graph --all --decorate 125 * 96a66a6 (HEAD -> branch1) C7 on branch1 126 * 07ace42 C7 on branch1 127 | * 4746581 (branch2) C6 on branch2 128 | * 5c6f50e C5 on branch2 129 | * 2ca7365 C4 on branch2 130 |/ 131 | * 67c01b8 (branch3) C3 on branch3 132 | * a21efa5 C2 on branch3 133 | * ca874a1 C1 on branch3 134 |/ 135 * d1be01c (master) Init Commit 136 137 C:\Users\zhangyang\Desktop\b>git commit --amend --allow-empty 138 [branch1 abc5b64] C8 on branch1 139 Date: Tue Feb 26 08:43:29 2019 +0800 140 141 C:\Users\zhangyang\Desktop\b>git log --oneline --graph --all --decorate 142 * abc5b64 (HEAD -> branch1) C8 on branch1 143 * 07ace42 C7 on branch1 144 | * 4746581 (branch2) C6 on branch2 145 | * 5c6f50e C5 on branch2 146 | * 2ca7365 C4 on branch2 147 |/ 148 | * 67c01b8 (branch3) C3 on branch3 149 | * a21efa5 C2 on branch3 150 | * ca874a1 C1 on branch3 151 |/ 152 * d1be01c (master) Init Commit 153 154 C:\Users\zhangyang\Desktop\b>git commit -m "C9 on branch1" --allow-empty 155 [branch1 f5f2d95] C9 on branch1 156 157 C:\Users\zhangyang\Desktop\b>git log --oneline --graph --all --decorate 158 * f5f2d95 (HEAD -> branch1) C9 on branch1 159 * abc5b64 C8 on branch1 160 * 07ace42 C7 on branch1 161 | * 4746581 (branch2) C6 on branch2 162 | * 5c6f50e C5 on branch2 163 | * 2ca7365 C4 on branch2 164 |/ 165 | * 67c01b8 (branch3) C3 on branch3 166 | * a21efa5 C2 on branch3 167 | * ca874a1 C1 on branch3 168 |/ 169 * d1be01c (master) Init Commit 170 171 C:\Users\zhangyang\Desktop\b>git merge branch3 172 Already up to date! 173 Merge made by the 'recursive' strategy. 174 175 C:\Users\zhangyang\Desktop\b>git log --oneline --graph --all --decorate 176 * f75db08 (HEAD -> branch1) Merge branch 'branch3' into branch1 177 |\ 178 | * 67c01b8 (branch3) C3 on branch3 179 | * a21efa5 C2 on branch3 180 | * ca874a1 C1 on branch3 181 * | f5f2d95 C9 on branch1 182 * | abc5b64 C8 on branch1 183 * | 07ace42 C7 on branch1 184 |/ 185 | * 4746581 (branch2) C6 on branch2 186 | * 5c6f50e C5 on branch2 187 | * 2ca7365 C4 on branch2 188 |/ 189 * d1be01c (master) Init Commit 190 191 C:\Users\zhangyang\Desktop\b>git checkout branch2 192 Switched to branch 'branch2' 193 194 C:\Users\zhangyang\Desktop\b>git rebase branch3 195 First, rewinding head to replay your work on top of it... 196 197 C:\Users\zhangyang\Desktop\b>git log --oneline --graph --all --decorate 198 * f75db08 (branch1) Merge branch 'branch3' into branch1 199 |\ 200 | * 67c01b8 (HEAD -> branch2, branch3) C3 on branch3 201 | * a21efa5 C2 on branch3 202 | * ca874a1 C1 on branch3 203 * | f5f2d95 C9 on branch1 204 * | abc5b64 C8 on branch1 205 * | 07ace42 C7 on branch1 206 |/ 207 * d1be01c (master) Init Commit 208 209
git rebase 在修改 commit 提交内容上发挥了很好的作用
它不会像merge一样还会给你造成冲突conflicx
1 C:\Users\zhangyang\Desktop\c>git rebase a8404dc 2 First, rewinding head to replay your work on top of it... 3 4 C:\Users\zhangyang\Desktop\c>git log --oneline --graph 5 * a8404dc (HEAD, rename) C1 on branch1 amend return to modify 6 * abd1ba4 (master) Init Commit 7 8 C:\Users\zhangyang\Desktop\c>git checkout branch1 9 Previous HEAD position was a8404dc C1 on branch1 amend return to modify 10 Switched to branch 'branch1' 11 12 C:\Users\zhangyang\Desktop\c>git log --oneline --graph 13 * 67297ca (HEAD -> branch1) C3 on branch1 14 * a23adfc C2 on branch1 15 * 7d5f43e C1 on branch1 16 * abd1ba4 (master) Init Commit 17 18 C:\Users\zhangyang\Desktop\c>git rebase rename 19 First, rewinding head to replay your work on top of it... 20 Applying: C2 on branch1 21 Applying: C3 on branch1 22 23 C:\Users\zhangyang\Desktop\c>git log --oneline --graph 24 * d639db7 (HEAD -> branch1) C3 on branch1 25 * 5f2ecaa C2 on branch1 26 * a8404dc (rename) C1 on branch1 amend return to modify 27 * abd1ba4 (master) Init Commit 28 29 C:\Users\zhangyang\Desktop\c>git checkout 5f2ecaa 30 Note: checking out '5f2ecaa'. 31 32 You are in 'detached HEAD' state. You can look around, make experimental 33 changes and commit them, and you can discard any commits you make in this 34 state without impacting any branches by performing another checkout. 35 36 If you want to create a new branch to retain commits you create, you may 37 do so (now or later) by using -b with the checkout command again. Example: 38 39 git checkout -b <new-branch-name> 40 41 HEAD is now at 5f2ecaa C2 on branch1 42 43 C:\Users\zhangyang\Desktop\c>git commit --amend 44 [detached HEAD 4b7fcad] C2 on branch1 amend return to modify 45 Date: Tue Feb 26 10:26:34 2019 +0800 46 1 file changed, 1 insertion(+) 47 48 C:\Users\zhangyang\Desktop\c>git checkout branch1 49 Warning: you are leaving 1 commit behind, not connected to 50 any of your branches: 51 52 4b7fcad C2 on branch1 amend return to modify 53 54 If you want to keep it by creating a new branch, this may be a good time 55 to do so with: 56 57 git branch <new-branch-name> 4b7fcad 58 59 Switched to branch 'branch1' 60 61 C:\Users\zhangyang\Desktop\c>git merge 4d7fcad 62 merge: 4d7fcad - not something we can merge 63 64 C:\Users\zhangyang\Desktop\c>git rebase 4d7fcad 65 fatal: Needed a single revision 66 invalid upstream '4d7fcad' 67 68 C:\Users\zhangyang\Desktop\c>git branch 69 * branch1 70 master 71 rename 72 73 C:\Users\zhangyang\Desktop\c>git branch -d rename 74 Deleted branch rename (was a8404dc). 75 76 C:\Users\zhangyang\Desktop\c>git branch rename 4d7fcad 77 fatal: Not a valid object name: '4d7fcad'. 78 79 C:\Users\zhangyang\Desktop\c>git branch rename 4b7fcad 80 81 C:\Users\zhangyang\Desktop\c>git checkout branch1 82 Already on 'branch1' 83 84 C:\Users\zhangyang\Desktop\c>git merge rename 85 Auto-merging log 86 CONFLICT (content): Merge conflict in log 87 Automatic merge failed; fix conflicts and then commit the result. 88 89 C:\Users\zhangyang\Desktop\c>git status 90 On branch branch1 91 You have unmerged paths. 92 (fix conflicts and run "git commit") 93 (use "git merge --abort" to abort the merge) 94 95 Unmerged paths: 96 (use "git add <file>..." to mark resolution) 97 98 both modified: log 99 100 no changes added to commit (use "git add" and/or "git commit -a") 101 102 C:\Users\zhangyang\Desktop\c>git merge --abort 103 104 C:\Users\zhangyang\Desktop\c>git rebase rename 105 First, rewinding head to replay your work on top of it... 106 Applying: C3 on branch1 107 108 C:\Users\zhangyang\Desktop\c>git log --oneline --graph 109 * 0b3cc39 (HEAD -> branch1) C3 on branch1 110 * 4b7fcad (rename) C2 on branch1 amend return to modify 111 * a8404dc C1 on branch1 amend return to modify 112 * abd1ba4 (master) Init Commit 113 114 C:\Users\zhangyang\Desktop\c>git reflog 115 0b3cc39 (HEAD -> branch1) HEAD@{0}: rebase finished: returning to refs/heads/branch1 116 0b3cc39 (HEAD -> branch1) HEAD@{1}: rebase: C3 on branch1 117 4b7fcad (rename) HEAD@{2}: rebase: checkout rename 118 d639db7 HEAD@{3}: reset: moving to HEAD 119 d639db7 HEAD@{4}: checkout: moving from branch1 to branch1 120 d639db7 HEAD@{5}: checkout: moving from 4b7fcad85c9476434c25ed17436e7fa9efa74afb to branch1 121 4b7fcad (rename) HEAD@{6}: commit (amend): C2 on branch1 amend return to modify 122 5f2ecaa HEAD@{7}: checkout: moving from branch1 to 5f2ecaa 123 d639db7 HEAD@{8}: rebase finished: returning to refs/heads/branch1 124 d639db7 HEAD@{9}: rebase: C3 on branch1 125 5f2ecaa HEAD@{10}: rebase: C2 on branch1 126 a8404dc HEAD@{11}: rebase: checkout rename 127 67297ca HEAD@{12}: checkout: moving from a8404dcdc9edf2700d3cafd3136670c1140eeb9f to branch1 128 a8404dc HEAD@{13}: rebase: checkout a8404dc 129 7d5f43e HEAD@{14}: checkout: moving from branch1 to 7d5f43e 130 67297ca HEAD@{15}: checkout: moving from a8404dcdc9edf2700d3cafd3136670c1140eeb9f to branch1 131 a8404dc HEAD@{16}: commit (amend): C1 on branch1 amend return to modify 132 7d5f43e HEAD@{17}: checkout: moving from branch1 to 7d5f43e 133 67297ca HEAD@{18}: commit (amend): C3 on branch1 134 8f51c6b HEAD@{19}: commit: C2 on branch1 135 a23adfc HEAD@{20}: commit: C2 on branch1 136 7d5f43e HEAD@{21}: commit: C1 on branch1 137 abd1ba4 (master) HEAD@{22}: checkout: moving from master to branch1 138 abd1ba4 (master) HEAD@{23}: commit (initial): Init Commit