Skip to content

Commit 2a9bfcb

Browse files
authored
clarified the HEAD~2 root commit issue and suggested using --root or creating an initial commit (bregman-arie#10569)
1 parent 5fde9ba commit 2a9bfcb

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed
Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
## Git - Squashing Commits - Solution
22

3-
4-
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:
54

65
```
6+
echo "Mario" > new_file
77
git add new_file
8-
echo "Mario" -> new_file
9-
git commit -a -m "New file"
8+
git commit -m "New file"
109
```
1110

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:
1312

1413
```
1514
echo "Mario & Luigi" > new_file
1615
git commit -a -m "Added Luigi"
1716
```
1817

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+
```
2023

21-
4. Squash the two commits you've created into one commit
24+
4. Squash the two commits you've created into one commit:
2225

2326
```
2427
git rebase -i HEAD~2
@@ -31,19 +34,25 @@ pick 5412076 New file
3134
pick 4016808 Added Luigi
3235
```
3336

34-
Change `pick` to `squash`
35-
37+
Change `pick` to `squash`:
3638

3739
```
3840
pick 5412076 New file
3941
squash 4016808 Added Luigi
4042
```
4143

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.
4349
4450
### After you complete the exercise
4551

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.
4756

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?**
58+
Yes.

0 commit comments

Comments
 (0)