1. Overview
1.概述
In this article, we’re going to explore EGit – an evolution of the JGit library for Eclipse.
在这篇文章中,我们将探讨EGit–用于Eclipse的JGit库的演变。
2. EGit Setup
2.EGit的设置
During the article, we’ll use the following tools:
在这篇文章中,我们将使用以下工具。
- Eclipse Neon.3 version 4.6.3
- EGit plugin version 4.8
2.1. Installing EGit in Eclipse
2.1.在Eclipse中安装EGit
Starting with Eclipse Juno, EGit is included with the Eclipse itself.
从Eclipse Juno开始,EGit被包含在Eclipse本身中。
For older versions of Eclipse, we can install the plugin through Help -> Install New Software and providing the URL http://download.eclipse.org/egit/updates:
对于旧版本的Eclipse,我们可以通过Help -> Install New Software并提供URL http://download.eclipse.org/egit/updates来安装该插件。
2.2. Identifying a Commiter
2.2.识别承诺人
Git needs to keep track of the user behind a commit, therefore we should provide our identity when we make commits via EGit.
Git需要跟踪提交背后的用户,因此我们在通过EGit进行提交时应该提供我们的身份。
This is done through Preferences -> Team -> Git -> Configuration and clicking on Add Entry to include information for user.name and user.email:
这是通过Preferences -> Team -> Git -> Configuration并点击Add Entry来完成的,包括user.name和user.email的信息。
a>
3. Repositories
3.存储库
3.1. Repositories View
3.1.存储库视图
EGit comes with the Repositories view that allows us to:
EGit自带的Repositories视图让我们可以。
- Browse our local repository
- Add and initialize local repositories
- Remove repositories
- Clone remote repositories
- Check out projects
- Manage branches
To open the Repositories view, click Window -> Show View -> Other -> Git -> Git Repositories:
要打开Repositories视图,点击Window -> Show View -> Other -> Git -> Git Repositories:。
3.2. Creating a New Repository
3.2.创建一个新的存储库
We need to create a project and right-click on it to choose Team -> Share Project, and Create.
我们需要创建一个项目,并在其上点击右键,选择Team -> Share Project, 和Create。
From here, we select the repository directory and click Finish:
从这里,我们选择存储库目录,然后点击完成:。
3.3. Cloning a Repository
3.3.克隆一个存储库
We can clone a repository from a remote git server to our local file system.
我们可以从远程git服务器上克隆一个版本库到我们的本地文件系统。
Let’s go to File -> Import… -> Git -> Projects from Git -> Next -> Clone URI -> Next, then the following window will be shown:
让我们进入文件 -> 导入… -> Git -> 来自Git的项目 -> 下一步 -> 克隆URI -> 下一步,然后将显示下面的窗口。
We can also open the same window from the Clone Remote Repository toolbar button in the Repositories view tab.
我们也可以通过Repositories视图标签中的Clone Remote Repository工具栏按钮打开同样的窗口。
Git supports several protocols such as https, ssh, git, and etc. If we paste the remote repository’s URI, other entries will be filled automatically.
Git 支持多种协议,如 https、ssh、git 等。如果我们贴上远程仓库的URI,其他条目就会自动填充。
4. Branches
4.分支
There are two types of branches we’ll deal with:
我们将处理两种类型的分支。
- Local branch
- Remote tracking branch
4.1. Creating Local Branch
4.1.创建本地分支
We can create a new local branch by clicking Team -> Repository -> Switch to -> New Branch:
我们可以通过点击Team -> Repository -> Switch to -> New Branch:创建一个新的本地分支。
We can choose the remote tracking branch from which to base our local branch. Adding upstream configuration to our new local branches will simplify synchronizing the local changes with the remote ones.
我们可以选择远程跟踪分支,并以此作为我们本地分支的基础。将上游配置添加到我们新的本地分支,将简化本地变化与远程变化的同步。
It’s recommended to check the option in the dialog Configure upstream for push and pull.
建议在为推拉配置上游选项的对话框中勾选该选项。
Another method to open the new branch dialog by right-clicking on branches in the Repositories view -> Switch To -> New Branch
另一种方法是在Repositories视图中右击分支,打开新分支对话框-> 切换到-> 新分支。
4.2. Checking out the Branch
4.2.检查分支机构
From the Repositories view, right click on the branch name and click Check Out:
在Repositories视图中,右击分支名称并点击Check Out。
Or right-click on the project and select Team -> Switch To -> select the branch name:
或者在项目上点击右键,选择团队-> 切换到-> 选择分支名称。
5. Tracking Files With Git
5.用Git跟踪文件
5.1. Tracking Changes
5.1.追踪变化
Question mark signs appear on files which are not yet under Git’s control. We can track these new files by right-clicking on them and selecting Team -> Add to Index.
问号标志出现在尚未被 Git 控制的文件上。我们可以通过右击这些新文件并选择Team -> Add to Index来追踪它们。
From here the decorator should change to (+) sign.
从这里开始,装饰器应改为(+)号。
5.2. Committing Changes
5.2.提交更改
We want to commit changes to a tracked files. This is done by right-clicking on these files and choosing Team -> Commit:
我们要提交对已跟踪文件的修改。这可以通过右击这些文件并选择Team -> Commit:来实现。
By default, the author and committer are taken from the .gitconfig file in our home directory.
默认情况下,作者和提交者来自我们主目录下的.gitconfig文件。
We can enter a commit message to explain the changes. In addition, by clicking on Add Signed-off-by icon in the top right corner, we can add a Signed-off-by tag.
我们可以输入一条提交信息来解释这些变化。此外,通过点击右上角的Add Signed-off-by图标,我们可以添加一个Signed-off-by标签。
5.3. Inspecting History
5.3.检查历史
We can check the history of a file by right-clicking on it and choosing Team -> Show in History.
我们可以通过右键单击文件并选择团队->在历史中显示.来检查文件的历史。
A history dialog will show all the committed changes of the inspected file:
一个历史对话框将显示被检查文件的所有已提交的修改。
We can open the last committed changes in the compare view by clicking on the compare mode icon in the top right corner in the history tab and then double-clicking on the file name (here’s an example: HelloEgit/src/HelloEgitClass.java) in file list:
我们可以通过点击历史标签中右上角的比较模式图标,然后双击文件列表中的文件名(这里有一个例子:HelloEgit/src/HelloEgitClass.java)来打开比较视图中最后提交的修改。
5.4. Pushing Changes to the Remote Repository
5.4.推送修改到远程仓库
To push our changes we need to have a remote Git repository.
为了推送我们的变化,我们需要有一个远程Git仓库。
From Team -> Remote -> Push we can enter the https URL of the new Git remote repository in a wizard:
从Team -> Remote -> Push我们可以在向导中输入新的Git远程仓库的https URL。
Next steps are to:
下一步是要。
- Choose the Add All Branches Spec to map local branch names to the same branch names in the destination repository
- Push the confirmation button – the wizard will show a preview of changed files
- Finally, we click Finish to push our repository to the remote location.
If we have set up the Upstream Configuration from section 4.1, this configuration dialog will not be shown and the push will be much easier.
如果我们已经在第4.1节中设置了上游配置,这个配置对话框就不会显示,推送就会更容易。
5.5. Fetching from Upstream
5.5.从上游获取
If we are working with a local branch that is based on a remote tracking branch, we can now fetch changes from the upstream.
如果我们正在处理一个基于远程跟踪分支的本地分支,我们现在可以从上游获取变化。
In order to fetch from the upstream, we right click on the project and select Team -> Fetch from Upstream (or by right-clicking on the repository on the Repositories View and selecting Fetch from Upstream).
为了从上游获取,我们右击项目,选择团队-> 从上游获取(或者在存储库视图上右击存储库,选择从上游获取)。
This fetch can be configured by right-clicking on the project and selecting Team -> Remote -> Configure Fetch from Upstream:
可以通过右击项目并选择团队 -> 远程 -> 配置从上游获取:来配置这种获取。
5.6. Comparing & Synchronizing
5.6.比较& 同步
If we want to see the changes between the local working directory and a committed change, we can right click on the resource and choose Compare With. This opens the Synchronize View to allow us to browse the changes:
如果我们想看看本地工作目录和已提交的更改之间的变化,我们可以右击资源并选择Compare With。这将打开Synchronize View,让我们浏览变化。
By double-clicking on the changed file, the compare editor will be opened, allowing us to compare the changes.
通过双击已更改的文件,比较编辑器将被打开,使我们能够比较这些更改。
If we want to compare two commits, we need to select Team -> Show in History.
如果我们想比较两个提交,我们需要选择Team -> Show in History.。
From the history view, we’ll highlight the two commits that we want to compare and select the Compare with Each Other option:
在历史视图中,我们将突出显示我们想要比较的两个提交,并选择相互比较选项。
If we want to compare between the working directory and a branch, we can use Team -> Synchronize
如果我们想在工作目录和一个分支之间进行比较,我们可以使用Team -> Synchronize。
5.7. Merging
5.7.合并[/strong]
Merging incorporates changes from one branch or tag into the currently checked out branch.
合并将一个分支或标签的修改纳入到当前签出的分支中。
We can merge by clicking Team -> Merge or by right-clicking on the repository name in the repositories view and select Merge:
我们可以通过点击Team -> Merge或者在版本库视图中右击版本库名称,选择Merge来进行合并。
Now we can select the branch or tag that we want to merge with the currently checked out branch.
现在我们可以选择要与当前签出的分支合并的分支或标签。
6. Conclusion
6.结论
In this tutorial, we introduced the EGit plugin for eclipse, how to install and configure it and how to use in our daily development.
在本教程中,我们介绍了eclipse的EGit插件,如何安装和配置它以及如何在日常开发中使用。
For more details on EGit, check out its official documentation here.
关于EGit的更多细节,请查看其官方文档这里。