2.7 KiB
2.7 KiB
Git Add Methods
Git Add Lines
git add -e
Select lines of changes interactively. This allows you to stage only specific lines of changes in a file.
Pros: More control over what gets staged.
Cons: More manual work, you have to edit the patch file yourself, which can be error-prone and time-consuming.
Guidelines:
- The editor shows a unified diff patch of working tree changes.
+lines: additions in the working tree (will be staged if kept).-lines: deletions in the working tree (will be staged if kept).- Context lines (no prefix): unchanged lines needed for patch context.
- To stage an addition: keep the
+line in the patch. - To NOT stage an addition: delete the
+line from the patch. - To stage a deletion: keep the
-line in the patch. - To NOT stage a deletion: convert the
-to a space. - For modified content (shown with
-lines followed by+lines):- To NOT stage the modification: convert the
-lines to spaces and remove the+lines. - Note: modifying only half of the pair may cause confusing changes to the index.
- To NOT stage the modification: convert the
- To keep a line unchanged: leave context lines as-is.
- If you want to decline the operation entirely, delete all lines of the patch.
- If you break the patch, run
git checkout -- <file>to undo, then rungit add -eagain.
Git Add Hunks
git add -p
Select hunks of changes interactively. This allows you to stage only parts of the changes in a file.
Pros: Easier to use than the -e method for staging specific parts of changes.
Cons: Sometimes split won't work, which will force you to use the -e method.
Guidelines
- Run
git add -p <file>orgit add -pto go through patch hunks interactively. - For each hunk, choose:
yto stage the hunknto not stage the hunkqto quit; do not stage the hunk or any remaining onesato stage the hunk and all later hunks in the filedto not stage the hunk or any later hunks in the filegto select a hunk to go to/to search for a hunk matching the given regexjto go to the next hunkJto go to the previous hunkkto go to the previous hunkKto go to the previous undecided hunksto split the current hunk into smaller oneseto manually edit the current hunk?to print help
- If you choose
s, Git will attempt to split the hunk into smaller hunks so you can stage a smaller section. - If you choose
e, Git opens the patch in a temporary editor; delete+lines you do NOT want staged and convert-lines to spaces to prevent staging their deletion, then save and close. - When finished, run
git statusto verify which hunks were staged.