Commits, branches, merges & pull requests
Work on branches and open pull requests confidently. (The GitHub path goes deeper.)
By Invicly Tech, , in Full DevOps with Python. About 13 minutes to read.
In short
Git records snapshots of your code as commits. Branches let you work without breaking main, and pull requests let teammates review changes before they merge.
Explained simply
Git is a tool that remembers every saved version of your project. Each saved version is called a commit. If something breaks, you can see exactly what changed, who changed it and why, and go back. In DevOps, Git holds not only app code but also Dockerfiles, pipelines and infrastructure files.
Saving takes two steps. First you choose which changes to include with git add; this is called staging. Then git commit saves those staged changes with a short message. The two steps let you save one tidy change at a time, even if you edited many files.
A branch is a separate line of work. You create one for each feature or fix, so the main branch stays stable while you experiment. When the work is ready, you merge the branch back into main.
On GitHub, merging usually goes through a pull request (PR): a page that shows your changes, runs automatic checks, and lets teammates comment and approve. It is the team's safety net and the place where knowledge is shared.
Press Play on a diagram to walk through it one step at a time.
- 1Branch from main
git switch -c feature/health-check
- 2Stage and commit
Small commits with clear messages.
- 3Push branch
git push -u origin feature/health-check
- 4Open pull request
Describe what changed and how to test it.
- 5Review and fix
Address comments with new commits.
- 6Merge to main
CI passes, reviewer approves, branch is deleted.
- Author to GitHub: Push branch, open PR1Push branch, open PR
- GitHub to CI: Run tests and lint on the PR2Run tests and lint on the PR
- CI to GitHub: Checks passed3Checks passed
- GitHub to Reviewer: Review requested4Review requested
- Reviewer to GitHub: Comment: please add a test5Comment: please add a test
- Author to GitHub: Push a new commit with the test6Push a new commit with the test
- Reviewer to GitHub: Approve7Approve
- Author to GitHub: Squash and merge, delete branch8Squash and merge, delete branch
In depth
What a commit and a branch really are
A commit is not a diff. It is a snapshot of the whole project, stored efficiently: files that did not change are not stored again, the commit just points to the same content as before. Each commit also records its author, date, message and the commit before it (its parent). Git names it by a hash of all that content, the long hex ID you see in git log. Change anything and you get a different ID.
A branch is just a small file holding one commit ID, stored under .git/refs/heads/. Creating a branch writes one tiny file, which is why branches in Git are instant and cheap. When you commit, Git moves the current branch forward to the new commit. HEAD is a pointer to the branch you are on right now.
- 1HEAD
Points to the branch you are on
- 2Branch main
A file holding one commit ID
- 3Commit
Author, message, parent, top tree
- 4Tree
A folder listing: names to blobs and subtrees
- 5Blob
One file's contents, stored once
The three places your changes live
Every change sits in one of three places. The working tree is the files you see and edit. The staging area (also called the index) holds what will go into the next commit. The repository holds the commits themselves. git diff compares the working tree with the staging area, and git diff --staged compares the staging area with the last commit, so together they show exactly what you are about to save.
git add -p lets you stage part of a file, hunk by hunk, which is how you split a messy afternoon of edits into clean commits. git restore file throws away unstaged edits to a file, and git restore --staged file takes it back out of the staging area without losing the edits.
Commits are local until you push. git push sends them to the remote (usually called origin), and git fetch or git pull bring other people's commits down.
- Step 1Working tree
You edit files
- Step 2git add
Copy the change into the staging area
- Step 3git commit
Save a snapshot in your local repo
- Step 4git push
Send commits to origin on GitHub
Writing commits people can use
A good commit holds one logical change: one fix, one feature step, one refactor. That makes it easy to review, easy to revert, and easy to find later with git log or git bisect.
The message has a short subject line, about 50 characters, written as a command: "Add retry to S3 upload", not "added stuff". If the change needs explaining, leave a blank line and write a body that says why, wrapped at about 72 characters. The diff already shows what changed; only the message can explain the reason.
Some teams use Conventional Commits, with prefixes like feat:, fix: and docs:, so tools can build changelogs. On GitHub, writing Fixes #123 in a commit or PR description closes issue 123 when the change is merged into the default branch.
Before pushing, you can still polish: git commit --amend changes the last commit. After pushing to a shared branch, do not rewrite history; add a new commit instead.
- Subject: imperative, about 50 characters, no full stop
- Body: why the change was needed, not a list of files
- Never commit secrets: add .env to .gitignore first
How merging works, and what a conflict is
When you merge a feature branch into main, Git finds their common ancestor, the last commit both share. If main has not moved since you branched, Git just slides main forward to your latest commit. This is a fast-forward, and no new commit is created. If both sides have new commits, Git does a three-way merge: it compares each side with the ancestor and combines the changes into a new merge commit with two parents.
A conflict happens only when both sides changed the same lines (or one side deleted a file the other edited). Git stops and marks the spot in the file: the lines between the <<<<<<< and ======= markers are yours, the lines up to >>>>>>> are theirs. You edit the file into the final version, delete the markers, git add it and commit. git merge --abort cancels the whole merge and puts everything back.
- git log --oneline --graph --all draws how branches split and join
- git status lists every conflicted file as both modified
- Pull main into long branches often, so conflicts stay small
Pull requests, reviews and merge options
A pull request asks to merge one branch into another. GitHub shows the diff, runs your CI checks on it, and collects comments. Teams usually protect main with rules: changes must come through a PR, at least one approval is needed, and checks must pass.
As an author, keep PRs small (a few hundred lines at most is a common guide), write what changed and how to test it, and open a draft PR early if you want feedback. As a reviewer, run the code when it matters: gh pr checkout 42 fetches the branch locally. Ask questions instead of giving orders, separate must-fix issues from small suggestions, and approve when it is good enough, not perfect.
GitHub offers three merge buttons: a merge commit keeps every commit plus a merge commit, squash and merge turns the whole PR into one commit on main, and rebase and merge replays the commits onto main without a merge commit.
- Keeps every branch commit
- Adds a merge commit with two parents
- Full history, but noisier
- Whole PR becomes one commit
- Clean, linear main
- Small branch commits are lost
- Replays each commit onto main
- Linear, keeps every commit
- Commit IDs change
Learn it in 5 short steps
About 50m each. Track them with a timer in your plan.
- 1Stage, commit and write good messages~50m
- 2Create and switch branches~50m
- 3Merge branches and resolve simple conflicts~50m
- 4Push and open a pull request~50m
- 5Review and merge a teammate's PR~50m
Key points
- git add stages changes; git commit saves the staged snapshot with a message.
- Write messages in the imperative: 'Add health check endpoint'.
- Conflicts happen when both sides change the same lines; edit, add, commit.
- Keep branches short-lived and pull main often to avoid big conflicts.
Key terms
Tap a card to flip it. 0/5 known
Examples
git switch main && git pull
git switch -c feature/health-check
git add app/health.py
git commit -m "Add /health endpoint"
git push -u origin feature/health-check
gh pr create --fillFrom fresh main to an open pull request.
Resolve a simple merge conflict
You changed the port in app/config.py on your branch, and a teammate changed the same line on main. Step 1: merge main into your branch; Git reports a conflict. Step 2: git status lists the conflicted file. Step 3: open it and you see both versions between markers. Step 4: keep the line you want (or combine both), delete the three marker lines. Step 5: git add marks it resolved, git commit finishes the merge, and you push. Run the tests before pushing.
git switch feature/port
git fetch origin && git merge origin/main
# CONFLICT (content): Merge conflict in app/config.py
git status # both modified: app/config.py
# the file now shows:
# <<<<<<< HEAD
# PORT = 8080
# =======
# PORT = 8000
# >>>>>>> origin/main
# edit it to the final line, delete the markers, then:
git add app/config.py
git commit --no-edit # finishes the merge
git pushFix mistakes before and after pushing
Step 1: you forgot a test file in your last commit, which is not pushed yet, so amend it. Step 2: you staged .env by accident; unstage it and add it to .gitignore. Step 3: you want to redo the last commit completely; reset --soft removes the commit but keeps the changes staged. Step 4: a bad commit already reached shared main; do not rewrite history there, revert it, which adds a new commit that undoes the old one.
git add tests/test_upload.py
git commit --amend --no-edit # add it to the last, unpushed commit
git restore --staged .env # unstage a file added by mistake
echo ".env" >> .gitignore
git reset --soft HEAD~1 # undo the last commit, keep changes staged
git revert a1b2c3d # on shared main: a new commit that undoes a1b2c3d
git log --oneline --graph -10Quick reference
- Run git status and git diff --staged before every commit to see what you are saving
- git add -p stages part of a file, one hunk at a time
- git switch -c name creates a new branch and switches to it
- git restore file discards local edits; git restore --staged file unstages it
- git log --oneline --graph --all draws the branch history in the terminal
- git merge --abort leaves a conflicted merge and restores the state before it
- After a rebase, git push --force-with-lease is the safer way to force push
- Fixes #123 in a PR description closes the issue when merged into the default branch
Common mistakes
- Committing secrets or .env files; add them to .gitignore before the first commit.
- Huge PRs that mix many changes are slow and hard to review.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Create a new repository, make two commits that change the same file, then show the history and the difference between the two commits.
- Exercise 2medium
Create a conflict on purpose: two branches change the same line of one file. Merge them, resolve the conflict, and show the merge commit in the graph.
- Exercise 3hard
Your feature branch is far behind main. Rebase it onto the latest origin/main, resolve a conflict during the rebase, run the tests, and update the remote branch safely.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- What is the difference between git fetch and git pull?
- git fetch downloads new commits from the remote but changes none of your branches. git pull is a fetch followed by a merge (or a rebase, if configured) into your current branch.
- Should I merge or rebase?
- Both combine work. Rebase gives a cleaner, linear history but rewrites commit IDs, so only rebase branches that nobody else is working on. When in doubt, merge.
- I committed a password. Is deleting it in a new commit enough?
- No, it is still in the history. Treat the secret as leaked and rotate it first, then remove it from history with a tool like git filter-repo if needed.
- What is the difference between Git and GitHub?
- Git is the version control tool that runs on your machine. GitHub is a hosting service for Git repositories that adds pull requests, reviews, issues and CI.