You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
1. In a git repository, create a new file with the content "Mario" and commit the change
3
+
1. In a git repository, create a new file with the content "Mario" and commit the change:
5
4
6
5
```
6
+
echo "Mario" > new_file
7
7
git add new_file
8
-
echo "Mario" -> new_file
9
-
git commit -a -m "New file"
8
+
git commit -m "New file"
10
9
```
11
10
12
-
2. Make change to the content of the file you just created so the content is "Mario & Luigi" and create another commit
11
+
2. Make a change to the content of the file you just created so it becomes "Mario & Luigi," then create another commit:
13
12
14
13
```
15
14
echo "Mario & Luigi" > new_file
16
15
git commit -a -m "Added Luigi"
17
16
```
18
17
19
-
3. Verify you have two separate commits - `git log`
18
+
3. Verify you have two separate commits by running:
19
+
20
+
```
21
+
git log
22
+
```
20
23
21
-
4. Squash the two commits you've created into one commit
24
+
4. Squash the two commits you've created into one commit:
22
25
23
26
```
24
27
git rebase -i HEAD~2
@@ -31,19 +34,25 @@ pick 5412076 New file
31
34
pick 4016808 Added Luigi
32
35
```
33
36
34
-
Change `pick` to `squash`
35
-
37
+
Change `pick` to `squash`:
36
38
37
39
```
38
40
pick 5412076 New file
39
41
squash 4016808 Added Luigi
40
42
```
41
43
42
-
Save it and provide a commit message for the squashed commit
44
+
Save it and provide a commit message for the squashed commit.
45
+
46
+
> **Note**: If running `git rebase -i HEAD~2` returns a fatal error (e.g., "invalid upstream 'HEAD~2'"), that usually means your second commit is actually the root commit and there's no valid parent before it. In that case, you can either:
47
+
> * Use `git rebase -i --root` to allow rewriting the root commit, **or**
48
+
> * Create an initial commit before these two commits so that `HEAD~2` points to valid commits.
43
49
44
50
### After you complete the exercise
45
51
46
-
Answer the following:
52
+
**Answer the following:**
53
+
54
+
***What is the reason for squashing commits?**
55
+
History becomes cleaner and it's easier to track changes without many small commits like "removed a character," for example.
47
56
48
-
*What is the reason for squashing commits? - history becomes cleaner and it's easier to track changes without commit like "removed a character" for example.
49
-
* Is it possible to squash more than 2 commits? - yes
57
+
***Is it possible to squash more than 2 commits?**
0 commit comments