Interactive add with Git GUI

The interactive features of git add are really powerful in order to create clean commits that only contain a single logical change even though it was coded as a mix of feature adding and bug fixing. The downside of the interactive git add feature is that it is hard to get an overview of all the changes that exist in the file when only being showed one hunk at a time. To get a better overview of the changes and still be able to only add selected hunks (and even single lines), we can use git gui. Git GUI is normally distributed with the Git installation (MsysGit on Windows) and can be launched from the command line: git gui. If your distribution doesn't have Git GUI available, you can probably install it from the package manager named git-gui.

Getting ready

We'll use the same repository as in the last example and reset it to the same state so we can perform the same adds with Git GUI:

$ git clone https://github.com/dvaske/cookbook-tips-tricks.git
$ cd cookbook-tips-tricks
$ git checkout interactive
$ git reset HEAD^

How to do it...

Load up Git GUI in the cookbook-tips-tricks repository. Here, you can see the unstaged changes (files) in the top-left side and the staged changes (files) under it. The main window will display the unstaged changes in the current marked file. You can right-click on a hunk and see a context menu with options for staging and so on. The first hunk shown by Git GUI is much larger than what we saw before with git add -p. Choose Show Less Context to split the hunk, as shown in the following screenshot:

How to do it...

Now, we get a smaller hunk like before, as shown in the following screenshot:

How to do it...

The first hunk we just choose to add, Stage Hunk For Commit, and the next hunk moves to the top of the screen:

How to do it...

Here, we can select the lines we want to add, instead of performing another split, and stage those lines: Stage Lines For Commit. We can add the rest of the hunks except the one with the debug line. Now, we are ready to create a commit and we can do so from the Git GUI. We can just write the commit message in the field at the bottom of the screen and hit Commit:

How to do it...
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset