|
| 1 | +# Managing Git pushes on master |
| 2 | + |
| 3 | +This document explains how it is possible to approach differently the way a |
| 4 | +commit become part of master, avoiding unwanted merges, due to the automatic |
| 5 | +ways git uses to fix conflicts. |
| 6 | +This can lead to this kind of situation in the master repo: |
| 7 | + |
| 8 | +``` |
| 9 | +| * 0662160cfe1b - (2020-04-01 18:14:18 +0200) Commit #11 <User2> |
| 10 | +| * e880c2c62c6e - (2020-04-01 17:32:34 +0200) Commit #10 <User3> |
| 11 | +| * aa721f8f350a - (2020-04-01 12:36:32 +0200) Commit #9 <User1> |
| 12 | +* | 77ab6238ae72 - (2020-04-02 10:40:03 +0200) Merge branch 'master' of ssh://myserver/var/git/puppet <User2> |
| 13 | +|\ \ |
| 14 | +| * | 6235d4ddad76 - (2020-04-01 18:11:02 +0200) Commit #8 <User1> |
| 15 | +| * | 69e6e5f3a904 - (2020-04-01 18:11:02 +0200) Commit #7 <User1> |
| 16 | +| * | c72dadaa329b - (2020-04-01 18:11:01 +0200) Commit #6 <User1> |
| 17 | +| * | fdd97696aa5d - (2020-04-01 18:11:01 +0200) Commit #5 <User1> |
| 18 | +* | | 2e970166e03b - (2020-04-02 10:36:23 +0200) Commit #3 <User2> |
| 19 | +|/ / |
| 20 | +* | 620258d4da35 - (2020-04-01 10:55:13 +0200) Commit #4 <User2> |
| 21 | +|/ |
| 22 | +* a52f3475907d - (2020-03-31 19:18:11 +0200) Commit #2 <User3> |
| 23 | +* de00acdbbe06 - (2020-03-31 17:31:49 +0200) Commit #1 <User1> |
| 24 | +``` |
| 25 | + |
| 26 | +What a mess, isn't it? |
| 27 | + |
| 28 | +## When there are no changes in the meantime |
| 29 | + |
| 30 | +In theory, if no additional commits are made in between your changes, the |
| 31 | +workflow can be: |
| 32 | + |
| 33 | +0. ```git checkout master``` # ensure you're on master branch |
| 34 | +1. ```git pull``` # get the latest commits from master branch |
| 35 | +2. Make your changes on the files |
| 36 | +3. ```git commit``` # commit changes into the personal local branch |
| 37 | +4. ```git pull``` # get the latest commits for master branch |
| 38 | +5. ```git push``` # push commits on origin/master |
| 39 | + |
| 40 | +## When something changed in the meantime |
| 41 | + |
| 42 | +If during the step 4 nothing happens, then you're good, othewise you will |
| 43 | +be asked to confirm a merge (which cannot be aborted). This means that |
| 44 | +something changed in the repo while you were making changes. |
| 45 | + |
| 46 | +If you accept the merge and push it to master you will find yourself in the |
| 47 | +situation described above, making hard for other developers to understand the |
| 48 | +changes flow. |
| 49 | + |
| 50 | +To have a linear sequence of commits in the master branch, it is possible to |
| 51 | +avoid the merge by using this workflow: |
| 52 | + |
| 53 | +0. ```git checkout master``` # ensure you're on master branch |
| 54 | +1. ```git pull``` # get the latest commits from master branch |
| 55 | +2. Make your changes on the files |
| 56 | +3. ```git commit``` # commit changes into the personal local branch |
| 57 | +4. ```git pull``` # get the latest commits from master with a merge request |
| 58 | + from git, vim or the default editor opens and you must continue |
| 59 | +5. ```git reset --hard HEAD^1``` # remove the merge |
| 60 | +6. ```git log``` # get the id of <yourcommithash> |
| 61 | +7. ```git reset --hard HEAD^1``` # remove your commit from the master branch on |
| 62 | + the local repo |
| 63 | +8. ```git pull``` # get all the new modifications from master (it should not |
| 64 | + ask for any merge) |
| 65 | +9. ```git cherry-pick <yourcommithash>``` # pick your specific commit with hash |
| 66 | +10. ```git push``` # push commits on origin/master |
| 67 | + |
| 68 | +When you've got a single quick commit to push, then the workflow could be a |
| 69 | +little smoother: |
| 70 | + |
| 71 | +0. ```git checkout master``` # ensure you're on master branch |
| 72 | +1. ```git pull``` # get the latest commits from master branch |
| 73 | +2. Make your changes on the files |
| 74 | +3. ```git commit``` # commit changes into the personal local branch |
| 75 | +4. ```git log``` # get the id of <yourcommithash> |
| 76 | +5. ```git reset --hard HEAD^1``` # remove your last commit |
| 77 | +6. ```git pull``` # get the latest commits for master branch |
| 78 | +7. ```git cherry-pick <yourcommithash>``` # pick your specific commit with hash |
| 79 | +8. ```git push``` # push commits on origin/master |
| 80 | + |
| 81 | +## Creating local branches when you have a huge amount of changes |
| 82 | + |
| 83 | +When there's a huge amount of modifications (with related commits) to be done, |
| 84 | +it is useful to create a local branch named after the related topic: |
| 85 | + |
| 86 | +0. ```git checkout master``` # ensure you're on master branch |
| 87 | +1. ```git pull``` # get the latest commits for master branch |
| 88 | +2. ```git checkout -b <mynewfeature>``` # create a new local branch |
| 89 | +3. Make your changes on the files |
| 90 | +4. ```git commit``` # commit changes into the personal local branch |
| 91 | +5. ```git checkout master``` |
| 92 | +6. ```git pull``` # this will pull any new commit made in the meantime on |
| 93 | + master |
| 94 | +7. ```git checkout <mynewfeature>``` # move back to the personal local branch |
| 95 | +8. ```git rebase master``` # put the branch commits on top of the master's one |
| 96 | +9. ```git checkout master``` # go back to local master branch |
| 97 | +10. ```git merge <mynewfeature>``` # merge all the commits of the local branch |
| 98 | + into master |
| 99 | +11. ```git push``` # push commits on origini/master |
| 100 | + |
| 101 | +Rebase it's always the best choice while playing with multiple commits. |
0 commit comments