Git workflow
Review a focused change and keep generated or private data out of version control.
On this page
The concept
Git distinguishes the working tree, the staging area, and committed history. git status shows file state, git diff shows unstaged changes, and git add selects content for a commit. A commit should represent one understandable purpose. Test and review it before recording it.
The executable example only reads the course's .gitignore and checks a few patterns. It does not initialize a repository or create a commit. .gitignore helps keep environments, caches, and private data out of new tracking; it does not remove a secret already committed in history.
Worked example
Extract the example files. From the python_from_zero directory, run the following command in a terminal. On Windows, use py in place of python3.
python3 08_professional_workflow/60_git_workflow/lesson.pyfrom pathlib import Path
root = Path(__file__).resolve().parents[2]
patterns = (root / ".gitignore").read_text(encoding="utf-8").splitlines()
for pattern in [".venv/", "__pycache__/", ".env", "tasks.json"]:
print(f"{pattern}: {pattern in patterns}")Trace the result
- The source-file path locates the course root independently of cwd.
- The ignore file is read as UTF-8 lines.
- Each relevant pattern is checked for presence, producing a small status report without changing Git state.
.venv/: True
__pycache__/: True
.env: True
tasks.json: TrueLine-by-line walkthrough
Line 1: from pathlib import Path
Import Path from pathlib. This supplies filesystem path values. The caller can now use the imported names directly.
Line 3: root = Path(__file__).resolve().parents[2]
Evaluate Path(__file__).resolve().parents[2], then bind or update root with the result. Assignment itself does not imply a deep copy.
Line 4: patterns = (root / ".gitignore").read_text(encoding="utf-8").splitlines()
Evaluate (root / '.gitignore').read_text(encoding='utf-8').splitlines(), then bind or update patterns with the result. The call finishes before its return value is bound; a failed call does not produce a successful assignment. Assignment itself does not imply a deep copy.
Lines 5–6: for pattern in [".venv/", "__pycache__/", ".env", "tasks.json"]:
Obtain each item from ['.venv/', '__pycache__/', '.env', 'tasks.json'] and bind it to pattern before executing the body. A tuple-shaped target unpacks that item. A loop else, if present, runs only when completion does not use break.
Line 6: print(f"{pattern}: {pattern in patterns}")
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Common mistake
Ignoring a file is not retroactive secret removal. Avoid committing credentials or private task data in the first place.