Git Workflow
You now know the individual commands. A workflow is how you combine them day to day. Almost every developer's day follows the same loop:
pull latest -> branch -> edit -> stage -> commit -> (repeat) -> merge / pushThe everyday loop
| Step | Command | Why |
|---|---|---|
| 1. Get up to date | git switch main then git pull |
start from the newest code |
| 2. Start a branch | git switch -c fix/typo |
keep the work isolated |
| 3. Edit files | your editor | |
| 4. Review | git status, git diff |
see exactly what you changed |
| 5. Stage | git add <files> |
choose what goes in the commit |
| 6. Commit | git commit -m "…" |
save a snapshot with a reason |
| 7. Share | git push and open a pull request, or git merge locally |
get it into main |
| 8. Clean up | git branch -d fix/typo |
remove finished branches |
Here is one full round on a local repository:
bash · try it yourself
mkdir handbook && cd handbook && git init -q
printf 'Welcome to the team!\nOur office opens at 9.\n' > handbook.md
git add . && git commit -q -m "Add handbook"
git switch -c fix/opening-hours
sed -i 's/at 9/at 8:30/' handbook.md
git diff
git commit -q -am "Correct office opening time"
git switch main
git merge -q fix/opening-hours
git branch -d fix/opening-hours
git log --oneline
cat handbook.mdSwitched to a new branch 'fix/opening-hours'
diff --git a/handbook.md b/handbook.md
index c80a3ca..e12330c 100644
--- a/handbook.md
+++ b/handbook.md
@@ -1,2 +1,2 @@
Welcome to the team!
-Our office opens at 9.
+Our office opens at 8:30.
Switched to branch 'main'
Deleted branch fix/opening-hours (was 59ad345).
59ad345 Correct office opening time
97def37 Add handbook
Welcome to the team!
Our office opens at 8:30.
Review before you commit
git diff shows unstaged changes, git diff --staged shows what you're about to commit. Looking at the staged diff right before committing catches mistakes such as debug prints and stray files:
bash · try it yourself
mkdir handbook && cd handbook && git init -q
printf 'Welcome!\n' > handbook.md && git add . && git commit -q -m "Add handbook"
printf 'Lunch is at noon.\n' >> handbook.md
echo 'console.log("debug")' > test.js
git add .
git status -s
git diff --staged --stat
git restore --staged test.js && rm test.js
git commit -q -m "Mention lunch time"
git log --onelineM handbook.md
A test.js
handbook.md | 1 +
test.js | 1 +
2 files changed, 2 insertions(+)
ee0c8f4 Mention lunch time
b3a0307 Add handbook
Several pieces of work at once
Branches let you park one task and pick up another. Each branch remembers its own commits, so switching is cheap:
bash · try it yourself
mkdir app && cd app && git init -q
echo "v1" > app.txt && git add . && git commit -q -m "Initial version"
git switch -q -c feature/search
echo "search box" > search.txt && git add . && git commit -q -m "Add search box"
# urgent bug report arrives: fix it on its own branch from main
git switch -q main
git switch -q -c hotfix/crash
echo "v1.0.1" > app.txt && git commit -q -am "Fix crash on start"
git switch -q main && git merge -q hotfix/crash && git branch -q -d hotfix/crash
# back to the feature, and bring in the fix
git switch -q feature/search
git merge -q main -m "Merge main into feature/search"
git log --oneline --graph --all* 5534d2f Merge main into feature/search
|\
| * 867f909 Fix crash on start
* | 199e2d2 Add search box
|/
* 3e99e51 Initial version
The hotfix went into main immediately, and feature/search merged main to stay current.
Choosing a team workflow
| Workflow | Idea | Suits |
|---|---|---|
| GitHub flow | main is always deployable; every change is a short branch + pull request |
most web projects, continuous deployment |
| Git flow | long-lived main and develop, plus feature/, release/ and hotfix/ branches |
software with scheduled, versioned releases |
| Trunk-based | everyone commits to main (or tiny branches merged within a day), unfinished features hidden behind flags |
large teams with strong automated testing |
| Forking | contributors push to their own copy and send pull requests | open-source projects |
The GitHub Flow and Fork lessons show two of these in action.
Test yourself
Which command shows the changes that are staged for the next commit?
- git diff
- git diff --staged
- git log -p
- git status --diff
Why start each task on its own branch?
- Git requires it
- It keeps unfinished work isolated so main stays stable and tasks don't mix
- Branches make commits smaller
- To avoid writing commit messages