Git branches
Virtual directories get created for each branch. main/master branch is considered as the branch which has the latest and greatest code. We create branches so each developer can work on its own branch and once the development is finished on a particular stage then it can be reviewed by the code owner and after that, it gets merged into the master branch.
By Default, the master branch gets created when you have your first commit.
Step 1:- List the branches of your local repository. (The branch which has * symbol in front of it is the current branch where we are working).
git branch
I have only one branch which is master branch and it is my current branch
*master
Step 2:- List all the commits of your master branch
git log --oneline
Step3:- Create a branch for senior developer and name it sdev1.
git branch sdev1
Step 4:- Create one more branch for junior developer and name it jdev1
git branch jdev1
Step 5:- List all the branches
git branch
Step 6:- Switch to sdev1 branch and it should be your current.
git checkout sdev1
git branch
Step 7:- List all the files and commits of sdev1 branch. It should have all the commits and files which are present in master branch.
ls
git log --oneline
Step 8:- Create a new file sdev1.java and commit it in sdev1 branch. It should have a new commit id once commit is done successfully.
touch sdev1.java
git add sdev1.java
git commit -m "sdev1.java file is added in sdev1 branch"
git log --oneline # it should have a new commit
ls # it show the sdev1.java file
Step 9:- Verify that master branch does not have the commit details which is for sdev1.java file and also sdev1.java file should exist in master branch.
git checkout master
git log --oneline # it should not show the sdev1.java commit id
ls # sdev1.java file should not be there.
Step 10:- sdev1 wants that his/her code is to be merged into master branch.After merging commit id for sdev1.java addition should be reflected in git log and also sdev1.java file should exist in master branch
git merge sdev1
git log --oneline # it should show the sdev1.java commit id
ls # sdev1.java file should be in master branch
Step 11:- Switch to jdev1 branch and commit a new file jdev1.java.
git checkout jdev1
touch jdev1.java
git add jdev1.java
git commit -m "jdev1.java file is added"
git log --oneline
Step 12:- Merge jdev1 code into master branch.
git checkout master
git merge jdev1
Step 13:- Update sdev1 and jdev1 branch with latest code (master branch)
git checkout sdev1
git merge master
ls
git checkout jdev1
git merge master
ls
Step 14:- jdev is reporting to sdev and all the code changes by jdev is first reviewed by sdev and they merge into master branch and later jdev1 branch get the lastet code from master branch.
git checkout jdev1
touch test.java
git add test.java
git commit -m "test.java file is added"
# To merge with sdev1 branch
git checkout sdev1
git merge jdev1
# To merge with master branch
git checkout master
git merge sdev1
Now all the branches are in sync.
Merge Conflict
Conflicts generally arise when two people have changed the same lines in a file, or if one developer deleted a file while another developer was modifying it. In these cases, Git cannot automatically determine what is correct.
Conflicts only affect the developer conducting the merge, the rest of the team is unaware of the conflict. Git will mark the file as being conflicted and halt the merging process. It is then the developers' responsibility to resolve the conflict.
Step 1:- Let's create a new branch jdev2 and he is reporting to sdev1
git checkout sdev1
git branch jdev2
Step 2:- sdev1 has asked both junior developers (jdev1 and jdev2) to work on different modules of test.java file and once they finish their work then that code will be reviewed and merge in sdev1 branch and then master branch.
git checkout jdev1
echo "This code is added by jdev1" >> test.java
git add test.java
git commit -m " test.java file is modified by jdev1"
git checkout jdev2
echo "This code is added by jdev2" >> test.java
git add test.java
git commit -m " test.java file is modified by jdev2"
Step3:- Merge jdev1 and jdev2 code changes into sdev1 branch if there is any conflict occurs then resolve that conflict.
git checkout sdev1
git merge jdev1
git merge jdev2 # Merge conflict
Step4:- Resolve Merge conflict
git mergetool
update the changes # test.java.orig will be auto-created (make sure to delete)
cat test.java
rm test.java.orig
git commit -m "final test.java file"
Step5:- Update all the branches (jdev1, jdev2 then master) after approval from sdev1
# to merge with jdev1
git checkout jdev1
git merge sdev1
cat test.java
# to merge with jdev2
git checkout jdev2
git merge sdev1
cat test.java
# to merge with master
git checkout master
git merge sdev1
cat test.java
Now all brances are in sync.