Practical Git guide

How to stage only part of a file in Git

Use git add --patch to stage one change while leaving another edit in the same file out of your commit.

Before you start

Two unrelated changes share a file. You want to commit the heading correction while leaving an unfinished footer edit pending. Separate the edits by enough context that Git presents two hunks.

Use Git 2.28 or newer in a POSIX shell on macOS/Linux, or Git Bash on Windows. Check git --version; Git's installation page covers setup. Each example starts in a new temporary folder. Keep that folder if you want to revisit your work; none of these commands removes your existing projects. The identity and signing settings below apply only to the practice repository.

Work through the example

practice=$(mktemp -d "${TMPDIR:-/tmp}/gitglow-learn.XXXXXX")
sandbox="$practice/project"
mkdir "$sandbox"
cd "$sandbox"
git init -b main
git config --local user.name "Git Learner"
git config --local user.email "learner@example.invalid"
git config --local commit.gpgsign false
printf 'Heading: draft\nLine 02\nLine 03\nLine 04\nLine 05\nLine 06\nLine 07\nLine 08\nLine 09\nLine 10\nLine 11\nFooter: draft\n' > notes.txt
git add notes.txt
git commit -m "Start document"
sed 's/Heading: draft/Heading: reviewed/; s/Footer: draft/Footer: unfinished/' notes.txt > notes.next
mv notes.next notes.txt
git diff -- notes.txt

Run patch staging. At the first hunk prompt press y and Enter; at the second press n and Enter. The prompt itself explains the available choices:

git add --patch -- notes.txt

Inspect both sides before committing:

git diff --cached -- notes.txt
git diff -- notes.txt
git commit -m "Review document heading"
git status --short

Check the result

The committed version has Heading: reviewed and Footer: draft. The working file still has Footer: unfinished. Final status shows one unstaged modification. Patch staging does not delete the edit you declined.

Try it yourself

Use git restore --staged notes.txt before committing to undo a staging choice. If two edits appear in one hunk, use the prompt’s s option when splitting is available; inspect the cached diff again.

If something goes wrong

If Git shows one hunk here, confirm the unchanged middle lines match the setup. Patch selection works on tracked text differences; inspect new files and binary changes separately. When staging choices are unclear, unstage and review rather than committing the whole file.

Use this workflow in GitGlow

In Changes, select the file and stage the desired hunk or supported consecutive changed lines. File, hunk, and supported line staging are Free desktop actions. Review the staged diff in the commit composer before confirming.

Product capture from a sample repository; your exercise files will differ.
Download GitGlow Free

Go deeper with the official Git docs

These references cover command options and behavior beyond this example.