Preamble

There is a git add --patch that allows you to determine which changes will go to the commit.

There is a git commit -v <file> command that allows you to commit a specific file.

Question:

How do I commit a patch for a specific file? Provided that the git add <some.other.file> command was previously executed git add <some.other.file> ?

Description:

Consider a series of the following commands:

  1. Created and committed a.txt file
  2. b.txt file and add it to git, but don't commit or change
  3. a.txt file
  4. They called git add --patch a.txt and selected only one change in the file.
  5. We tried to commit changes using the git commit -v command - it turned out that the b.txt file also gets into the commit
  6. We tried to commit changes using the git commit -v a.txt - it turned out that now the file is committed completely

 $ git init Initialized empty Git repository in /home/soon/Src/Git/CommitPatch/.git/ $ cat a.txt Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. $ git add a.txt $ git commit a.txt -m "Added a.txt" [master (root-commit) 697228d] Added a.txt 1 file changed, 8 insertions(+) create mode 100644 a.txt $ touch b.txt $ git add b.txt $ cat a.txt A line Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Another line $ git add a.txt --patch diff --git a/a.txt b/a.txt index 2061a78..0ea2f5a 100644 --- a/a.txt +++ b/a.txt @@ -1,3 +1,4 @@ +A line Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen Stage this hunk [y,n,q,a,d,/,j,J,g,e,?]? y @@ -6,3 +7,4 @@ typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. +Another line Stage this hunk [y,n,q,a,d,/,K,g,e,?]? n $ git commit -v # Commits both patch to the a.txt file and whole b.txt file $ git commit -v a.txt # Commits whole a.txt file $ git commit --patch -v a.txt diff --git a/a.txt b/a.txt index 986cf0b..0ea2f5a 100644 --- a/a.txt +++ b/a.txt @@ -7,3 +7,4 @@ typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. +Another line Stage this hunk [y,n,q,a,d,/,e,?]? n # Behaves like git commit -v 

There is, of course, an option with git rm , but I don’t like it because of its cronyism.

    2 answers 2

    You can try this:

    1. Index the part of a.txt we a.txt (the one we want to commit)

       git add --patch a.txt 
    2. We pocket non-indexed changes. The a.txt indexed part of a.txt also goes into the pocket.

       git stash save --keep-index 
    3. We commit only from the a.txt file.

       git commit a.txt -m'message' 
    4. We take out from the pocket non-indexed changes, including the deferred part a.txt

       git stash pop 
    • @soon: sort of figured out how to get around through the pocket - try. - Nick Volynkin
    • Yes, it worked - thanks! - awesoon
    • @soon: great) In general, it’s annoying that git commit filename does not work with the index, but with the work area - it would be convenient. - Nick Volynkin
    • By the way, I did not find a similar question in the English segment of the Internet. There is a possibility that I was looking bad, because I did not invent a better git commit patch request. However, isn't this a git proposal? Of course, this is not such a frequent problem, but I was somewhat embarrassed by why git commit --patch -v a.txt also git commit --patch -v a.txt changes in the b.txt file. - awesoon
    • @soon also thought about proposal. Do you know how? - Nick Volynkin

    Just do git stash before running the git add --patch .

    • It did not work - a.txt also hid. @NickVolynkin had a similar sentence - you can see it in the answer editing history - awesoon
    • If I understand correctly, after git stash there will be nothing to index. - Nick Volynkin