|
1 |
| -##Basic Branching and Merging |
| 1 | +## Basic Branching and Merging ## |
| 2 | + |
| 3 | +A single git repository can maintain multiple branches of |
| 4 | +development. To create a new branch named "experimental", use |
| 5 | + |
| 6 | + $ git branch experimental |
| 7 | + |
| 8 | +If you now run |
| 9 | + |
| 10 | + $ git branch |
| 11 | + |
| 12 | +you'll get a list of all existing branches: |
| 13 | + |
| 14 | + experimental |
| 15 | + * master |
| 16 | + |
| 17 | +The "experimental" branch is the one you just created, and the |
| 18 | +"master" branch is a default branch that was created for you |
| 19 | +automatically. The asterisk marks the branch you are currently on; |
| 20 | +type |
| 21 | + |
| 22 | + $ git checkout experimental |
| 23 | + |
| 24 | +to switch to the experimental branch. Now edit a file, commit the |
| 25 | +change, and switch back to the master branch: |
| 26 | + |
| 27 | + (edit file) |
| 28 | + $ git commit -a |
| 29 | + $ git checkout master |
| 30 | + |
| 31 | +Check that the change you made is no longer visible, since it was |
| 32 | +made on the experimental branch and you're back on the master branch. |
| 33 | + |
| 34 | +You can make a different change on the master branch: |
| 35 | + |
| 36 | + (edit file) |
| 37 | + $ git commit -a |
| 38 | + |
| 39 | +at this point the two branches have diverged, with different changes |
| 40 | +made in each. To merge the changes made in experimental into master, run |
| 41 | + |
| 42 | + $ git merge experimental |
| 43 | + |
| 44 | +If the changes don't conflict, you're done. If there are conflicts, |
| 45 | +markers will be left in the problematic files showing the conflict; |
| 46 | + |
| 47 | + $ git diff |
| 48 | + |
| 49 | +will show this. Once you've edited the files to resolve the |
| 50 | +conflicts, |
| 51 | + |
| 52 | + $ git commit -a |
| 53 | + |
| 54 | +will commit the result of the merge. Finally, |
| 55 | + |
| 56 | + $ gitk |
| 57 | + |
| 58 | +will show a nice graphical representation of the resulting history. |
| 59 | + |
| 60 | +At this point you could delete the experimental branch with |
| 61 | + |
| 62 | + $ git branch -d experimental |
| 63 | + |
| 64 | +This command ensures that the changes in the experimental branch are |
| 65 | +already in the current branch. |
| 66 | + |
| 67 | +If you develop on a branch crazy-idea, then regret it, you can always |
| 68 | +delete the branch with |
| 69 | + |
| 70 | + $ git branch -D crazy-idea |
| 71 | + |
| 72 | +Branches are cheap and easy, so this is a good way to try something |
| 73 | +out. |
| 74 | + |
| 75 | +### How to merge ### |
| 76 | + |
| 77 | +You can rejoin two diverging branches of development using |
| 78 | +linkgit:git-merge[1]: |
| 79 | + |
| 80 | + $ git merge branchname |
| 81 | + |
| 82 | +merges the development in the branch "branchname" into the current |
| 83 | +branch. If there are conflicts--for example, if the same file is |
| 84 | +modified in two different ways in the remote branch and the local |
| 85 | +branch--then you are warned; the output may look something like this: |
| 86 | + |
| 87 | + $ git merge next |
| 88 | + 100% (4/4) done |
| 89 | + Auto-merged file.txt |
| 90 | + CONFLICT (content): Merge conflict in file.txt |
| 91 | + Automatic merge failed; fix conflicts and then commit the result. |
| 92 | + |
| 93 | +Conflict markers are left in the problematic files, and after |
| 94 | +you resolve the conflicts manually, you can update the index |
| 95 | +with the contents and run git commit, as you normally would when |
| 96 | +creating a new file. |
| 97 | + |
| 98 | +If you examine the resulting commit using gitk, you will see that it |
| 99 | +has two parents, one pointing to the top of the current branch, and |
| 100 | +one to the top of the other branch. |
| 101 | + |
| 102 | +### Resolving a merge ### |
| 103 | + |
| 104 | +When a merge isn't resolved automatically, git leaves the index and |
| 105 | +the working tree in a special state that gives you all the |
| 106 | +information you need to help resolve the merge. |
| 107 | + |
| 108 | +Files with conflicts are marked specially in the index, so until you |
| 109 | +resolve the problem and update the index, linkgit:git-commit[1] will |
| 110 | +fail: |
| 111 | + |
| 112 | + $ git commit |
| 113 | + file.txt: needs merge |
| 114 | + |
| 115 | +Also, linkgit:git-status[1] will list those files as "unmerged", and the |
| 116 | +files with conflicts will have conflict markers added, like this: |
| 117 | + |
| 118 | + <<<<<<< HEAD:file.txt |
| 119 | + Hello world |
| 120 | + ======= |
| 121 | + Goodbye |
| 122 | + >>>>>>> 77976da35a11db4580b80ae27e8d65caf5208086:file.txt |
| 123 | + |
| 124 | +All you need to do is edit the files to resolve the conflicts, and then |
| 125 | + |
| 126 | + $ git add file.txt |
| 127 | + $ git commit |
| 128 | + |
| 129 | +Note that the commit message will already be filled in for you with |
| 130 | +some information about the merge. Normally you can just use this |
| 131 | +default message unchanged, but you may add additional commentary of |
| 132 | +your own if desired. |
| 133 | + |
| 134 | +The above is all you need to know to resolve a simple merge. But git |
| 135 | +also provides more information to help resolve conflicts: |
| 136 | + |
| 137 | +### Undoing a merge ### |
| 138 | + |
| 139 | +If you get stuck and decide to just give up and throw the whole mess |
| 140 | +away, you can always return to the pre-merge state with |
| 141 | + |
| 142 | + $ git reset --hard HEAD |
| 143 | + |
| 144 | +Or, if you've already committed the merge that you want to throw away, |
| 145 | + |
| 146 | + $ git reset --hard ORIG_HEAD |
| 147 | + |
| 148 | +However, this last command can be dangerous in some cases--never |
| 149 | +throw away a commit you have already committed if that commit may |
| 150 | +itself have been merged into another branch, as doing so may confuse |
| 151 | +further merges. |
| 152 | + |
| 153 | +### Fast-forward merges ### |
| 154 | + |
| 155 | +There is one special case not mentioned above, which is treated |
| 156 | +differently. Normally, a merge results in a merge commit, with two |
| 157 | +parents, one pointing at each of the two lines of development that |
| 158 | +were merged. |
| 159 | + |
| 160 | +However, if the current branch is a descendant of the other--so every |
| 161 | +commit present in the one is already contained in the other--then git |
| 162 | +just performs a "fast forward"; the head of the current branch is moved |
| 163 | +forward to point at the head of the merged-in branch, without any new |
| 164 | +commits being created. |
0 commit comments