Git Partial Staging in Eclipse

When committing changes to your  source control system, it’s considered good practice to only resolve a single task with each commit. There are several advantages to this approach:

  • The history reads like a list of tasks that were accomplished
  • Each commit can be easily reviewed, as it only does one thing
  • Commits can be easily reverted or cherry-picked

However, when working with source code, it’s temping to sneak-in another small change into your current commit. Take the following example:

Screen Shot 2014 06 02 at 10.57.06 PM Git Partial Staging in EclipseLet’s assume we want to implement a more accurate method for computing pi. It might also be temping to fix the typo in the computePie() method name.

Screen Shot 2014 06 02 at 10.57.36 PM Git Partial Staging in EclipseHowever, these are two independent tasks. If the implementation of the computePi method was reverted (or did not pass code review), I would still expect the typo to be corrected. While I could stash the work I’m doing, fix the typo, retrieve the stash and continue, the overhead of that workflow would likely deter me. With Eclipse Git (EGit) there is a much easier way.

From the staging view, double-click on the file. A compare dialog will appear. On the left are your current changes; on the right is the currently staged work. You can now copy changes from the left to the right, effectively staging parts of your file. In this case I will stage the correction to the method name.

Screen Shot 2014 06 02 at 11.11.26 PM Git Partial Staging in EclipseIn a second commit I will change the implementation of the method that computes Pi. Now these two change-sets can be pushed, reviewed, cherry-picked, tested and reverted, separately!

Screen Shot 2014 06 02 at 11.23.29 PM Git Partial Staging in Eclipse

You may also like...

Share this Post

Twitter11
Google+4
LinkedIn
Facebook

Tags

2 Responses to “Git Partial Staging in Eclipse”

  1. Bananeweizen says:

    Nice trick, Ian. Do you know if your suggested workflow will still play nicely with gerrit as the push target? Typically I avoid multiple dependent commits, and instead switch local branches (or reset) in between such commit series. In the past we often got into trouble when intermediate commits of such series had to be reworked (where gerrit then complains about outdated dependencies).

  2. Ian Bull says:

    @Bananeweizen
    Yes, you can do a reset –mixed between commits. This will base both commits off the same parent.

2 responses so far

Written by . Published in Categories: EclipseSource News, Planet Eclipse

Author:
Published:
Jun 3rd, 2014
Follow:

Twitter LinkedIn Google+ GitHub