Skip to content

Commit 922db73

Browse files
committed
add some git
1 parent c739647 commit 922db73

18 files changed

+1172
-0
lines changed

content/_toc.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,10 @@ parts:
7878
chapters:
7979
- file: 12-extensions/extensions-overview
8080
- file: 12-extensions/extensions-example
81+
82+
- caption: Git and Github
83+
chapters:
84+
- file: git/version-control
85+
- file: git/git
86+
- file: git/github
87+
- file: git/pull-requests
236 KB
Loading

content/git/git-branches.md

Lines changed: 374 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,374 @@
1+
# Git Branches
2+
3+
When we develop as a team, we often use *branches* to organize our
4+
changes. Here we walk through an example of branches.
5+
6+
To get more practice, we'll start a new project and initialize it.
7+
8+
```{figure} https://imgs.xkcd.com/comics/git.png
9+
:width: 75%
10+
:align: center
11+
:alt: xkcd comic on git
12+
13+
from XKCD
14+
```
15+
16+
1. Let's repeat the setup we did before...
17+
18+
```bash
19+
mkdir project2
20+
cd project2
21+
echo "a second git project" > README
22+
git init
23+
```
24+
25+
2. Now let's add our `README` to git and commit:
26+
27+
```bash
28+
git add README
29+
git commit
30+
```
31+
32+
(Remember to enter a log and save...)
33+
34+
3. Let's create and add another file.
35+
36+
We write a simple shell script. Open a new file, called `myscript`, e.g., with nano:
37+
38+
```bash
39+
nano myscript
40+
```
41+
42+
and copy-paste the following content into it:
43+
44+
```bash
45+
ls -l > script.out
46+
```
47+
48+
be sure to end with a new line.
49+
50+
Now, this script is not that fancy and it needs to be run as:
51+
52+
```bash
53+
bash ./myscript
54+
```
55+
56+
when you do this, you should see the output `script.out` created.
57+
58+
Now let's tell git that we want it to track this:
59+
60+
```bash
61+
git add myscript
62+
git commit
63+
```
64+
65+
Be sure to add a useful message.
66+
67+
4. Ignoring things.
68+
69+
Let's look at the status of our project:
70+
71+
```bash
72+
git status
73+
```
74+
75+
You'll see something like:
76+
77+
```
78+
On branch main
79+
Untracked files:
80+
(use "git add <file>..." to include in what will be committed)
81+
82+
script.out
83+
84+
nothing added to commit but untracked files present (use "git add" to track)
85+
```
86+
87+
It is telling us that it is not keeping track of `script.out`.
88+
But we don't want it to&mdash;that is the output from running out
89+
script, and generally we don't keep the output of our codes in
90+
version control.
91+
92+
So we'd like to tell git to ignore that file. The way to do this is to
93+
create a `.gitignore` file:
94+
95+
```bash
96+
nano .gitignore
97+
```
98+
99+
and add the following:
100+
101+
```
102+
*.out
103+
```
104+
105+
now if you do `git status`, that file will not appear, but `.gitignore` does!
106+
107+
Be sure to add `.gitignore` to git by doing `git add` followed
108+
by `git commit`.
109+
110+
111+
112+
## A Feature Branch
113+
114+
Now let's imagine that our project is mature and we don't want to break it as
115+
we test out some new ideas. This is where *branches* come into play.
116+
117+
Let's create a new branch called `feature` that we can work on without
118+
disturbing our code in ``main``.
119+
120+
```bash
121+
git checkout -b feature
122+
```
123+
124+
This creates a new branch called `feature` that is initially identical to `main`.
125+
126+
You can tell what branch you are on by doing:
127+
128+
```bash
129+
git branch
130+
```
131+
132+
and we see:
133+
134+
```
135+
* feature
136+
main
137+
```
138+
139+
The `*` indicates which branch we are currently on.
140+
141+
What about the log?
142+
143+
```bash
144+
git log
145+
```
146+
147+
we see:
148+
149+
```
150+
commit 69eb3bf482bd78c3bf63e890f52b9aac33d5ee2a (HEAD -> feature, main)
151+
Author: Michael Zingale <michael.zingale@stonybrook.edu>
152+
Date: Tue Feb 1 10:21:19 2022 -0500
153+
154+
add an ignore file
155+
156+
commit 9b0ae624393bd28f26f37d633d9692be3c2929f0
157+
Author: Michael Zingale <michael.zingale@stonybrook.edu>
158+
Date: Tue Feb 1 10:18:53 2022 -0500
159+
160+
add my first script
161+
162+
commit 9625926dd4bc26e04d37988ffceaa7eba64a76da
163+
Author: Michael Zingale <michael.zingale@stonybrook.edu>
164+
Date: Tue Feb 1 10:18:02 2022 -0500
165+
166+
start of our new project
167+
```
168+
169+
Notice that the most recent commit line shows that both `feature` and `main`
170+
are at the same hash, and it also calls that commit `HEAD`.
171+
`HEAD` is the most recent change on the branch.
172+
173+
174+
Now let's make a change.
175+
176+
Let's put a "Hello, World" code in our repo! Create a file called
177+
`hello.cpp` and add the following:
178+
179+
```c++
180+
#include <iostream>
181+
182+
int main() {
183+
184+
std::cout << "Hello, World!" << std::endl;
185+
186+
}
187+
```
188+
189+
Let's add it to git control:
190+
191+
```bash
192+
git add hello.cpp
193+
git commit
194+
```
195+
196+
Now look at the log:
197+
198+
```
199+
Author: Michael Zingale <michael.zingale@stonybrook.edu>
200+
Date: Tue Feb 1 10:23:51 2022 -0500
201+
202+
add hello world
203+
204+
commit 69eb3bf482bd78c3bf63e890f52b9aac33d5ee2a (main)
205+
Author: Michael Zingale <michael.zingale@stonybrook.edu>
206+
Date: Tue Feb 1 10:21:19 2022 -0500
207+
208+
add an ignore file
209+
210+
commit 9b0ae624393bd28f26f37d633d9692be3c2929f0
211+
Author: Michael Zingale <michael.zingale@stonybrook.edu>
212+
Date: Tue Feb 1 10:18:53 2022 -0500
213+
214+
add my first script
215+
216+
commit 9625926dd4bc26e04d37988ffceaa7eba64a76da
217+
Author: Michael Zingale <michael.zingale@stonybrook.edu>
218+
Date: Tue Feb 1 10:18:02 2022 -0500
219+
220+
start of our new project
221+
```
222+
223+
Now it is clear that `main` is still on the last commit but
224+
`feature` is on the latest (`HEAD`) commit.
225+
226+
227+
Recall that we can compile our `hello.cpp` via:
228+
229+
```bash
230+
g++ -o hello hello.cpp
231+
```
232+
233+
```{tip}
234+
We don't want the executable `hello` to be under git control, so
235+
add it to your `.gitignore` and commit that change.
236+
```
237+
238+
239+
## Switching Branches
240+
241+
Let's go back to `main`. The `checkout` command does this for us:
242+
243+
```bash
244+
git checkout main
245+
```
246+
247+
Now notice that if you do `ls`, you don't see `hello.cpp`! That
248+
file is in your `feature` branch, and under git control, and git
249+
knows it is not on `main` so when you switch to main, it does not
250+
appear.
251+
252+
Let's add an `authors.txt` file to our project, just containing your name.
253+
254+
```{admonition} Try it...
255+
create an `authors.txt` and add it to git control.
256+
```
257+
258+
Note that this is on `main`. If you switch to `feature` you won't see it:
259+
260+
```bash
261+
git checkout feature
262+
```
263+
264+
````{tip}
265+
Just like we can use ``cd -`` to switch to the previous directory we were on,
266+
we can use
267+
268+
```bash
269+
git checkout -
270+
```
271+
to switch back to the previous branch we were on -- in this case, `main`
272+
````
273+
274+
Switch back to ``main``.
275+
276+
277+
## Diff
278+
279+
Let's look at the differences between our branches. Since we're on
280+
`main`, we can ask git what the difference between our current code
281+
and the code in `feature` is via:
282+
283+
```bash
284+
git diff feature
285+
```
286+
287+
As you use git more and more, you'll see that `diff` is very handy.
288+
289+
290+
## Merging
291+
292+
Now we're happy with the changes we made on `feature` and we want to
293+
incorporate them into `main`&mdash;this is called *merging*, we
294+
accomplish this by doing
295+
296+
```bash
297+
git merge feature
298+
```
299+
300+
This is a special type of commit, and your editor will pop up with a
301+
merge commit already entered. Just save this, and it will be logged.
302+
303+
304+
## Going back in time...
305+
306+
If we look at our project history so far:
307+
308+
```bash
309+
git log
310+
```
311+
312+
We see something like this (again, your hashes will be different)
313+
314+
```
315+
commit 42596acdd432e1dbdc4f8abd668dffa30c707473 (HEAD -> main)
316+
Merge: c8904ec bb38a3d
317+
Author: Michael Zingale <michael.zingale@stonybrook.edu>
318+
Date: Tue Feb 1 10:54:51 2022 -0500
319+
320+
Merge branch 'feature' into main
321+
322+
commit c8904ec0bd8ac1bc3449ec79ade971ee9902c14e
323+
Author: Michael Zingale <michael.zingale@stonybrook.edu>
324+
Date: Tue Feb 1 10:31:03 2022 -0500
325+
326+
add authors
327+
328+
commit bb38a3d1f3f4f2971ced93a1f203c52c276f37a5 (feature)
329+
Author: Michael Zingale <michael.zingale@stonybrook.edu>
330+
Date: Tue Feb 1 10:27:09 2022 -0500
331+
332+
don't track executable
333+
334+
commit 22e1d58cee38021da961516b24dde689d3b8a66e
335+
Author: Michael Zingale <michael.zingale@stonybrook.edu>
336+
Date: Tue Feb 1 10:23:51 2022 -0500
337+
338+
add hello world
339+
340+
commit 69eb3bf482bd78c3bf63e890f52b9aac33d5ee2a
341+
Author: Michael Zingale <michael.zingale@stonybrook.edu>
342+
Date: Tue Feb 1 10:21:19 2022 -0500
343+
344+
add an ignore file
345+
346+
commit 9b0ae624393bd28f26f37d633d9692be3c2929f0
347+
Author: Michael Zingale <michael.zingale@stonybrook.edu>
348+
Date: Tue Feb 1 10:18:53 2022 -0500
349+
350+
add my first script
351+
352+
commit 9625926dd4bc26e04d37988ffceaa7eba64a76da
353+
Author: Michael Zingale <michael.zingale@stonybrook.edu>
354+
Date: Tue Feb 1 10:18:02 2022 -0500
355+
356+
start of our new project
357+
```
358+
359+
Imagine that our current code is not working, but we remember that it
360+
was before we did our branching and added the `hello.cpp`. Looking
361+
at the log or the graph shows that that change came in with the commit
362+
`22e1d58cee38021da961516b24dde689d3b8a66e`. We can checkout the
363+
state of the code before that commit by using the hash from the
364+
previous commit:
365+
366+
```bash
367+
git checkout 69eb3bf482bd78c3bf63e890f52b9aac33d5ee2a
368+
```
369+
370+
Note that you don't need to type out the entire hash&mdash;you only need the starting bits,
371+
as long as it is unique.
372+
373+
This command puts you in a detached branch, but you could make it a named branch by using
374+
`git checkout -b name`.

0 commit comments

Comments
 (0)