Git Partial Staging in Eclipse

June 3, 2014 | 2 min Read

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:

Let’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.

However, 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.

In 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!

Ian Bull

Ian Bull

Ian is an Eclipse committer and EclipseSource Distinguished Engineer with a passion for developer productivity.

He leads the J2V8 project and has served on several …