GitHub collaboration
English translation
Learning objectives
Use a safe local example to learn how to combine changes without losing them when several people edit the same file.
Execution conditions
The baseline exercise uses a temporary local Git repository. You can explore changes, branches, and merges without a GitHub account or a remote repository connection.
Track changes to one file
Starting conditions
repo is temporary storage on the computer running the notebook kernel. In README.md inside it, change Condition: baseline to Condition: test.
Procedure
- After checking the baseline commit, read
repo/'README.md'in a new code cell in the same notebook, change only the specified sentence, and save. Resolve file paths relative torepo. - Call the provided
git(*args)function in a code cell and record the diff and status. For example, you can display changes withprint(git('diff')). - Commit the changes, then check the difference from the previous commit again.
Results to retain
- A 3-row table for before editing, after editing, and after committing, plus a diff code block.
from pathlib import Path
import tempfile, subprocess
lab_tmp=tempfile.TemporaryDirectory(prefix='course-git-')
repo=Path(lab_tmp.name)
def git(*args):
return subprocess.check_output(['git','-C',str(repo),*args],text=True).strip()
git('init','-b','main');git('config','user.name','Learner');git('config','user.email','learner@example.invalid')
(repo/'README.md').write_text('Robot experiment\nCondition: baseline\n')
git('add','README.md');git('commit','-m','Record baseline')
print(repo,git('log','--oneline'))
Record results and self-check
Step:
File contents:
Working tree status:
Commit message:
Points to check
- Are file changes distinguished from committed history?
- Does the record contain no passwords or tokens?
Separate experimental conditions with branches
Starting conditions
Create an experiment branch in the same repository and add Rate: 10 Hz to README.md.
Procedure
- After committing on experiment, return to main and compare the file contents.
- Inspect both branches' histories with log –oneline –all –graph.
- After merging into main, describe what remains in the file and history.
Results to retain
- A table showing before and after branching, plus history text or 1 screenshot.
# Use repo and the git function from the previous cell.
print(git('branch','--show-current'))
# TODO: Create the experiment Branch and record changes, commits, and merges.
Record results and self-check
Branch and step:
README contents:
HEAD Commit:
Location of the change:
Points to check
- Is the working tree clean before switching branches?
- Did you actually read the main file before and after merging?
Reproduce and resolve a conflict
Starting conditions
Change the same Condition line to different values on main and experiment, and commit each change.
Procedure
- Attempt the merge and keep the file containing conflict markers.
- Resolve the conflict by writing the final selected condition on one line, then commit.
- Explain the difference between editing different lines and editing the same line.
Results to retain
- Code blocks before and after the conflict, and 3 sentences explaining the resolution.
print(git('status','--short'))
# TODO: Create a conflict on the same line and record the final changes.
Record results and self-check
Scenario:
Merge result:
Selected final sentence:
Reason for selection:
Points to check
- Are the conflict markers <<<<<<<, =======, and >>>>>>> absent from the final file?
- Is git status clean after resolution?