hg equivalent of git add -p?

白昼怎懂夜的黑 提交于 2019-11-30 16:21:54

问题


Is there a mercurial equivalent of git add -p?

Quoting from man, git-add with the option -p (or --patch) does the following:

Interactively choose hunks of patch between the index and the work tree and add them to the index. This gives the user a chance to review the difference before adding modified contents to the index.


回答1:


Have a look at the record extension (which comes bundled with Mercurial).

Note that since Mercurial doesn't have the concept of the staging area like git, running hg record will simply allow you to examine, hunk by hunk, the modifications in your working copy. Any changes you choose to record will be committed, and any changes you choose not to record are simply left as modifications in your working copy.




回答2:


The Record Extension is the standard tool for this. It allows you to pick hunks to include or not in a commit. Once you've enabled the extension in your hgrc, the command is just

hg record

The CRecord Extension gives you a TUI (Text User Interface) on top of this which allows you to go down to which lines you want to include. This isn't standard though, so it need downloading to a directory before you can enable it in your hgrc.

hg crecord

Edit:

  1. The Record extension is no longer necessary since approx v3.4. Now various commands support the -i or --interactive flag. For example:

    hg commit -i
    

    ...will ask you hunk by hunk what you want to include.

  2. CRecord made it in to 3.8 as a core feature. Add the following to your .hgrc

    [ui]
    interface = curses
    

    Now, --interactive commands will bring up the same interface as the old CRecord extension.




回答3:


As of Mercurial 3.8.1 the crecord extension is builtin. Make sure the “ui” section in your .hgrc contains interface = curses, or you get an uncomfortable question-and-answer-interface.

[ui]
interface = curses

Then do your commits interactively to be asked about which hunk—or—which line you want to include in that commit.

hg commit --interactive



回答4:


If you are using TortoiseHg, it has a Shelve feature which allows you to store changes you don't want to commit to a temporary area. It allows hunk selection, just like git.

In the TortoiseHg Workbench, this tool can be accessed in 2 ways:

  1. Click Repository -> Shelve
  2. In Commit window, select the shelve tool icon. It looks like this:



来源:https://stackoverflow.com/questions/7764914/hg-equivalent-of-git-add-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!