|
| 1 | +--- |
| 2 | +layout: page |
| 3 | +title: Branching and merging |
| 4 | +--- |
| 5 | + |
| 6 | +I touched on just a few things about git. Get yourself going with git and |
| 7 | +github and then start looking at some of the many |
| 8 | +[resources](resources.html). |
| 9 | + |
| 10 | +In particular, pay attention to: [Branching](http://git-scm.com/book/en/Git-Branching-Basic-Branching-and-Merging): |
| 11 | +Create a separate _branch_ to develop a feature (or work on a |
| 12 | +bug) without disturbing the _master_ branch. If it works out, you |
| 13 | +can merge it back into the master; if it doesn't, you can trash it. |
| 14 | +Branching is super easy, so for big projects, you should probably do it more |
| 15 | +often than not. |
| 16 | + |
| 17 | +To create a branch called `new_feature`: |
| 18 | + |
| 19 | + $ git branch new_feature |
| 20 | + |
| 21 | +Then “check it out”: |
| 22 | + |
| 23 | + $ git checkout new_feature |
| 24 | + |
| 25 | +Make various modifications, and then add and commit. |
| 26 | + |
| 27 | +To go back to the master branch, check it out: |
| 28 | + |
| 29 | + $ git checkout master |
| 30 | + |
| 31 | +To push the branch to github, use this: |
| 32 | + |
| 33 | + $ git push origin new_feature |
| 34 | + |
| 35 | +If you make changes to the master branch, you'll want to merge them |
| 36 | +into your exploratory one: |
| 37 | + |
| 38 | + $ git checkout new_feature |
| 39 | + $ git merge master |
| 40 | + |
| 41 | +If you're satisfied with your changes in the exploratory branch, merge |
| 42 | +them into the master: |
| 43 | + |
| 44 | + $ git checkout master |
| 45 | + $ git merge new_feature |
| 46 | + |
| 47 | +If you're done with the branch and want to delete it: |
| 48 | + |
| 49 | + $ git branch -d new_feature |
| 50 | + |
| 51 | +But if you pushed it to github, it will still exist there. This is |
| 52 | +how to delete the branch from github: |
| 53 | + |
| 54 | + $ git push origin --delete new_feature |
| 55 | + |
| 56 | +After pulling from github, use the following to get access to a branch |
| 57 | +that is only on github: |
| 58 | + |
| 59 | + $ git checkout -b new_feature origin/new_feature |
| 60 | + |
| 61 | +If you want to pull a particular branch from a collaborator's |
| 62 | +repository, do this: |
| 63 | + |
| 64 | + $ git checkout new_feature |
| 65 | + $ git pull myfriend new_feature |
| 66 | + |
| 67 | +One final point: note that |
| 68 | +[`git pull`](https://www.kernel.org/pub/software/scm/git/docs/git-pull.html) |
| 69 | +is really doing a |
| 70 | +[`git fetch`](https://www.kernel.org/pub/software/scm/git/docs/git-fetch.html) |
| 71 | +followed by a |
| 72 | +[`git merge`](https://www.kernel.org/pub/software/scm/git/docs/git-merge.html). |
0 commit comments