Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: commit partial changes

Tags:

There are multiple questions on Stack Overflow addressing the problem of staging and committing only parts from files. However, I can't make it work.

  • How can I commit only part of a file in git

Let's say we want to implement a dummy math class in php (the language doesn't matter) with some basic methods like: add, subtract, multiply and divide.

Let's start with the class definition:

<?php     class Math {     } ?> 

Now:

$ git add math.php $ git commit -m "Create Math class." 

In the next step we implement both add and subtract four methods:

<?php     class Math {         public function add($a, $b) {             return $a + $b;         }          public function subtract($a, $b) {             return $a - $b;         }     } ?> 

But now we want to commit the implementation of the add and the subtract methods in separate commits.

Is this possible?

What I tried

$ git add -p

The following appears:

enter image description here

I would love to split the hunk into smaller hunks, so I press s, and the following appears:

enter image description here

It seems that git didn't split it into smaller hunks.

Let's try now to manually edit the current hunk. So I press: e.

My default text editor (sublime text) opens up, and here I can edit the hunk:

enter image description here

I try to simply delete line 8,9,10,11 - because I want to stage only the add function. I save, close the editor, but git says:

Your edited hunk does not apply. Edit again (saying "no" discards!) [y/n]?

I'm really new to git add -p and interactive staging, so maybe it's something I do wrong, or simply it's not possible what I want, and really hope some more experienced git users have some instructions maybe also a solution for me.

Thank you!

like image 380
Tamás Pap Avatar asked Apr 21 '13 18:04

Tamás Pap


People also ask

What is partial commit in git?

For those who use Git Extensions: In the Commit window, select the file you want to partially commit, then select the text you want to commit in the right pane, then right-click on the selection and choose 'Stage selected lines' from the context menu.

Can I commit only staged changes?

Since you only need to commit the staged changes to a file, you can just stash, keeping the indexed changes intact, and committing the file after that.

Is it possible to stage only some part of file git?

It's also possible for Git to stage certain parts of files and not the rest. For example, if you make two changes to your simplegit. rb file and want to stage one of them and not the other, doing so is very easy in Git. From the same interactive prompt explained in the previous section, type p or 5 (for patch).


1 Answers

You are doing exactly what I've done in the past, and I even recreated your entire experiment on a UNIX machine and it worked just as expected.

I suspect your editor is changing the line endings in the diff hunk when it saves.

like image 141
Ben Jackson Avatar answered Sep 29 '22 11:09

Ben Jackson