|
1 | 1 | # Managing Git Branches |
| 2 | + |
2 | 3 | ## Learning Goals |
3 | 4 | - Explore how Git tracks _branches_ |
4 | 5 | - Learn common uses of the `checkout` command for managing _branches_ |
5 | 6 | - Examine the difference between _merge_ and _rebase_ |
6 | 7 | - Discuss when to _branch_, when to _merge_, and when to _rebase_ |
7 | | -- Learn about Pull Requests for branches. |
8 | 8 |
|
9 | 9 | ## Branches |
10 | 10 | Much of this discussion is going to happen on the whiteboard with sticky notes and drawing. However, here's a list of handy Git commands for working with branches: |
11 | 11 |
|
12 | | -### File Commands |
13 | | -- `git checkout [path/to/file]`: reverts any unstaged changes to the specified file(s) to their last committed state. |
14 | | -- `git checkout .`: reverts all unstaged changes to tracked files in the current direstory to their last committed state. |
15 | | -- `git stash -k`: set aside unstaged changes into a stash. |
16 | | -- `git stash`: set aside _all_ changes into a stash. |
17 | | -- `git stash pop`: apply the most recently (unapplied) stash. |
18 | | - |
19 | | -(Changes that haven't been `add`ed are unstaged.) |
20 | | - |
21 | | -### Branch Maintenance Commands |
22 | | -- `git checkout [branch_name]`: switches Git to an _existing_ branch. |
23 | | -- `git branch [branch_name]`: creates a new branch _but does not switch to it_. |
24 | | -- `git checkout -b [branch_name]`: creates a new branch and switches Git to this _new_ branch. |
25 | | -- `git branch -a`: shows a list of all local and remote branches. |
26 | | -- `git branch -d [branch_name]`: delete the specified local branch. |
27 | | - |
28 | 12 | ### Branch Sharing Commands |
29 | 13 |
|
30 | 14 | #### Update a Remote Branch (from your local branch) |
@@ -94,64 +78,27 @@ $ git merge master |
94 | 78 |
|
95 | 79 | This won't always be possible in practice but it's a good guiding principle and will make your life easier if you can stick to it as much as possible. |
96 | 80 |
|
97 | | -### Pull Requests |
98 | | - |
99 | | -You've dealt with Pull Requests between forks before, however they are also extremely helpful when using branches as well! |
100 | | - |
101 | | -#### Creating a Pull Request |
102 | | -To create a pull request first push your branch to Github: `git push -u origin [branch_name]`. |
103 | | - |
104 | | -Then open your repository on Github and click on the "X branches" link near the top. |
105 | | - |
106 | | - |
107 | | - |
108 | | -From the branches page locate the branch you are interested in the "Your Branches" section and click "New pull request". |
109 | | - |
110 | | - |
111 | | - |
112 | | -Once you're on the "Open a pull request" page you have one final step. Fill in your title and description and then click "Create pull request". In the sidebar you can add your teammates as reviewers if you'd like so that they receive an email telling them to review the PR. |
113 | | - |
114 | | - |
115 | | - |
116 | | -#### Reviewing a Pull Request |
117 | | - |
118 | | -To start we need to go into the "Files changed" section of the pull request. |
119 | | - |
120 | | - |
121 | | - |
122 | | -To add a comment hover over the margin for the line you would like to comment on and click on the blue plus sign. |
123 | | - |
124 | | - |
125 | | - |
126 | | -This will open a box that lets you leave a comment and then begin a code review. Choose "Start a review" and not "Add a single comment". |
127 | | - |
128 | | - |
129 | | - |
130 | | -To finish reviewing a Pull Request click on the green "Review changes" button in the upper right corner. |
131 | | - |
132 | | - |
133 | | - |
134 | | -This will open a box giving you space to write a comment and three options: |
135 | | - |
136 | | -- Comment: use this when you don't have an opinion on whether or not the PR should be merged. |
137 | | -- Approve: use this when you think the PR should be merged. |
138 | | -- Request changes: use this when you think the PR needs more work before merging. |
139 | | - |
140 | | - |
141 | | - |
142 | | -Click "Submit review" to finish. |
| 81 | +### Branch Maintenance Commands |
143 | 82 |
|
144 | | -#### Merging a Pull Request |
| 83 | +These commands allow you to create, switch and delete branches. |
145 | 84 |
|
146 | | -Once a PR has been reviewed click back into the "Conversation" view. |
| 85 | +- `git checkout [branch_name]`: switches Git to an _existing_ branch. |
| 86 | +- `git branch [branch_name]`: creates a new branch _but does not switch to it_. |
| 87 | +- `git checkout -b [branch_name]`: creates a new branch and switches Git to this _new_ branch. |
| 88 | +- `git branch -a`: shows a list of all local and remote branches. |
| 89 | +- `git branch -d [branch_name]`: delete the specified local branch. |
147 | 90 |
|
148 | | - |
| 91 | +### File Commands |
149 | 92 |
|
150 | | -Scroll down to the bottom until you get to the "Merge pull request" box. Click on "Merge pull request" to merge the PR and then click "Confirm merge". |
| 93 | +These commands let you clear out or set aside changes to files. |
151 | 94 |
|
152 | | - |
| 95 | +- `git checkout [path/to/file]`: reverts any unstaged changes to the specified file(s) to their last committed state. |
| 96 | +- `git checkout .`: reverts all unstaged changes to tracked files in the current direstory to their last committed state. |
| 97 | +- `git stash -k`: set aside unstaged changes into a stash. |
| 98 | +- `git stash`: set aside _all_ changes into a stash. |
| 99 | +- `git stash pop`: apply the most recently (unapplied) stash. |
153 | 100 |
|
154 | | -Your PR might say that you aren't ready to merge because there are conflicts with the base branch. In this case you probably need to merge `master` (see [Working with Branches](#working-with-branches)). |
| 101 | +(Changes that haven't been `add`ed are unstaged.) |
155 | 102 |
|
156 | 103 | ## Resources |
157 | 104 | - [Git SCM Documentation](https://git-scm.com/book/ch3-2.html) |
|
0 commit comments