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:
- Created and committed a.txt file
b.txt
file and add it to git, but don't commit or changea.txt
file- They called
git add --patch a.txt
and selected only one change in the file. - We tried to commit changes using the
git commit -v
command - it turned out that theb.txt
file also gets into the commit - 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.