Saturday, May 30, 2015

Git with Eclipse (3) - basic use cases in EGit with local repository

This post introduces indexing files (stage), committing files and reverting changes in local repository with EGit. Here assume the project has already been shared to Git local repository. For how to share a project to Git local repository, please read Git with Eclipse - creating local repository with EGit in Eclipse. All screenshots below are from Eclipse Kepler.

Adding Files to staging area

Staging area is a new concept introduced by Git. Git has three main states of a file: modified, staged and committed. In a centralized version control system, there are only two states which are modified and committed. In Git, staged means that your file is marked in its current version to go into your next commit snapshot. It is useful when you have a major change in your project like adding a new feature. You cannot finish the work in one goal and you also cannot commit half completed work to the repository. In Git, you can stage your completed changes and continue your work. When you finish the whole implementation of the new feature, you can commit your work and all staged files will be committed in one snapshot. The following diagram shows the relationship of the three states.



To add a file into staging area in EGit, right click on the file and select Team -> Add to Index.


Once the file is added into staging area, a plus symbol will appear on the file and all parent folders of the file will have an asterisk symbol indicating it is staged.


EGit also allows to commit files not staged. In this case, those files will be staged and committed at the same time.

Committing Files to local repository 

Similar to centralized version control system, commit is straightforward. Right click on the project and select Team -> Commit... It will open a new dialog. In the upper textbox, you will have to enter a commit message. In the lower part of the dialog, you can select the files you want to commit. All staged files of the project are selected by default and you can also select staged files from other projects. In my example, the staged file from Project1 is selected by default and I can also select the staged file from Project2. Furthermore, if you click on the Show Untracked Files icon, those files not staged will also be displayed. And you are allowed to select those files. Click Commit button and all selected files will be committed to the local repository.


In Git, You can change your previous commit if you realize your previous commit is incomplete or your commit message needs modification. This is different from centralized version control system and it is a very convenient function to correct mistaken commits. To amend previous commit, click this icon on the right top of the commit message textbox. It will not add a new version to your committed files and your comment message in last commit will be overridden. However, this should only be used if the previous commit has not been pushed to a remote repository.

Reverting Changes 

There are several ways to revert changes in Git. Revert the changes in files or revert the changes in project.

Reverting changes in files

If you want to revert changes in one or more files, select the files and right click to popup the context menu. Select Replace With in the context menu, EGit provides several options to revert. Four of them relate to the local repository, HEAD Revision, Git Index, Previous Revision and Branch, Tag, or Reference...


HEAD Revision: This will replace the contents of the file with the HEAD revision. Any untracked changes and staged changes will be lost.
Git Index: This will replace the contents of the file with the contents in the staging area. Any untracked changes will be lost.
Previous Revision: This will replace the contents of the file with previous revision.
Branch, Tag, or Reference...: This enables you to revert your file to any branch, tag or reference revision.

Furthermore, you can use the Compare With to compare your files with any revision and revert changes line by line.

Reverting changes in project

If you want to revert changes in the whole project, right click on the project and select Team ->Reset... in the context menu. The Reset dialog will be popup. In the upper box, you can select any revision you want to reset to. In the bottom Reset type box, there are three types.

Soft (HEAD updated): This will only reset your current HEAD revision to the revision you select in the upper box. If the revision you selected is the same as your current HEAD revision, you will notice there is no change after reset. The untracked changes and staged changes will be retained.

Mixed (HEAD and index updated): This will do a Soft reset and reset the staging area. After Mixed reset, you will notice all your staged files become untracked. You can imagine as all the changes are removed out of staging area, but they still stay in your working directory.

Hard (HEAD, index, and working directory updated): This will do a Mixed reset and reset your working directory. After Hard reset, your working directory will also be reverted to the revision you selected, which means all uncommitted changes will be lost. You should be careful with Hard reset because you cannot bring your changes back.



There are other use cases of EGit in local repository. For a full guide of EGit usage, please refer to https://wiki.eclipse.org/EGit/User_Guide. For how to interact with remote Git repository in EGit, please read Git with Eclipse (4) - pushing local repositories to remote repositories.


Reference:

No comments:

Post a Comment