Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I commit only parts of my code using SVN or Mercurial?

I use Tortoise SVN usuallly, but I have been looking into Mercurial since it is a distributed revision control system.

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.

like image 949
RobbieGee Avatar asked Sep 29 '08 15:09

RobbieGee


People also ask

What is a commit in git?

Commits are the core building block units of a Git project timeline. Commits can be thought of as snapshots or milestones along the timeline of a Git project. Commits are created with the git commit command to capture the state of a project at that point in time.


2 Answers

Mercurial can do this with the record extension.

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.

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

crecord screenshot

like image 199
Nicholas Riley Avatar answered Oct 08 '22 18:10

Nicholas Riley


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.

Here's a screencast on interactive adding which also demonstrates the patch feature of git add.

like image 41
Pat Notz Avatar answered Oct 08 '22 19:10

Pat Notz