我可以使用SVN或Mercurial仅提交部分代码吗?

时间:2021-05-20 20:44:39

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支持这个,如果这是正确的,请告诉我。

6 个解决方案

#1


39  

Mercurial can do this with the record extension.

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 Queues)将存储库中的各个更改分离为修补程序更容易。还有一个MQ变体记录(qrecord)。

Update: Also try the crecord extension, which provides a curses interface to hunk/line selection.

更新:还可以尝试使用crecord扩展,它为hunk / line选择提供了curses接口。

我可以使用SVN或Mercurial仅提交部分代码吗?

#2


12  

Yes, git allows you to do this. The git add command 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 adding which also demonstrates the patch feature of git add.

这是一个关于交互式添加的截屏视频,它还演示了git add的补丁功能。

#3


7  

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.

它甚至允许您对一些文件提交所有更改,并在一次提交中对其他文件进行部分更改。

http://tortoisehg.bitbucket.io/

#4


5  

I asked a similar question just a little while ago, and the resulting answer of using the hgshelve extension was 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.

过去几天我一直在使用它并喜欢它。非常容易可视化和使用。

#5


2  

Mercurial now provides an option --interactive (or -i) to the commit command, which enables this functionality right out of the box.

Mercurial现在为commit命令提供了一个选项--interactive(或-i),它可以立即启用此功能。

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 --patch and --interactive options for the git add and git commit commands.

这与git add和git commit命令的--patch和--interactive选项非常相似。

#6


0  

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 others on 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?

最大的问题是,为什么你觉得有必要这样工作?

#1


39  

Mercurial can do this with the record extension.

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 Queues)将存储库中的各个更改分离为修补程序更容易。还有一个MQ变体记录(qrecord)。

Update: Also try the crecord extension, which provides a curses interface to hunk/line selection.

更新:还可以尝试使用crecord扩展,它为hunk / line选择提供了curses接口。

我可以使用SVN或Mercurial仅提交部分代码吗?

#2


12  

Yes, git allows you to do this. The git add command 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 adding which also demonstrates the patch feature of git add.

这是一个关于交互式添加的截屏视频,它还演示了git add的补丁功能。

#3


7  

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.

它甚至允许您对一些文件提交所有更改,并在一次提交中对其他文件进行部分更改。

http://tortoisehg.bitbucket.io/

#4


5  

I asked a similar question just a little while ago, and the resulting answer of using the hgshelve extension was 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.

过去几天我一直在使用它并喜欢它。非常容易可视化和使用。

#5


2  

Mercurial now provides an option --interactive (or -i) to the commit command, which enables this functionality right out of the box.

Mercurial现在为commit命令提供了一个选项--interactive(或-i),它可以立即启用此功能。

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 --patch and --interactive options for the git add and git commit commands.

这与git add和git commit命令的--patch和--interactive选项非常相似。

#6


0  

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 others on 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?

最大的问题是,为什么你觉得有必要这样工作?