Back to List

Version Control for Experiment Code with Git

Learn to manage analysis code and experiment records with Git. A version control introduction tailored for biology researchers.

Beginner
|
75min
|
Verified (2026-06)
Version ControlReproducibilityProtocolLab NotebookGitHub
Progress0/7 (0%)

Version Control for Experiment Code with Git

After Completing This Topic

You'll be able to record the change history of your analysis code with Git, revert to past versions, and back up to GitHub.


Why You Need Git

Have you ever seen filenames like these in your lab?

text
analysis_final.py
analysis_final_v2.py
analysis_final_v2_advisor_edits.py
analysis_final_v2_advisor_edits_REAL_FINAL.py

Git stores the entire change history in a single file. No need to copy files.

Installation & Initial Setup

Git is already installed on Google Colab. You just need to set your name and email.

bash
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
# Verify settings
git config --list --global | grep user

Creating Your First Repository

git init is like opening a fresh lab notebook.

bash
# Create a practice directory + initialize Git
mkdir -p /tmp/bio-analysis && cd /tmp/bio-analysis
git init
# A .git folder is created (where Git stores history)
ls -la .git/

Your First Commit: Taking a Snapshot

A commit is like writing the date in your lab notebook and noting "completed up to here."

bash
cd /tmp/bio-analysis
# Create an analysis script
cat > gc_analysis.py << 'SCRIPT'
"""GC Content Analysis Script v1"""
def calculate_gc(sequence: str) -> float:
seq = sequence.upper()
gc = seq.count("G") + seq.count("C")
return (gc / len(seq)) * 100
sequences = {
"BRCA1": "ATGGATTTATCTGCTCTTCG",
"TP53": "ATGGAGGAGCCGCAGTCAG",
}
for name, seq in sequences.items():
print(f"{name}: GC={calculate_gc(seq):.1f}%")
SCRIPT
# Git's 3 steps: Work โ†’ Stage โ†’ Commit
git add gc_analysis.py # 1. Stage (select files for the snapshot)
git status # Check: green = staged
git commit -m "feat: initial GC content analysis script" # 2. Commit (snapshot!)
# View history
git log --oneline

Tracking Changes: Viewing Diffs

Just as you mark changes with a red pen when proofreading a paper, git diff shows you what changed.

bash
cd /tmp/bio-analysis
# Create the environment if it doesn't exist
if [ ! -f gc_analysis.py ]; then
git init
cat > gc_analysis.py << 'SCRIPT'
"""GC Content Analysis Script v1"""
def calculate_gc(sequence: str) -> float:
seq = sequence.upper()
gc = seq.count("G") + seq.count("C")
return (gc / len(seq)) * 100
sequences = {
"BRCA1": "ATGGATTTATCTGCTCTTCG",
"TP53": "ATGGAGGAGCCGCAGTCAG",
}
for name, seq in sequences.items():
print(f"{name}: GC={calculate_gc(seq):.1f}%")
SCRIPT
git add gc_analysis.py
git commit -m "feat: initial GC content analysis script"
fi
# Modify the script: add EGFR gene
cat > gc_analysis.py << 'SCRIPT'
"""GC Content Analysis Script v2 โ€” Added EGFR"""
def calculate_gc(sequence: str) -> float:
seq = sequence.upper()
gc = seq.count("G") + seq.count("C")
return (gc / len(seq)) * 100
sequences = {
"BRCA1": "ATGGATTTATCTGCTCTTCG",
"TP53": "ATGGAGGAGCCGCAGTCAG",
"EGFR": "ATGCGACCCTCCGGGACGGC",
}
for name, seq in sequences.items():
gc = calculate_gc(seq)
label = "HIGH" if gc > 60 else "NORMAL"
print(f"{name}: GC={gc:.1f}% [{label}]")
SCRIPT
# View changes
git diff gc_analysis.py
# Commit
git add gc_analysis.py
git commit -m "feat: add EGFR + HIGH label for GC > 60%"
git log --oneline

Reverting: Traveling to the Past

Just as you return to a previous protocol when an experiment fails, Git lets you go back to any point in time.

bash
cd /tmp/bio-analysis
# Create the environment if it doesn't exist
if [ ! -d .git ]; then
git init
git config user.name "Test" && git config user.email "test@test.com"
echo "v1" > gc_analysis.py
git add gc_analysis.py && git commit -m "v1"
echo "v2" > gc_analysis.py
git add gc_analysis.py && git commit -m "v2"
fi
# View history
git log --oneline
# Revert a specific file to a previous commit state
FIRST_COMMIT=$(git log --oneline | tail -1 | cut -d' ' -f1)
echo "First commit: $FIRST_COMMIT"
# Check current content
echo "--- Current ---"
cat gc_analysis.py
# Preview the first commit's content (before reverting)
echo "--- First commit ---"
git show $FIRST_COMMIT:gc_analysis.py

Caution: git checkout -- filename reverts modifications (uncommitted changes will be lost). Commit important changes first.

.gitignore: Excluding Files from Tracking

Just as you don't keep intermediates from experiments, you exclude large data files and temp files from Git.

bash
cd /tmp/bio-analysis
# Create .gitignore โ€” for bio projects
cat > .gitignore << 'EOF'
# Large data (don't put in Git)
*.fastq
*.fastq.gz
*.bam
*.sam
# Python cache
__pycache__/
*.pyc
.ipynb_checkpoints/
# Environment settings (may contain personal info)
.env
*.log
# OS files
.DS_Store
Thumbs.db
EOF
# Test: create a large file and verify Git ignores it
echo "fake fastq data" > sample.fastq
git status # Success if sample.fastq doesn't appear in Untracked

Try It Yourself (Faded Example)

Fill in the blanks to complete Git's basic workflow.

Fill in the Blanksbash
# 1. Create a new repository
git
# 2. Add a file to the staging area
git analysis.py
# 3. Save a snapshot
git -m "first analysis script"

Common Errors & Solutions

Q: fatal: not a git repository

The current directory is not a Git repository. Initialize with git init, or cd into a Git repo folder.

Q: nothing to commit, working tree clean

There are no changes. Modify a file and git add it. If you already did git add, it's already committed.

Q: I accidentally committed a large FASTQ file

Add *.fastq to .gitignore, then run git rm --cached sample.fastq to untrack it (the file itself won't be deleted).

Q: I wrote the wrong commit message

You can fix the last commit message with git commit --amend -m "new message" (only if you haven't pushed yet).


In the next article, we'll learn how to visualize experiment data with Python.

๐Ÿ’ฌ Questions & Comments

0 comments

You can post without signing in. Guest comments cannot be edited or deleted by their author.

0/2000

Loading...