I have two files not staged for commmit:
- src/A.java
- src/B.java
I want add and commit B.java in one line. What is the most convenient way to this, like a one liner.
I'm aware of git commit -am "Commit message"
, I need to do it for specific file
I have two files not staged for commmit:
I want add and commit B.java in one line. What is the most convenient way to this, like a one liner.
I'm aware of git commit -am "Commit message"
, I need to do it for specific file
You can also run either:
git commit --only <paths>
(add -m <message>
if desired), or:
git commit --include <paths>
(again, add -m <message>
if desired). This—to oversimplify it a bit—has the effect of adding the specified paths to the index (--include
) or to a temporary index (--only
), then committing using the index you chose with --only
vs --include
.
If you used --include
, the completed commit that becomes the new HEAD commit on the current branch is automatically represented by the augmented index. This case is the simpler one to describe: it really does work as if you had git add
ed those files, since it carries along any earlier git add
ed files.
If you used --only
, the completed commit that becomes the new HEAD represents something that's not necessarily in the normal index. This case is more complicated, since the index should normally represent (and hence contain all the same files as) the HEAD commit until you start git add
ing files to overwrite the index versions. The internal details here are that Git copies the --only
files to the regular index after the new commit is safely installed.
Retaining the oversimplification (because all versions really use some special temporary index files), we can diagram this as:
Normal commit: uses the index as the source for all the files committed. After the commit completes, the index is still the index, and still contains all the files.
Hence, if you git add fileA
and then git commit
, the new commit has the new fileA
(and the same old every-other-file as before). The index and the new HEAD
commit match.
git commit --include <file>
: Acts a lot like git add
followed by git commit
.
Hence, if you git add fileA
and then git commit --include fileB
, the new commit has the new fileA
and the new fileB
(and the same old every-other-file as before). The index and the new HEAD
commit match.
git commit --only <file>
: Copies the HEAD
commit to a temporary index, adds <file>
to the temporary index, and runs git commit
using the temporary index. On success, adds <file>
to the index as well.
Hence, if you git add fileA
and then git commit --only fileB
, the new commit has the old fileA
(from the previous HEAD
commit), the new fileB
(from the temporary index), and the same old every-other-file as before. The index after committing has the new fileA
and the new fileB
, so the index after committing differs from the new HEAD
commit in that it has a different version of fileA
.
Since git commit -am
would add everything, you still need to add first, then commit, but at least you can do it in one line
git add src/B.java && git commit -m "add B"
The point is, there is no native Git command for adding and committing just one file (or a subset of the modified files)