Git is a very powerful and popular version control tool. As a developer, I can not imagine my programming life without it because I always need to save my code, compare with older versions, and finally share it. Git is a perfect tool for all these tasks and more.
This post isn’t about using Git because there are a lot of books, articles, and tutorials covering all aspects of Git. Instead, I want to show how Git works in Eclipse Che. A developer can use Che from the command line (since Che includes a terminal), but in this post, I’ll explain how to perform the most common Git operations without any console commands but instead using interactive menus.
There are two ways to start work with Git:
Importing a project from GitHub.
Initializing Git in an existing project.
The easiest way is to clone your project from GitHub. To clone a repo you need an HTTPS or SSH link to your project. If you want to use SSH, you need to generate an SSH key. In the Che menu, select Profile -> Preferences -> SSH -> VCS. For GitHub, just click the octocat button and Che will do the oAuth magic. For other Git providers, use the generate key or upload key buttons.
Then go to Workspace -> Import project and choose Git. Paste your link in the URL field:
Keep following directory means that only the specified directory will be cloned. If a Git repository contains a lot of submodules but you want to work only with one, select keep following directory and input the path of the single directory you want in the Directory field. You can also indicate a specific branch to clone from.
If you’re using GitHub, you can further simplify things by authorizing Che to view the repos associated with your account. Instead of selecting Git in the left-hand list, choose GitHub. Then click the Load Repo button:
Regardless of whether you choose Git or GitHub, clicking the import button will clone the project into a new Che workspace and initialize Git.
If you have a local project that is not stored in a remote repository you can initialize Git for it from the Menu: Git -> Initialize Repository.
Once you've made any changes, you should commit them to the repo. To do this, Git requires adding changes to an index before committing them. To add something to index, you should select a file or a folder in project explorer that you want to add to the index, then click Git -> Add To Index. A confirmation dialog will appear. If you’d like, you can click Git -> Status to make sure that your changes have been added to Git index.
Having added changes to index it is time to do a commit: Click Git -> Commit. A dialog box will provide some options and give you the opportunity to add a commit message.
Everything is pretty straightforward. Just enter a commit message and commit your changes. If you think you have made a mistake, you can always correct it by resetting a single, or even multiple, commits at Git -> Reset.
Remember that all chosen commits will be removed. Make sure you choose the right reset mode. See: Git reset command documentation.
There is one Git thing that I absolutely love in Che: visual side-by-side code comparison. This is something that a console Git can’t offer. Sure, you can view diffs but with a lot of changes, it becomes unreadable.
There are three options when viewing changes:
With a current HEAD.
With HEAD of the selected branch.
With the selected commit (only possible for one file).
To open a Git comparison window, select the folder or the file you want to compare and click Git -> Compare in Git menu or right-click context menu. If there are changes in multiple files, you will see a window with the list of changed files:
If there are too many files to view you can switch to a tree mode view by clicking Group by directory.
Select the file you want to compare, double click it or click Compare to call a comparison window:
There are many other Git operations in Che that this post does not cover such as push, pull to remote, manage remotes, checkout to branch, merge with other branch, but those actions are quite simple and intuitive even for a first time Che user.