Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

83%
+8 −0
Q&A Git add/stage only part of a file's changes

Git's interactive mode has a patch action. This is the shortcut for it: git add --patch <file> It will split the file into hunks and interactively ask which one's to add. It has a plethor...

posted 7mo ago by Iizuki‭  ·  edited 7mo ago by Iizuki‭

Answer
#2: Post edited by user avatar Iizuki‭ · 2023-10-17T10:28:56Z (7 months ago)
Don't highlight the output of git patch, as it looked silly.
  • Git's [interactive mode](https://git-scm.com/docs/git-add#_interactive_mode) has a [patch action](https://git-scm.com/docs/git-add#Documentation/git-add.txt---patch). This is the shortcut for it:
  • ```bash
  • git add --patch <file>
  • ```
  • It will split the file into hunks and interactively ask which one's to add. It has a plethora of options but selecting `?` explains them nicely:
  • ```
  • <the hunk diff is shown here>
  • (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]? ?
  • y - stage this hunk
  • n - do not stage this hunk
  • q - quit; do not stage this hunk or any of the remaining ones
  • a - stage this hunk and all later hunks in the file
  • d - do not stage this hunk or any of the later hunks in the file
  • j - leave this hunk undecided, see next undecided hunk
  • J - leave this hunk undecided, see next hunk
  • g - select a hunk to go to
  • / - search for a hunk matching the given regex
  • e - manually edit the current hunk
  • ? - print help
  • ```
  • The free [online git book](https://git-scm.com/book/en/v2/Git-Tools-Interactive-Staging) has a good explanation of this too.
  • Git's [interactive mode](https://git-scm.com/docs/git-add#_interactive_mode) has a [patch action](https://git-scm.com/docs/git-add#Documentation/git-add.txt---patch). This is the shortcut for it:
  • ```bash
  • git add --patch <file>
  • ```
  • It will split the file into hunks and interactively ask which one's to add. It has a plethora of options but selecting `?` explains them nicely:
  • ```txt
  • <the hunk diff is shown here>
  • (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]? ?
  • y - stage this hunk
  • n - do not stage this hunk
  • q - quit; do not stage this hunk or any of the remaining ones
  • a - stage this hunk and all later hunks in the file
  • d - do not stage this hunk or any of the later hunks in the file
  • j - leave this hunk undecided, see next undecided hunk
  • J - leave this hunk undecided, see next hunk
  • g - select a hunk to go to
  • / - search for a hunk matching the given regex
  • e - manually edit the current hunk
  • ? - print help
  • ```
  • The free [online git book](https://git-scm.com/book/en/v2/Git-Tools-Interactive-Staging) has a good explanation of this too.
#1: Initial revision by user avatar Iizuki‭ · 2023-10-17T10:19:11Z (7 months ago)
Git's [interactive mode](https://git-scm.com/docs/git-add#_interactive_mode) has a [patch action](https://git-scm.com/docs/git-add#Documentation/git-add.txt---patch). This is the shortcut for it:

```bash
git add --patch <file>
```

It will split the file into hunks and interactively ask which one's to add. It has a plethora of options but selecting `?` explains them nicely:
```
<the hunk diff is shown here>
(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]? ?
y - stage this hunk
n - do not stage this hunk
q - quit; do not stage this hunk or any of the remaining ones
a - stage this hunk and all later hunks in the file
d - do not stage this hunk or any of the later hunks in the file
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
g - select a hunk to go to
/ - search for a hunk matching the given regex
e - manually edit the current hunk
? - print help
```

The free [online git book](https://git-scm.com/book/en/v2/Git-Tools-Interactive-Staging) has a good explanation of this too.