Skip to content
Open
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
60 changes: 60 additions & 0 deletions hands_on/update_remote.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import os
import subprocess

from exercise_utils.cli import run_command
from exercise_utils.file import create_or_update_file, append_to_file
from exercise_utils.git import add, init, commit

__requires_git__ = True
__requires_github__ = True

REPO_NAME = "gitmastery-things"

def download(verbose: bool):
username = run_command(["gh", "api", "user", "-q", ".login"], verbose).strip()
os.makedirs("things")
os.chdir("things")
init(verbose)
create_or_update_file("fruits.txt", """
apples
bananas
cherries
dragon fruits
""",
)
add(["fruits.txt"], verbose)
append_to_file("fruits.txt", """
figs
""",
)
add(["fruits.txt"], verbose)
commit("Insert figs into fruits.txt", verbose)
Comment on lines +25 to +31
Copy link

Copilot AI Nov 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first add on line 25 stages fruits.txt when it hasn't been committed yet. This adds the initial fruits content to the staging area, then appends 'figs' to the file, and stages it again before committing. However, no commit is made after the first add, so both the initial content and the appended 'figs' will be in the same commit. This differs from the pattern in other exercises (like stage_modified.py) and may not demonstrate the intended workflow. Consider adding a commit after line 25 if you want to show two separate commits, or remove the first add if you want one commit with all content.

Copilot uses AI. Check for mistakes.
create_or_update_file("colours.txt", """
a file for colours
""",
)
create_or_update_file("shapes.txt", """
a file for shapes
""",
)
add(["colours.txt", "shapes.txt"], verbose)
commit("Add colours.txt, shapes.txt", verbose)
repo_check = subprocess.run(
["gh", "repo", "view", f"{username}/{REPO_NAME}"],
capture_output=True,
text=True
)

if repo_check.returncode == 0:
run_command(["gh", "repo", "delete", REPO_NAME, "--yes"], verbose)

run_command(["gh", "repo", "create", REPO_NAME, "--public"], verbose)
run_command(["git", "remote", "add", "origin", f"https://github.com/{username}/{REPO_NAME}"], verbose)

default_branch = run_command(["git", "branch", "--list", "main", "master"], verbose).strip()
if "main" in default_branch:
default_branch = "main"
else:
default_branch = "master"
Comment on lines +54 to +58
Copy link

Copilot AI Nov 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default branch detection logic is unnecessary and unreliable. The init function from exercise_utils.git (line 17) already forces the initial branch to be 'main' via --initial-branch=main. Additionally, if both 'main' and 'master' branches exist, git branch --list main master would list both, and the check if 'main' in default_branch would incorrectly match. Since the repository is always initialized with 'main' as the default branch, this code should be simplified to directly use 'main'.

Suggested change
default_branch = run_command(["git", "branch", "--list", "main", "master"], verbose).strip()
if "main" in default_branch:
default_branch = "main"
else:
default_branch = "master"
default_branch = "main"

Copilot uses AI. Check for mistakes.

run_command(["git", "push", "-u", "origin", default_branch], verbose)