Commit only part of a file in Git

后端 未结 23 1982
一整个雨季
一整个雨季 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:42

    Much like jdsumsion's answer you can also stash your current work but then use a difftool like meld to pull selected changes from the stash. That way you can even edit the hunks manually very easy, which is a bit of a pain when in git add -p:

    $ git stash -u
    $ git difftool -d -t meld stash
    $ git commit -a -m "some message"
    $ git stash pop
    

    Using the stash method gives you the opportunity to test, if your code still works, before you commit it.

提交回复
热议问题