Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions merge_undo/.gitmastery-exercise.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"exercise_name": "merge-undo",
"tags": [
"git-branch",
"git-merge",
"git-reset"
],
"requires_git": true,
"requires_github": false,
"base_files": {},
"exercise_repo": {
"repo_type": "local",
"repo_name": "play-characters",
"repo_title": null,
"create_fork": null,
"init": true
}
}
41 changes: 41 additions & 0 deletions merge_undo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# merge-undo

Scenario: You are keeping notes on the characters of a play that you are writing. In the main story line (which is in the `main` branch), you introduced two characters, Rick and Morty. You had two other characters in two separate branches `daughter` and `son-in-law`. Just now, you introduced these two characters to the main story line by merging the two branches to the `main` branch.

```mermaid
gitGraph
commit id: "Add Rick"
commit id: "Add morty"
branch daughter
branch son-in-law
checkout daughter
commit id: "Add Beth"
checkout son-in-law
commit id: "Add Jerry"
checkout main
commit id: "Mention Morty is grandson"
merge daughter id: "Introduce Beth"
merge son-in-law id: "Introduce Jerry"
```

However, now you realise this is premature, and wish to undo that change.

## Task

Undo the merging of branches `son-in-law` and `daughter`.

The result should be as follows:

```mermaid
gitGraph
commit id: "Add Rick"
commit id: "Add morty"
branch daughter
branch son-in-law
checkout daughter
commit id: "Add Beth"
checkout son-in-law
commit id: "Add Jerry"
checkout main
commit id: "Mention Morty is grandson"
```
Empty file added merge_undo/__init__.py
Empty file.
57 changes: 57 additions & 0 deletions merge_undo/download.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from exercise_utils.file import append_to_file, create_or_update_file
from exercise_utils.git import add, checkout, commit, merge_with_message


def setup(verbose: bool = False):
create_or_update_file(
"rick.txt",
"""
Scientist
""",
)
add(["rick.txt"], verbose)
commit("Add Rick", verbose)

create_or_update_file(
"morty.txt",
"""
Boy
""",
)
add(["morty.txt"], verbose)
commit("Add morty", verbose)

checkout("daughter", True, verbose)
create_or_update_file(
"beth.txt",
"""
Vet
""",
)
add(["beth.txt"], verbose)
commit("Add Beth", verbose)

checkout("main", False, verbose)

checkout("son-in-law", True, verbose)
create_or_update_file(
"jerry.txt",
"""
Salesman
""",
)
add(["jerry.txt"], verbose)
commit("Add Jerry", verbose)

checkout("main", False, verbose)
append_to_file(
"morty.txt",
"""
Grandson
""",
)
add(["morty.txt"], verbose)
commit("Mention Morty is grandson", verbose)

merge_with_message("daughter", True, "Introduce Beth", verbose)
merge_with_message("son-in-law", True, "Introduce Jerry", verbose)
Empty file added merge_undo/tests/__init__.py
Empty file.
60 changes: 60 additions & 0 deletions merge_undo/tests/specs/base.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
initialization:
steps:
- type: commit
empty: true
message: Empty commit
id: start

# Add rick.txt and commit
- type: new-file
filename: rick.txt
contents: Scientist
- type: commit
message: Add Rick

# Add morty.txt and commit
- type: new-file
filename: morty.txt
contents: Boy
- type: commit
message: Add morty
id: morty-initial-commit

# Create 'daughter' branch, add beth.txt and commit
- type: branch
branch-name: daughter
from-commit: morty-initial-commit
- type: checkout
branch-name: daughter
- type: new-file
filename: beth.txt
contents: Vet
- type: commit
message: Add Beth

# Create 'son-in-law' branch, add jerry.txt and commit
- type: checkout
branch-name: main
- type: branch
branch-name: son-in-law
from-commit: morty-initial-commit
- type: checkout
branch-name: son-in-law
- type: new-file
filename: jerry.txt
contents: Salesman
- type: commit
message: Add Jerry

# Checkout back to main, edit morty.txt and commit.
- type: checkout
branch-name: main
- type: edit-file
filename: morty.txt
contents: |
Boy
Grandson
- type: commit
message: Mention Morty is grandson
id: final-main-commit

51 changes: 51 additions & 0 deletions merge_undo/tests/specs/detached_head.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
initialization:
steps:
- type: commit
empty: true
message: Empty commit
id: start
- type: new-file
filename: rick.txt
contents: Scientist
- type: commit
message: Add Rick
- type: new-file
filename: morty.txt
contents: Boy
- type: commit
message: Add morty
id: morty-initial-commit
- type: branch
branch-name: daughter
from-commit: morty-initial-commit
- type: checkout
branch-name: daughter
- type: new-file
filename: beth.txt
contents: Vet
- type: commit
message: Add Beth
- type: checkout
branch-name: main
- type: branch
branch-name: son-in-law
from-commit: morty-initial-commit
- type: checkout
branch-name: son-in-law
- type: new-file
filename: jerry.txt
contents: Salesman
- type: commit
message: Add Jerry
- type: checkout
branch-name: main
- type: edit-file
filename: morty.txt
contents: |
Boy
Grandson
- type: commit
message: Mention Morty is grandson
- type: checkout
commit-hash: main

42 changes: 42 additions & 0 deletions merge_undo/tests/specs/main_wrong_commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
initialization:
steps:
- type: commit
empty: true
message: Empty commit
id: start
- type: new-file
filename: rick.txt
contents: Scientist
- type: commit
message: Add Rick
- type: new-file
filename: morty.txt
contents: Boy
- type: commit
message: Add morty
id: morty-initial-commit
- type: branch
branch-name: daughter
from-commit: morty-initial-commit
- type: checkout
branch-name: daughter
- type: new-file
filename: beth.txt
contents: Vet
- type: commit
message: Add Beth
- type: checkout
branch-name: main
- type: branch
branch-name: son-in-law
from-commit: morty-initial-commit
- type: checkout
branch-name: son-in-law
- type: new-file
filename: jerry.txt
contents: Salesman
- type: commit
message: Add Jerry
- type: checkout
branch-name: main

56 changes: 56 additions & 0 deletions merge_undo/tests/specs/merges_not_undone.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
initialization:
steps:
- type: commit
empty: true
message: Empty commit
id: start
- type: new-file
filename: rick.txt
contents: Scientist
- type: commit
message: Add Rick
- type: new-file
filename: morty.txt
contents: Boy
- type: commit
message: Add morty
id: morty-initial-commit
- type: branch
branch-name: daughter
from-commit: morty-initial-commit
- type: checkout
branch-name: daughter
- type: new-file
filename: beth.txt
contents: Vet
- type: commit
message: Add Beth
- type: checkout
branch-name: main
- type: branch
branch-name: son-in-law
from-commit: morty-initial-commit
- type: checkout
branch-name: son-in-law
- type: new-file
filename: jerry.txt
contents: Salesman
- type: commit
message: Add Jerry
- type: checkout
branch-name: main
- type: edit-file
filename: morty.txt
contents: |
Boy
Grandson
- type: commit
message: Mention Morty is grandson
- type: merge
branch-name: daughter
message: Introduce Beth
no-ff: true
- type: merge
branch-name: son-in-law
message: introduce Jerry
no-ff: true
50 changes: 50 additions & 0 deletions merge_undo/tests/specs/not_main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
initialization:
steps:
- type: commit
empty: true
message: Empty commit
id: start
- type: new-file
filename: rick.txt
contents: Scientist
- type: commit
message: Add Rick
- type: new-file
filename: morty.txt
contents: Boy
- type: commit
message: Add morty
id: morty-initial-commit
- type: branch
branch-name: daughter
from-commit: morty-initial-commit
- type: checkout
branch-name: daughter
- type: new-file
filename: beth.txt
contents: Vet
- type: commit
message: Add Beth
- type: checkout
branch-name: main
- type: branch
branch-name: son-in-law
from-commit: morty-initial-commit
- type: checkout
branch-name: son-in-law
- type: new-file
filename: jerry.txt
contents: Salesman
- type: commit
message: Add Jerry
- type: checkout
branch-name: main
- type: edit-file
filename: morty.txt
contents: |
Boy
Grandson
- type: commit
message: Mention Morty is grandson
- type: checkout
branch-name: daughter
Loading