How do you create parts of a new file?

I tried the following sentence:

How to create only part of a new file with git?

git add -N new_file
git add -i

But I can’t make it work, because the interactive mode presents the whole file without a choice s, which will allow me to split the file into smaller parts and thereby execute the part of the file:

~/git_practice$ git init my_project
Initialized empty Git repository in /Users/7stud/git_practice/.git/

~/git_practice$ cd my_project

~/git_practice/my_project$ git status
On branch master

Initial commit

nothing to commit (create/copy files and use "git add" to track)

~/git_practice/my_project$ echo This is the README file. > README.txt
~/git_practice/my_project$ ls
README.txt

~/git_practice/my_project$ git status
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    README.txt

nothing added to commit but untracked files present (use "git add" to track)

~/git_practice/my_project$ git add README.txt 
~/git_practice/my_project$ git commit -m "Add README file."
[master (root-commit) e815ed7] Add README file.
 1 file changed, 1 insertion(+)
 create mode 100644 README.txt

~/git_practice/my_project$ git status
On branch master
nothing to commit, working directory clean

~/git_practice/my_project$ git checkout -b new_feature
Switched to a new branch 'new_feature'

~/git_practice/my_project$ m new_feature.rb

(m is the alias that I set for the mvim command, which launches the macvim text editor.)

This is the code I entered in new_feature.rb:

def addition(x, y)
  x+y
end

def substraction(x,y)
  x-y
end

#Uh oh!  I got carried away and created two new features.  
#I want to split addition/subtraction into two commits.

Back to the command line:

~/git_practice/my_project$ git add -p new_feature.rb 
No changes.  

This did not work. Instead, I had to do:

 ~/git_practice/my_project$ git add -N new_feature.rb

As far as I can tell, this adds an empty version of new_feature.rb to the staging area; then you can fix this empty file with part of the code in new_feature.rb:

~/git_practice/my_project$ git add -i new_feature.rb 

           staged     unstaged path
  1:        +0/-0       +10/-0 new_feature.rb

*** Commands ***
  1: status   2: update   3: revert   4: add untracked
  5: patch    6: diff     7: quit     8: help

-i . git , . git . , , (, p ):

What now> p
           staged     unstaged path
  1:        +0/-0       +10/-0 new_feature.rb

, , , ( staged , , unstaged , 10 ):

Patch update>> 1
           staged     unstaged path
* 1:        +0/-0       +10/-0 new_feature.rb

, . , Return :

Patch update>> <Hit Return>

diff --git a/new_feature.rb b/new_feature.rb
index e69de29..b44829e 100644
--- a/new_feature.rb
+++ b/new_feature.rb
@@ -0,0 +1,10 @@
+def addition(x, y)
+  x+y
+end
+
+def substraction(x,y)
+  x-y
+end
+
+#Uh oh!  I got carried away and created two new features.  
+#I want to separate addition/subtraction into two commits.

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

:

e, :

# Manual hunk edit mode -- see bottom for a quick guide                       
@@ -0,0 +1,10 @@                                                              
+def addition(x, y)                                                           
+  x+y                                                                        
+end                                                                          
+                                                                             
+def substraction(x,y)                                                        
+  x-y                                                                        
+end                                                                          
+                                                                             
+#Uh oh!  I got carried away and created two new features.                    
+#I want to separate addition/subtraction into two commits.                   
# ---                                                                         
# To remove '-' lines, make them ' ' lines (context).                         
# To remove '+' lines, delete them.                                           
# Lines starting with # will be removed.                                      
#                                                                             
# If the patch applies cleanly, the edited hunk will immediately be           
# marked for staging. If it does not apply cleanly, you will be given         
# an opportunity to edit again. If all lines of the hunk are removed,         
# then the edit is aborted and the hunk is left unchanged.                    
~                                                                               
~                                                                               
"~/git_practice/my_project/.git/addp-hunk-edit.diff" 21L, 671C

(new_feature.rb). , , . :

# Manual hunk edit mode -- see bottom for a quick guide                       
@@ -0,0 +1,10 @@                                                              
+def addition(x, y)                                                           
+  x+y                                                                        
+end                                                                          


# ---                                                                         
# To remove '-' lines, make them ' ' lines (context).                         
# To remove '+' lines, delete them.                                           
# Lines starting with # will be removed.                                      
#                                                                             
# If the patch applies cleanly, the edited hunk will immediately be           
# marked for staging. If it does not apply cleanly, you will be given         
# an opportunity to edit again. If all lines of the hunk are removed,         
# then the edit is aborted and the hunk is left unchanged.                    
~                                                                               
~                                                                               
"~/git_practice/my_project/.git/addp-hunk-edit.diff" 21L, 671C

:

git commit -m "Add addition() method."

diff, , git v. :

~/git_practice/my_project$ git diff new_feature.rb 

diff --git a/new_feature.rb b/new_feature.rb
index 6579fef..b44829e 100644
--- a/new_feature.rb
+++ b/new_feature.rb
@@ -1,3 +1,10 @@
 def addition(x, y)
   x+y
 end
+
+def substraction(x,y)
+  x-y
+end
+
+#Uh oh!  I got carried away and created two new features.  
+#I want to separate addition/subtraction into two commits.

A blank , . A + , , . ( - , , .) diff, , @@ -1,3 +1,10 @@, . .

:

git add -p new_feature.rb

( git add -i new_feature.rb, p).

e :

+#Uh oh!  I got carried away and created two new features.                    
+#I want to separate addition/subtraction into two commits.

:

git commit -m "Add subtraction() method."

, , . , new_feature.rb git status , . , reset , git:

git checkout new_feature.rb

- .

git status:

$ git status
On branch new_feature
nothing to commit, working directory clean

:

$ git log

commit 70c566157a0f41052c6091ce9025d8b95722015f
Author: 7stud <me@me.com>
Date:   Tue May 26 13:06:21 2015 0000

    Add subtraction() method.

commit 2ca5952c53bae7bc407d21cb3601395886d2cd4c
Author: 7stud <me@me.com>
Date:   Tue May 26 13:05:41 2015 0000

    Add addition() method.

commit 72ae28cbd1d7cf998eca5862b18e2af45b58f752
Author: 7stud <me@me.com>
Date:   Tue May 26 13:00:55 2015 0000

    Add README file.
+4
1

hunk , . e diff. vim .

:

# Manual hunk edit mode -- see bottom for a quick guide
@@ -0,0 +1,10 @@
+def addition(x, y)
+  x+y
+end
+
+def substraction(x,y)
+  x-y
+end
+
+#Uh oh!  I got carried away and created two new features.  
+#I want to separate addition/subtraction into two commits.
# ---
# To remove '-' lines, make them ' ' lines (context).
# To remove '+' lines, delete them.
# Lines starting with # will be removed.
#
# If the patch applies cleanly, the edited hunk will immediately be
# marked for staging. If it does not apply cleanly, you will be given
# an opportunity to edit again. If all lines of the hunk are removed,
# then the edit is aborted and the hunk is left unchanged.
+3

All Articles