Commit only part of a file in Git

后端 未结 23 2016
一整个雨季
一整个雨季 2020-11-22 05:50

When I make changes to a file in Git, how can I commit only some of the changes?

For example, how could I commit only 15 lines out of 30 lines that have been changed

23条回答
  •  一生所求
    2020-11-22 06:23

    When I have a lot of changes, and will end up creating a few commits from the changes, then I want to save my starting point temporarily before staging things.

    Like this:

    $ git stash -u
    Saved working directory and index state WIP on master: 47a1413 ...
    $ git checkout -p stash
    ... step through patch hunks
    $ git commit -m "message for 1st commit"
    $ git checkout -p stash
    ... step through patch hunks
    $ git commit -m "message for 2nd commit"
    $ git stash pop
    

    Whymarrh's answer is what I usually do, except sometimes there are lots of changes and I can tell I might make a mistake while staging things, and I want a committed state I can fall back on for a second pass.

提交回复
热议问题