These are some visual git operations explanations I found. Enjoy
Git Reset:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Assumption: Currently checked out branch is master and you have: A - B - C - D - E - F - G - H master Then `git branch newbranch` gives you: master A - B - C - D - E - F - G - H newbranch and `git reset --hard C` gives you: A - B - C master \ \ D - E - F - G - H newbranch |
Simple Rebase:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Initial state: ============== A - B - C - D - E - F - G - H master \ \ I - J - K - L feature REBASE: After git checkout feature; git rebase master (or git rebase master feature): ===================================================== A - B - C - D - E - F - G - H master \ \ I' - J' - K' - L' feature |
Merge vs Rebase:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
Initial state: ============== A - B - C - D - E - F - G - H master \ \ I - J - K - L feature REBASE: After git checkout feature; git rebase master: ====================================================== A - B - C - D - E - F - G - H master \ \ I' - J' - K' - L' feature After git checkout master; git merge feature: --------------------------------------------- A - B - C - D - E - F - G - H \ \ master I' - J' - K' - L' feature aka. master A - B - C - D - E - F - G - H - I' - J' - K' - L' feature MERGE: After git checkout master; git merge feature: ==================================================== A - B - C - D - E - F - G - H - M master \ / \ / I - J - K - L feature |
Merge vs Squash:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
Initial state: ============== A - B - C - D - E - F - G - H master \ \ I - J - K - L feature SQUASH: After git checkout master; git merge --squash feature ============================================================== A - B - C - D - E - F - G - H - N master \ \ I - J - K - L feature N is a combination of I, J, K and L (they are squashed) After git branch -D feature: ============================ A - B - C - D - E - F - G - H - N master MERGE: After git checkout master; git merge feature: ==================================================== A - B - C - D - E - F - G - H - M master \ / \ / I - J - K - L feature After git branch -D feature: ============================ A - B - C - D - E - F - G - H - M master \ / \ / I - J - K - L M is merge commit. From there you can look at I, J, K and L |
Rebase from origin/master:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Initial state: ============== A - B - C - D - E - F - G - H origin/master \ \ I - J - K - L master REBASE: After git checkout master; git rebase origin/master (or git rebase origin/master master): ===================================================== A - B - C - D - E - F - G - H origin/master \ \ I' - J' - K' - L' master |
Pull and Rebase from origin/master:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
Initial state: ============== A - B - C - D - E - F - G - H origin/master \ \ I - J - K - L master MERGE: After a) git pull b) git fetch && git merge origin/master: ======================================== A - B - C - D - E - F - G - H origin/master \ \ \ \ I - J - K - L - M master REBASE: After a) git pull --rebase b) git fetch && git rebase origin/master: ========================================= A - B - C - D - E - F - G - H origin/master \ \ I' - J' - K' - L' master |
Rebase and Pull ending with 2 branches:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
Initial state: ============== A - B - C - D - E - F - G - H master \ \ I - J - K - L feature Assume both branches are pushed. REBASE: After git checkout feature; git rebase master (or git rebase master feature): ===================================================== A - B - C - D - E - F - G - H master \ \ I' - J' - K' - L' feature Now if you try to push without -f you get an error telling you to pull before pushing. So if you follow that advice... PULL: git pull ============== A - B - C - D - E - F - G - H master \ \ \ \ \ I' - J' - K' - L' - M feature \ / \ / `- I - J - K - L - - - - - - -' origin/feature You end up with a merge commit that merges the "old" and the "new" branch. |
Rebase onto:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
Initial state: ============== A - B - C - D - E - F - G - H master \ \ I - J - K - L branchA \ \ M - N - O branchB EITHER: After git checkout branchB; git rebase master (or git rebase master branchB): ===================================================== A - B - C - D - E - F - G - H master \ \ \ \ \ I' - J' - M' - N' - O' branchB \ I - J - K - L branchA OR: After git checkout branchB; git rebase --onto master branchA (or git rebase --onto master branchA branchB) ================================================================ A - B - C - D - E - F - G - H master \ \ \ \ \ M' - N' - O' branchB \ I - J - K - L branchA |
Rebase onto and onto again:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
Initial state: ============== A - B - C - D - E - F - G - H master \ \ I - J - K - L branchA \ \ M - N - O branchB EITHER: After git checkout branchA; git rebase master (or git rebase master branchB): ===================================================== A - B - C - D - E - F - G - H master \ \ \ \ I - J I' - J' - K' - L' branchA \ \ M - N - O branchB OR: After git checkout branchB; git rebase branchA (or git rebase branchA branchB) ================================================== A - B - C - D - E - F - G - H master \ \ I' - J' - K' - L' branchA \ \ M' - N' - O' branchB (assuming I' is similar enough to I and J' is similar enough to J that Git can recognize them as equivalent) |