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.

Comments on Git add/stage only part of a file's changes

Parent

Git add/stage only part of a file's changes

+7
−0

Say I've made a bunch of changes to a file and would like to split those changes into two or more commits. Normal git add however stages the whole file in one go.

So how to add only some of the changes in a file?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

Post
+8
−0

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 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 has a good explanation of this too.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

2 comment threads

This doesn't work (2 comments)
TIL (2 comments)
This doesn't work
H_H‭ wrote about 1 year ago · edited about 1 year ago

Try:

$ cd "$(mktemp -d)"
$ git init
Leeres Git-Repository in /tmp/tmp.pBN57TpCk0/.git/ initialisiert
$ echo -e "test-123\nline 2\nline 3" > foo.txt
$ git add --patch foo.txt
No changes.

git just ignores the add command.

Makes no difference if the repository already has commits or not.

r~~‭ wrote about 1 year ago

Yes, for a new file you need to run git add -N foo.txt first (short for git add --intent-to-add). This adds the file to the index but without any content, which allows git add -p to make a diff containing the whole file.