-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path2.2-history.qmd
155 lines (111 loc) · 4.25 KB
/
2.2-history.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
---
title: "Intro to `git`"
subtitle: "Block 2.2: History"
---
# Traveling Through Time: `git log` {background-color="black" background-opacity="0.5" background-image="images/backgrounds/back-to-the-future-time.gif"}
::: attribution
Background: https://giphy.com/gifs/back-to-the-future-dgEIhYAo3lZiE
:::
## Seeing History
We can see the history of our committed changes, called `commits` using `git log`.
```bash
git log
# commit ed226e022f60d9f578265c2c246367f5f07756de (HEAD -> main)
# Author: Jan Simson <[email protected]>
# Date: Fri Sep 16 17:00:03 2022 +0200
# Edit hello.txt
# commit 795780d123f6eeedaa09734005c08d1ad89c1976
# Author: Jan Simson <[email protected]>
# Date: Fri Sep 16 16:59:45 2022 +0200
# Adding hello.txt
# lines 1-9
```
You can move up and down with the arrow keys and leave the log view by pressing `q`.
## Practical: Seeing History {background-color="black"}
1. Go back to your Terminal
2. Open the `git log` of your respository
3. Navigate up and down in the history
4. Copy the commit hash of the 2nd commit you made in the repository
5. Also explore the git history in a GUI
# *Properly* Traveling Through Time: <br> `git checkout` {background-color="black" background-opacity="0.5" background-image="images/backgrounds/back-to-the-future-travel.gif"}
::: attribution
Background: https://giphy.com/gifs/back-to-the-future-xsF1FSDbjguis
:::
## `git checkout`
You can go back to any previous commit (and many other things!) with `git checkout`.
```bash
git log
# commit ed226e022f60d9f578265c2c246367f5f07756de (HEAD -> main)
# Author: Jan Simson <[email protected]>
# Date: Fri Sep 16 17:00:03 2022 +0200
# Edit hello.txt
# commit 795780d123f6eeedaa09734005c08d1ad89c1976
# Author: Jan Simson <[email protected]>
# Date: Fri Sep 16 16:59:45 2022 +0200
# Adding hello.txt
# lines 1-9
```
## `git checkout`
You can go back to any previous commit (and many other things!) with `git checkout`.
```bash
git checkout 795780d123f6eeedaa09734005c08d1ad89c1976
```
## `git checkout`
You can go back to any previous commit (and many other things!) with `git checkout`.
```bash
git checkout 795780d
```
:::{.callout-tip}
You don't need to pass the whole commit hash, just the first few characters are usually enough.
:::
## `git checkout`
You can go back to any previous commit (and many other things!) with `git checkout`.
```bash
git checkout 795780d
# Note: switching to '795780d123f6eeedaa09734005c08d1ad89c1976'.
#
# You are in 'detached HEAD' state. You can look around, make experimental
# changes and commit them, and you can discard any commits you make in this
# state without impacting any branches by switching back to a branch.
#
# If you want to create a new branch to retain commits you create, you may
# do so (now or later) by using -c with the switch command. Example:
#
# git switch -c <new-branch-name>
#
# Or undo this operation with:
#
# git switch -
#
# Turn off this advice by setting config variable advice.detachedHead to false
#
# HEAD is now at 795780d Adding hello.txt
```
## Detached `HEAD`, everything OK? 🤕
- When traveling back to a commit, we got a scary warning about a `detached HEAD`
- Normally we are always on a branch in git
- The `HEAD` is always the currently checked out state of your repository
- If the `HEAD` is detached, it is not associated with a branch
- If we make commits with a detached `HEAD` our changes will be lost
- We can go back to `main` with `git checkout main`
## Referencing Commits Relatively
- You will also see `HEAD` in e.g. the `git log` or Sourcetree view
- `HEAD` can be used to reference commits *relatively*
- `HEAD` the current commit
- `HEAD~1` the previous commit
- `HEAD~2` two commits ago
## Tracking changes: Using a GUI
![The labels in Sourcetree mirror the names within the git CLI](images/screenshots/sourcetree-test.png)
# Questions?
## Practical: Time-Travel ⏱️ {background-color="black"}
1. Go back to your Terminal
2. Open the `git log` of your respository
3. *Checkout* the previous commit using `HEAD~1`
4. Go back to your `main` branch
5. Checkout your very first commit through its hash
## *End of Section* 🎉 {background-color="black"}
:::{.r-fit-text}
Any Questions?
:::
[[🏡 Back to Overview]](./index.html)
[[⏩️ Next Section]](./2.3-branches_merging.html)