git 我可以使用 SVN 或 Mercurial 仅提交部分代码吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/149198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I commit only parts of my code using SVN or Mercurial?
提问by RobbieGee
I use Tortoise SVN usuallly, but I have been looking into Mercurial since it is a distributed revision control system.
我通常使用 Tortoise SVN,但我一直在研究 Mercurial,因为它是一个分布式版本控制系统。
What I am looking for, in both systems, is a tool that let me pick only parts of a file and commit them. If I want to do this now, I have to copy to a temp version of the file and keep only the changes I want to commit in the current version, and then copy the temp version to the current version again after committing. It's just such a hassle and the program should be able to do this for me.
在这两个系统中,我正在寻找一种工具,它让我只选择文件的一部分并提交它们。如果我现在想这样做,我必须复制到文件的临时版本并仅保留我想在当前版本中提交的更改,然后在提交后再次将临时版本复制到当前版本。这真是太麻烦了,程序应该能够为我做到这一点。
I heard Git supports this, please let me know if this is correct.
我听说 Git 支持这个,请告诉我这是否正确。
回答by Nicholas Riley
Mercurial can do this with the recordextension.
Mercurial 可以通过记录扩展来做到这一点。
It'll prompt you for each file and each diff hunk. For example:
它会提示您输入每个文件和每个差异块。例如:
% hg record
diff --git a/prelim.tex b/prelim.tex
2 hunks, 4 lines changed
examine changes to 'prelim.tex'? [Ynsfdaq?]
@@ -12,7 +12,7 @@
\setmonofont[Scale=0.88]{Consolas}
% missing from xunicode.sty
\DeclareUTFcomposite[\UTFencname]{x00ED}{\'}{\i}
-\else
+\else foo
\usepackage[pdftex]{graphicx}
\fi
record this change to 'prelim.tex'? [Ynsfdaq?]
@@ -1281,3 +1281,5 @@
%% Local variables:
%% mode: latex
%% End:
+
+foo
\ No newline at end of file
record this change to 'prelim.tex'? [Ynsfdaq?] n
Waiting for Emacs...
After the commit, the remaining diff will be left behind:
提交后,剩余的差异将被留下:
% hg di
diff --git a/prelim.tex b/prelim.tex
--- a/prelim.tex
+++ b/prelim.tex
@@ -1281,3 +1281,5 @@
%% Local variables:
%% mode: latex
%% End:
+
+foo
\ No newline at end of file
Alternatively, you may find it easier to use MQ (Mercurial Queues) to separate the individual changes in your repository into patches. There is a MQ variant of record (qrecord), too.
或者,您可能会发现使用 MQ(Mercurial 队列)将存储库中的单个更改分成补丁更容易。记录 (qrecord) 也有一个 MQ 变体。
Update:Also try the crecordextension, which provides a curses interface to hunk/line selection.
更新:还可以尝试crrecord扩展,它提供了一个用于大块/行选择的Curses接口。


回答by Pat Notz
Yes, git allows you to do this. The git addcommand has a -p(or --patch) option that allows you to review your changes hunk-by-hunk, select which to stage (you can also refine the hunks or, edit the patches in place). You can also use the interactive mode to git-add (git add -i) and use the "p" option.
是的,git 允许你这样做。该git add命令有一个-p(或--patch) 选项,允许您逐个查看更改,选择要暂存的内容(您还可以优化大块或就地编辑补丁)。您还可以使用交互模式来 git-add ( git add -i) 并使用“p”选项。
Here's a screencast on interactive addingwhich also demonstrates the patch feature of git add.
回答by Hauge
Check out TortoiseHG, which will do hunk selection and let you commit different changes to one file as to different commits.
查看 TortoiseHG,它将进行大块选择,并让您针对不同的提交对一个文件提交不同的更改。
It will even let you commit all changes to some files together with partial changes to other files in one commit.
它甚至可以让您在一次提交中提交对某些文件的所有更改以及对其他文件的部分更改。
回答by Ted Naleid
I asked a similar questionjust a little while ago, and the resulting answer of using the hgshelve extensionwas exactly what I was looking for.
不久前我问了一个类似的问题,使用hgshelve 扩展的结果正是我想要的。
Before you do a commit, you can put changes from different files (or hunks of changes within a file) on the "shelf" and then commit the things you want. Then you can unshelve the changes you didn't commit and continue working.
在进行提交之前,您可以将来自不同文件的更改(或文件中的大量更改)放在“架子”上,然后提交您想要的内容。然后,您可以取消未提交的更改并继续工作。
I've been using it the past few days and like it a lot. Very easy to visualize and use.
过去几天我一直在使用它,并且非常喜欢它。非常容易可视化和使用。
回答by Oly
Mercurial now provides an option --interactive(or -i) to the commitcommand, which enables this functionality right out of the box.
Mercurial 现在为命令提供了一个选项--interactive(或-i)commit,可以立即启用此功能。
This works directly from the command-line so it's perfect if you are a command-line enthusiast!
这直接从命令行工作,因此如果您是命令行爱好者,它是完美的!
Running
跑步
> hg commit -i
begins an interactive session which allows examination, editing and recording of individual changes to create a commit.
开始一个交互式会话,允许检查、编辑和记录单个更改以创建提交。
This behaves very similarly to the --patchand --interactiveoptions for the git addand git commitcommands.
这与and命令的--patchand--interactive选项非常相似。git addgit commit
回答by craigb
I would recommend not working like this.
我建议不要这样工作。
If you have to sets of changes, set A which is ready to check in and set B which is not ready yet, how can you be sure that only checking in set A will not break your build/tests? You may miss some lines, forget about lines in a different file, or not realize a dependency that A has on B breaking the build for others.
如果您必须更改设置,设置 A 准备签入和设置 B 尚未准备好,您如何确保仅签入 A 集不会破坏您的构建/测试?您可能会错过一些行,忘记其他文件中的行,或者没有意识到 A 对 B 的依赖破坏了其他人的构建。
Your commits should be discreet atomic changes that don't break the build for you or otherson you team. If you are partially committing a file you are greatly increasing the chances you will break the build for others without knowing about it until you've got some unhappy coworker knocking on your door.
您的提交应该是谨慎的原子更改,不会破坏您或您团队中的其他人的构建。如果您正在部分提交一个文件,那么您将大大增加为其他人破坏构建的机会,直到您有一些不高兴的同事敲门。
The big question is, why do you feel the need to work this way?
最大的问题是,为什么你觉得有必要以这种方式工作?

