1

I have a certain repository with some files already. Now I created some new file but that had some blocks of code which I want to create different commits from.

$git status
On branch bar
Your branch is up to date with 'bar'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   a.c

Untracked files:

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

        b.c

I want to create two commits from the file b.c with each commit having different pieces of code in them.

Now if I do git add b.c or anything similar then it will directly move b.c to the staged directory.

I have tried using git add -p and git add -i but they don't do what I intend to achieve. Is it possible to do this ?

P.S: This is a use case meant more for trying to understand Git rather than a practical one. I know one obvious answer would be to first write the piece of code that you want to be in the first patch, commit it and then write the code for another patch and commit that. But a use case might arise where you have written a file that might need different reviewers to review different parts of the file and rather than have them wading through the other parts of code, just have them look at what's intended to be reviewed by them. So consider it just as a query on Git.

petrpulc
  • 940
  • 6
  • 22
Zoso
  • 3,273
  • 1
  • 16
  • 27
  • Why doesn’t patch do what you want? – evolutionxbox Jun 28 '18 at 10:07
  • 1
    @evolutionxbox: the root of the problem in this case is that `b.c` is currently *untracked* and for some reason `git add --patch` won't turn that into a patch against `/dev/null`. – torek Jun 28 '18 at 13:19

1 Answers1

3

Set the file as tracked without adding changes to index

git add --intent-to-add b.c

see: Start tracking a file in Git without adding to to the index

Then split the file to hunks

using git add -p or git add -e.

petrpulc
  • 940
  • 6
  • 22