When I type c-c
(type c twice) in staged files, it will commit all the staged files, however i only want to commit a single file of the staged files.
How to do it? Thx.
1 Answer
You can't just commit a part of the index. The index/staging area is where your next commit is built. Asking to commit only part of it makes no sense.
I'm going to speculate that you've staged a bunch of changes, you don't want to unstage them all (because then you'd need to work through all the changes again to find and re-stage them), but you've realised you actually wanted to make multiple commits out of what you've staged.
There are many ways you could achieve the end goal. A couple of fairly straightforward options are:
a) Unstage the bit you wanted to commit, and then go ahead and commit the rest. Then stage and commit the thing you wanted to commit first. If you're unhappy with the commit order, re-order them afterwards in an interactive rebase.
b) Using Magit's stashing facilities, stash the worktree (only), leaving you with no changes other than what is staged. Now unstage everything, and re-stage the thing you want to commit first (and commit it). Then re-stage all of the unstaged changes and finally apply the stash to get back to where you started (new commit excepted).
-
1Thx, after my study, i found in solution a) I should take cursor on
Staged
, typeu
to unstage all files, and stage the single file to commit. And in solution b) , typez
andz
to stash, ands
to stage the file to commit.– zhoujiOct 19, 2015 at 12:31 -
@byboating you can also use
U
to unstage all without moving the cursor.– npostavsOct 20, 2015 at 0:36 -
1It is possible via the terminal to commit only some files. See man git-commit. Essentially
git commit [files] -m "your message"
has an implied--only
flag to only commit the staged changes related to the given files.– ldeckOct 19, 2022 at 23:41
C-c
does nothing, what are you referring to? Or did you meanc c
? Withc c
you commit only staged files (only if there is nothing staged you are asked if you want to stage everything).c-c
is type c twice on the staged files. When i do this, it will commit all the staged files, but i only want to commit one file.s
and then commit it withc c
.C-c
means ctrl-C because that's the notation used throughout Emacs and the documentation. (AlsoM-
for a meta shift,S-
for a regular shift, etc.)