Introduction
Here is the part-2 of Git tutorial. In part-1 i have explained creating local repository, Branching, committing and pushing your code to your local repository. Here is the continuation of part-1 Merging through pull request with no conflicts, updating your code in remote repository and local system and working with third party contributors.
Merging
Normally, a commit is something that you create, typically by editing files in the working tree, adding them to the index, and then saying git commit. But when you ask it to merge one branch into another, Git itself is generating a commit — the merge commit. Fine, but I have been completely uninformative about how Git does that. I have waved my hands at the matter, merely saying that Git proceeds by “combining the contributions from both branches” to form the merge commit. What on earth do those words actually mean?
To create a merge commit, Git undergoes a standard internal thought process that I call merge logic. The nitty-gritty details can get very nitty and gritty indeed, but in broad outline, here’s what Git does:
A merge starts with Git locating the common commit from which the merging branches most recently diverged. More technically, this is the first commit reachable from both branches. This commit is called the merge base.
Git then calculates two diffs — from the merge base to the first branch, and from the merge base to the second branch.
To form the merge commit, Git applies both diffs to the merge base.
For example, if you are about to merge, and you would like to know what the merge base would be, you can ask Git! That’s what the git merge-base command is for. If you’re about to merge other branch into master, say git merge-base other branch master and you will be told the identifier of the commit where Git thinks these branches diverged.
Similarly, if you are about to merge, and you’d like to see the two diffs that would be applied to the merge base, you can ask Git! If you are on master and you are about to merge other branch into it, say git diff other branch... and git diff ...other branch to see the master diff and the other branch diff, respectively.
Still, that sort of information is perhaps better suited to a machine than to a human being. What you’d really like to do, I expect, is to see the merge enacted directly on your working tree, and compare that to how things were before you performed the merge.To do so, you can give the merge command, but add the --no-commit flag to it. Git responds by configuring your working tree and your index as if it were about to make the merge commit, but it doesn’t actually make the commit. Instead, Git pauses in the middle of the merge operation so that you can inspect the merged files in the working tree.
Since no commit was actually performed, you can now compare the “merged” state of the working tree with its previous state by saying git diff HEAD. When you’re done thinking about the situation, you should probably either complete the merge with git commit or else reverse course, aborting the merge with git merge --abort (which also resets everything back to HEAD).
Before Merging you need to push your code to your local repository.
Here are the steps to be followed to commit and push your code to your local repository
Commit Your Changes
Once you are done with your code you are ready to commit your code by following these steps.
Once you click the commit option You will be getting Git staging window showing the files that user need to add to the commit. If the user want to commit one particular file then need to add that particular file to Staged Changes, rest of the files will be displayed in Unstaged Changes. Once you are done with your changes, move on to Commit Message, enter message and then click Commit.
2. Once user committed the code then again right click your project goto->team->Push Branch 'master'. By clicking this option, your code will be ready to push to your local repository.
3. After selecting Push, user will be redirected to next step. Check repository name and branch name, then click Push option.
yahoo!Now your code is pushed to your local repository. Click close to finish.
Creating a Pull Request
1. Merging can not be done without doing pull request to the remote repository. Once commit and push completed, move on to your local repository and check if the code is pushed or not. If the code pushed then you will be getting the below message as 1 commit ahead.
2. By clicking that you will be entered to this below page. There you can see from which repository to which repository your commit is going to merge.U can also check the files you made changes locally in your local system.Finally, click Create pull request option to create a request to merge your commit.
3. It will be redirected to confirmation page, there user need to enter message (it will be automatically displayed with default message if not give commit message) and then click on Create pull request.
Once you made a pull request it will be notified in the main user repository as shown below. Here the request is shown under repo name as Pull requests 1. Click on Pull requests to go check where files and commit changes are displayed.
User 1 (Remote repository) can check and review the files under the pull request.Here its showing the latest commit with commit message which has given earlier while doing commit by user2.If the files are correct and there is no conflict then it will be automatically ask the user1 to merge the request.
By clicking Merge pull request, it will go to the next step to confirm merge.Now, click Confirm merge to merge the code.
If the code is merged without any conflict then your repository will updated with the latest merge as shown below.
These changes are made only in the repository not in the local system. Again User1 has to do pull option to pull the latest changes to local system.Merging is easy only there is no conflicts in your code.If two users modified the same code then there will be conflicts in merging code. In that situation user need to resolve the conflicts manually and merge the code.This is how git works and project can be merged.
Updating your local system
After merge is done, now it is time to update the code in your local system also. The changes are in git repo only. The user1 has to pull the code from their master branch to their local project location. For that the user1 has to do below steps to update their project with all latest changes. Right click your project which you are working under git hub go to Team->Pull.
To complete the pull operation click Finish.
Working with third-party contributors
You can also work with other users in your repository by inviting them in collaborators. For that you need go to your respective repository settings->click on Collaborators and then click Add people.
You will be directed to this below page.You can add names if they are already worked in your repository or enter email id of other users and invite them to your repository.
CODE EXECUTION THROUGH JENKINS.
Here are the quick steps to run your code in jenkins through git link. Log in to jenkins account, click new item. Enter the details of project name, project type and then click ok.
It will direct you to configure the project, enter the project description.
Enter project description and scroll down to give more details.
Below the page click on git, there you will need to give your git repository link and your branch name.
Once Git connection done, scroll down, enter .xml file path from your project.For that you need to go eclipse project->right click pom.xml->go to properties-> copy the path and paste in root pom option and enter clean test in goals and options.
Once configurations are done, go to your project select Build Now to run the project. To check the console output right click on build and select console output.
There you can see the console output with your project execution status.
Conclusion
These are the steps to be followed to run a code through jenkins. Though it takes time to understand the process initially it makes your job easier.
Hope you like this blog and got some knowledge on git and jenkins.
Here's a attempt to my blog without directly copying and pasting it from other source. Keep these things in mind as you move forward. I am giving Git tutorial part-1 link below, go through both the blogs. Hope you find it useful.
Part-1: