0

Committing only parts of your edits is possible by using either git add --interactive or git commit filename. However when combining both methods the results are unexpected.

For example you "stage" one out of four hunks in file1 (among other staging in different files). Then you want to commit only those changes staged for file1, using git commit file1 (git commit would only commit the staged changes of all files).

Unfortunately the result is that all changes made to file1 are committed.

Is this a bug, and if not: Is there a way to commit only the staged changes of a specific file?

U. Windl
  • 492
  • 4
  • 25
  • Never tried similar stuff but it sounds to me that you shouldn't (need to) have `file` after `commit` if you rely on `add` (to "partially stage"). It sounds natural to me if git assume you want to commit *all* (staged and unstaged) changes you made to that file. – Tom Yan Mar 14 '23 at 08:25

1 Answers1

1

I believe you are a little bit confused:

git add --interactive -- Interactive way to stage files.

git commit file -- commit only file. Staged changes ignored during this commit.

git add -p file -- partially stage changes of file

markalex
  • 265
  • 11
  • Well, my assumption was that `git commit file` would only commit the staged changes (that is the intersection of the staged changes and the file's changes). Or is `git commit file` identical to `git add file; git commit file`? – U. Windl Mar 14 '23 at 14:59
  • Yes `git commit file` is identical to `git add file; git commit file`. But you should understand that `git commit file` completely disregards stage, so `git add file` is redundant in this case. @U.Windl – markalex Mar 14 '23 at 15:07
  • I wasn't aware that I can `git commit file` without having to add (stage) it before. – U. Windl Mar 15 '23 at 09:40