Commit only part of a file in Git

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

    As one answer above shows, you can use git add --patch filename.txt

    or the short-form git add -p filename.txt

    ... but for files already in you repository, there is, in s are much better off using --patch flag on the commit command directly (if you are using a recent enough version of git): git commit --patch filename.txt

    ... or, again, the short-form git commit -p filename.txt

    ... and then using the mentioned keys, (y/n etc), for choosing lines to be included in the commit.

提交回复
热议问题