2

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.

5
  • C-c does nothing, what are you referring to? Or did you mean c c? With c c you commit only staged files (only if there is nothing staged you are asked if you want to stage everything).
    – giordano
    Oct 19, 2015 at 8:01
  • I mean, the 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.
    – zhouji
    Oct 19, 2015 at 8:09
  • 6
    If I want to commit just one file, I usually stage only that file with s and then commit it with c c. Oct 19, 2015 at 8:29
  • I think the comment of @manuel-uberti is actually the answer.
    – Marco Wahl
    Oct 19, 2015 at 8:53
  • You might want to take note of the Emacs key notation conventions. To most Emacs users, C-c means ctrl-C because that's the notation used throughout Emacs and the documentation. (Also M- for a meta shift, S- for a regular shift, etc.)
    – tripleee
    Nov 6, 2015 at 5:40

1 Answer 1

5

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).

3
  • 1
    Thx, after my study, i found in solution a) I should take cursor on Staged, type u to unstage all files, and stage the single file to commit. And in solution b) , type z and z to stash, and s to stage the file to commit.
    – zhouji
    Oct 19, 2015 at 12:31
  • @byboating you can also use U to unstage all without moving the cursor.
    – npostavs
    Oct 20, 2015 at 0:36
  • 1
    It 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.
    – ldeck
    Oct 19, 2022 at 23:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.