What is git?
Git is nothing but a version control system like svn which is used to track our local changes. Since git is decentralized unlike svn and is also open source, we don't need an internet connection to track and update all our changes (through commits).
If we want to share all our changes with our team, we update all our commits to a centralizer repository like Github or Bitbucket.
Important features in Git
Branching: Git allows users to create branches and work on their features independently so that the main branch is not affected and each team member can work on their own branch.
Merging: Once each team member is done with working in their own branch or they have completed the feature, they can merge the code into main branch to complete the flow of the code.
Collaboration: We can share all these changes in Github so that the team can view all the code and share their code as well.
Pull requests: A pull request is basically a request to merge the branch code into main branch. It is approved when there are no merge conflicts.
Common commands used git
git init: Initialize a new Git repository
git clone: Clone an existing Git repository
git add: Stage changes for commit
git commit: Save changes to the local repository
git push: Push changes to a remote repository
git pull: Both Fetch and merge happens from a remote repository
git branch: Manage branches and create new branch.
git fetch: Fetch all branches from Git remote
git merge: Merge changes from one branch to another
Many of us know how to use these commands in the command prompt or terminal.
But all these git commands can be done using Eclipse IDE as well which is way more easy to understand and execute.
Let us see how to do this eclipse with an example.
In your eclipse, go to Help-> Install software.
In the opened window, search for "egit" and select the option displayed as shown in the below picture.
Select all the checkboxes under Git Integration for Eclipse and Click Finish
Now We've installed egit for eclipse, Next we have to configure our user details, so that when we do a commit, our details will be shown in the github.
Mac : Click Eclipse --> settings --> search "Git" --> click Configuration
Windows :Window --> Preferences --> search "Git" --> Click Configuration
Configuring a new repository in Git
Once, we've added the email and name, lets see how to add a new git project.
Lets Create a sample, maven project by clicking the Create a New maven project and giving a Group id and artifact id like below and click Finish.
Next Right click on Project Name, Select Team --> Share Project
Choose the Repository location by clicking the Create button. Click the checkbox near the project name and click Finish.
Now the new project has been configured in Git.
Cloning an existing repository from Github
We can also clone a repository from github, by doing the following steps.
Click File --> Import
Choose Git --> Projects from Git --> Next
Now click the Clone URI link and click Next
Now place the repository path from github and paste it in the URI field and click Next
Now Choose all the branches that you want and the Local Destination folder and Click Next
Click Import as general Project and Click Finish
Adding Commits, Branches and Resolving Merge Conflicts:
Commit:
To add a new Commit, Right click Project --> Team --> Commit
Now add the commit message and click commit and push
Creating a branch:
To create a new branch, Right click project --> Team --> Switch To --> New Branch
Give a new branch name and click Finish
Resolving Merge conflicts in eclipse:
Resolving merge conflicts is easy with egit since eclipse provides a merge tool where we can see what the conflicts and it makes it easy to resolve them.
Go to Right Click --> Team --> Merge
Now choose which branch you want to merge with. Here I've chosen the latest Master branch from the remote repository and click Merge
If there are any conflicts, a window will be displayed saying there are conflicts and also the file that has conflicts, a red symbol is displayed near the file in the project explorer. Also the file also shows the HEAD and refs in the file.
Eclipse (egit) has a built in tool called Merge tool te help us resolve these conflicts.
Now we can resolve the conflicts by changing the code and saving the file.
After making changes to resolve the conflicts, We need to commit the file again by navigating to
Team --> Commit and push. Now we have successfully resolved all the merge conflicts using egit.
Conclusion
We have learnt how to use EGit plugin in eclipse without using command prompt or terminal to execute git commands and track all code changes in Github.