Commit only part of a file in Git

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

    git-meld-index -- quoting from the website:

    git-meld-index runs meld -- or any other git difftool (kdiff3, diffuse, etc.) -- to allow you to interactively stage changes to the git index (also known as the git staging area).

    This is similar to the functionality of git add -p, and git add --interactive. In some cases meld is easier / quicker to use than git add -p. That's because meld allows you, for example, to:

    • see more context
    • see intra-line diffs
    • edit by hand and see 'live' diff updates (updated after every keypress)
    • navigate to a change without saying 'n' to every change you want to skip

    Usage

    In a git repository, run:

    git meld-index
    

    You'll see meld (or your configured git difftool) pop up with:

    LEFT: temporary directory contining files copied from your working tree

    RIGHT: temporary directory with the contents of the index. This also includes files that are not yet in the index but are modified or untracked in the working copy -- in this case you'll see the file contents from HEAD.

    Edit the index (right hand side) until happy. Remember to save when needed.

    When you're done, close meld, and git-meld-index will update the index to match the contents of the temporary directory on the right hand side of meld that you just edited.

提交回复
热议问题