As a fresh Git user, I had been using only basic functionalities. In time I grew stronger and discovered new, useful options. I’d known about hunks for a long time but I started to use them relatively recently. Hunks seem to be quite small functionality, but sometimes they can be tricky. Especially if Git sees them not as you do.
Problem description
I have XmlDocumentation.xml file with nodes like on the picture below:
documents element contains few blocks – first, comment block, then four document blocks. I would like to comment out all four document blocks and add one new below them. Just like that:
As we can see, the first block stayed untouched, next four blocks are commented and the new document block is added. I’d like now split this change to two commits. First would contain markers commenting four blocks, second – newly added document. What if I just stage this file in SmartGit?
Two hunks are marked. First contains only „<!–” marker, second contains „</document>–>” and new document together. It’s not in align with my plans. What to do?
Editing hunks in the command line
Git orthodox would do it like a pro. One command is enough. How does this process look like in SmartGit?
Right mouse click on the repository, „Open Git-Shell” option, Git-Shell window opens. Enter command:
git add . --patch
And press „e” to edit the hunk. A new window appears where you can edit it. It’s not an easy task so I advise to test it on some simple example first, before struggling with something more complicated. Save the work when finished and the hunk will be staged. You can proceed to the next one.
Editing hunks in SmartGit
Doing the same in SmartGit isn’t much easier, but first – one doesn’t need to write commands 🙂 and second – hunk editor seems to be easier to understand and use.
When the change is ready to be committed, stage the file from which hunks should be edited. Then right-click on it and select Index Editor option. Optionally press Ctrl+Alt+T.
Now a new window appears. This editor contains three columns – Repository (HEAD), Index and Working Tree. Only two first are interesting for our purposes. Index tab contains hunks that we want to split. Repository tab already committed code.
Now, all we need to do is to remove unwanted code from the middle column. This seems a bit misleading because removed code is marked as removed from Working Tree column – thus, one may have the impression that this code will be lost. It’s not true – just remove unwanted part of the hunk and save the work.
Now you can commit staged work (remember to check this option in commit window).
Newly created commit contains all I wanted – just comment markers. Working tree contains the rest of the changes – added document node. All that’s left is to create the next commit with the node only.
I think that the described approach is maybe not very „pro”, but it’s very easy for all people that just use Git, not worship it (like me). I hope it helped you too. If so, check also splitting files article.