'git add --patch' to add new files?

When I run git add -p , is there any way for git to select new files as hunks to select

So, if I create a new file called foo.java , then run git add -p, git will not let me select the contents of this file to be added to the index.

+57
git git-add
Jan 24 '13 at 0:09
source share
4 answers

To do this with each new file, you can run:

 git add -N . git add -p 

If you want to use it often, you can create an alias in ~/.bashrc :

 alias gapan='git add --intent-to-add . && git add --patch' 



NB: If you use this with an empty new file, git will not be able to fix it and move on to the next.

+7
Aug 11 '17 at 17:44 on
source share

When I tried git add -p someNewFile.txt in a new file (file without a trace), git would simply git add -p someNewFile.txt No changes. and stopped. I had to tell git that I intended to track the new file first.

 git add -N someNewFile.txt git add -p 

However, since the file has not been verified, it will appear as one giant piece that cannot be broken (because it is all new!). So, I needed to edit the piece into smaller bits. If you are not familiar with this, check out this link to get started.

Update - Hunk Editing Information I would like to update this if the above link disappears. Since the new file is not tracked, git add -p will display each line in the file as a new line in one column. He will then ask you what you want to do with this hunk by providing you with the following prompt:

Stage this hunk [y,n,q,a,d,/,e,?]?

Assuming you don't want to commit the entire hunk (and therefore the whole file, because I'm not sure why you would like to use git add -p in this case?), You need to specify the e option to tell git that you want to edit hunk.

After you tell git that you want to edit the piece, it should drop you into your editor so that you can make your changes. All lines must have the + prefix, and git contains some explanatory comments (with the # prefix) at the end of the file. Just delete any lines you don't want in your original file commit. Then save and close the editor.

Git explanation of git hunk options:

 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 g - select a hunk to go to / - search for a hunk matching the given regex j - leave this hunk undecided, see next undecided hunk J - leave this hunk undecided, see next hunk k - leave this hunk undecided, see previous undecided hunk K - leave this hunk undecided, see previous hunk s - split the current hunk into smaller hunks e - manually edit the current hunk ? - print help 
+82
Jul 31 '13 at 20:38
source share

There is also a very similar approach using the --cached flag ...

1) Turn your uninstalled changes into phased changes, like your added file.

 git add edited-file.txt git add new-file.txt git add directory-of-changes/ 

2) Look at diff (note: you can include both changes and new files).

 git diff --cached 

3) Create a patch.

 git diff --cached > my_patch_file.patch 
+1
Sep 16 '16 at 20:52
source share

git add -p really adds changes to already tracked files.

The command to interactively select files to add is git add -i . For example:

 $ git add -i *** Commands *** 1: status 2: update 3: revert 4: add untracked 5: patch 6: diff 7: quit 8: help What now> a 1: another-new.java 2: new.java Add untracked>> 2 1: another-new.java * 2: new.java Add untracked>> added one path *** Commands *** 1: status 2: update 3: revert 4: add untracked 5: patch 6: diff 7: quit 8: help What now> q Bye. $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: new.java Untracked files: (use "git add <file>..." to include in what will be committed) another-new.java 

(This team has colors that I could not cut and paste here, so this is better than it seems)

Actually, the p atch git add -i command does the same thing as git add -p , so the second is a subset of the first (even if I admit that I love add -p and hate add -i myself!).

+1
Aug 23 '17 at 14:07 on
source share



All Articles