Git Basics
This page introduces the Git commands used to record analysis scripts. It covers repository creation, file status, commits, history, differences, and recovery of earlier file versions.
Creating a repository with git init
Section titled “Creating a repository with git init”A Git repository is a directory that Git is tracking. You create one by running git init inside your project folder.
mkdir rnaseq-analysiscd rnaseq-analysisgit initInitialized empty Git repository in /home/user/rnaseq-analysis/.git/This creates a hidden .git/ directory for repository data. The status examples below assume the initial branch is main; your Git configuration can select a different name. Git also permits git init in an existing repository without overwriting its existing contents.
Checking status with git status
Section titled “Checking status with git status”git status tells you what has changed since the last commit. Run it constantly.
git statusOn branch main
No commits yet
nothing to commit (create/copy files and use "git add" to track)After you create a file:
echo "library(DESeq2)" > deseq2_analysis.Rgit statusOn branch main
No commits yet
Untracked files: (use "git add <file>..." to include in what will be committed) deseq2_analysis.R
nothing added to commit but untracked files present (use "git add" to track)Git sees the new file but is not tracking it yet.
The three states of Git
Section titled “The three states of Git”Git manages your files through three states. Understanding these states is the key to understanding Git.
| State | Location | Description |
|---|---|---|
| Modified | Working directory | You changed a file but have not staged it |
| Staged | Staging area | You marked a changed file to go into the next commit |
| Committed | Repository | The change is safely stored in Git history |
The workflow is: edit files, stage the changes you want to keep, then commit them as a snapshot.
Staging changes with git add
Section titled “Staging changes with git add”git add moves changes from your working directory to the staging area. Think of the staging area as a preparation zone for your next commit.
# Stage one filegit add deseq2_analysis.R
# Stage multiple filesgit add deseq2_analysis.R normalize.R
# Stage everything in the current directorygit add .After staging:
git statusOn branch main
No commits yet
Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: deseq2_analysis.RThe file is now staged and ready to commit.
Saving snapshots with git commit
Section titled “Saving snapshots with git commit”git commit records the staged content in a new commit. A branch or tag keeps the commit reachable through repository history.
git commit -m "Add DESeq2 analysis script"[main (root-commit) a1b2c3d] Add DESeq2 analysis script 1 file changed, 1 insertion(+) create mode 100644 deseq2_analysis.RA commit has an object ID; a1b2c3d illustrates an abbreviated ID. The ID identifies the commit, but it does not preserve a copy. After references and reflog entries no longer retain a commit, Git can remove it during garbage collection.
The -m flag lets you write a message describing the change. Always write a message. You will thank yourself later.
Viewing history with git log
Section titled “Viewing history with git log”git log shows the commit history of your project.
git logcommit a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0Author: Jane Doe <jane@lab.edu>Date: Mon Mar 15 10:30:00 2026 +0000
Add DESeq2 analysis scriptFor a compact view:
git log --onelinea1b2c3d Add DESeq2 analysis scriptThis is easier to scan when you have many commits.
Seeing what changed with git diff
Section titled “Seeing what changed with git diff”git diff shows the exact lines that changed since your last commit.
# See unstaged changesgit diff
# See staged changes (ready to commit)git diff --stagedExample output after editing a file:
diff --git a/deseq2_analysis.R b/deseq2_analysis.Rindex 1234567..abcdefg 100644--- a/deseq2_analysis.R+++ b/deseq2_analysis.R@@ -1 +1,3 @@ library(DESeq2)+counts <- read.csv("counts.csv", row.names = 1)+coldata <- read.csv("coldata.csv", row.names = 1)Lines starting with + were added. Lines starting with - were removed. This is the same format you see on GitHub.
Ignoring files with .gitignore
Section titled “Ignoring files with .gitignore”Not everything belongs in version control. Large data files, sensitive information, and generated output should be excluded. Create a .gitignore file to tell Git what to skip.
# .gitignore for a bioinformatics project
# Large sequencing data*.fastq*.fastq.gz*.bam*.bai*.cram
# Single-cell data*.h5ad*.h5*.loom
# Output directoriesoutput/results/figures/
# R artifacts.Rhistory.RData.Rproj.user/
# Python artifacts__pycache__/*.pyc.venv/
# OS files.DS_StoreThumbs.db
# Sensitive data*.csv!metadata.csvAdd and commit the .gitignore file itself:
git add .gitignoregit commit -m "Add gitignore for bioinformatics project"Writing good commit messages
Section titled “Writing good commit messages”A good commit message tells you what changed and why. Start with a verb. Be specific.
Good messages:
Add DESeq2 differential expression scriptFix off-by-one error in read countingUpdate STAR alignment parameters for paired-end dataRemove deprecated normalization stepBad messages:
updatesfixstufffinal versionA useful rule: if you read the message six months from now, will you understand what the commit did?
How often to commit
Section titled “How often to commit”Commit whenever you reach a working state. Here are good moments to commit:
- You finished writing a new analysis step
- You fixed a bug in your script
- You updated parameters after testing
- You added a new input file or configuration
Do not wait until the end of the day to commit everything at once. Small, frequent commits are easier to understand and easier to undo if something goes wrong.
A typical workflow
Section titled “A typical workflow”Here is a complete workflow for a typical analysis session:
# 1. Check your current stategit status
# 2. Work on your analysis (edit files, run scripts)
# 3. Review what you changedgit diff
# 4. Stage the files you want to commitgit add deseq2_analysis.R normalize.R
# 5. Double-check what is stagedgit status
# 6. Commit with a descriptive messagegit commit -m "Add normalization step before DE analysis"
# 7. Verify the commitgit log --onelineRepeat this cycle throughout your workday. It becomes second nature quickly.
Undoing mistakes
Section titled “Undoing mistakes”Everyone makes mistakes. Git provides several ways to recover.
Unstage a file (remove from staging area, keep your edits):
git restore --staged deseq2_analysis.RDiscard changes to a file (revert to last commit):
git restore deseq2_analysis.RSee a previous version of a file:
git show HEAD~1:deseq2_analysis.RThis shows the file as it was one commit ago. HEAD~2 goes two commits back.
Summary
Section titled “Summary”git initcreates a new repository. Run it once per project.git statusshows what changed. Run it often.git addstages changes for the next commit.git commit -m "message"saves a snapshot with a description.git logshows commit history. Use--onelinefor a compact view.git diffshows exactly what lines changed..gitignoreexcludes large data files, generated output, and sensitive information.- Commit often in small, logical chunks. Write messages that your future self will understand.
Further reading
Section titled “Further reading”The Git restore manual distinguishes changes to the index from changes to working files. Check its source and destination options before using the recovery commands above.
The ignore-pattern manual explains pattern precedence and negation. The garbage collection manual explains object retention and pruning, which matter when a commit no longer has a branch or tag.