Commit only part of a file in Git

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

    vim-gitgutter plugin can stage hunks without leaving vim editor using

    :GitGutterStageHunk
    

    Beside this, it provides other cool features like a diff sign column as in some modern IDEs

    If only part of hunk should be staged vim-fugitive

    :Gdiff
    

    allows visual range selection then :'<,'>diffput or :'<,'>diffget to stage/revert individual line changes.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 06:45

    For emacs there is also gitsum

    0 讨论(0)
  • 2020-11-22 06:46

    For Atom users, the package github includes interactive staging, in the style of git gui. For shortcuts see the package's documentation.

    Using Atom allows working with a theme that has dark background (by default, git gui has a white background).

    0 讨论(0)
  • 2020-11-22 06:47

    If you are using vim, you may want to try the excellent plugin called fugitive.

    You can see the diff of a file between working copy and index with :Gdiff, and then add lines or hunks to the index using classic vim diff commands like dp. Save the modifications in the index and commit with :Gcommit, and you're done.

    Very good introductory screencasts here (see esp. part 2).

    0 讨论(0)
提交回复
热议问题