How to split a hunk and stage part of a hunk in Magit

Maybe you accumulated too many unstaged changes, maybe there are man different changes gathered in a same hunk. No you get a hunk contains totally different code, you may not want to stage them as a whole. If you are in command line, you can use git add -p. But this feature is very limited, you just can not pick out the code you really want to change, you can not pick code in terms of characters in command line tool.

Thats where Magit comes in, it literally allows you select your wanted changes the way you select text in Emacs, it can be anything.

magit stage hunk split

Start select text you want to stage, for example, select the first function, the shortcut key I used to select Emacs is Ctrl + Shift + K to select to next line. After I select the function, it can be anything else, a line, a paragraph, a sentence, .

magit stage hunk select

Now press s, it will be staged, check the staged changes with command line git diff --cached

magit stage cached