Git branches Virtual directories get created for each branch. main/master branch is considered as the branch which has the latest and g...
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 at a particular stage then it can be reviewed by the code owner and after that, it gets merged into the master/main branch.
By default, the master branch gets created when you have your first commit.
Use Case
In the below example we are creating branches for a senior developers (sdev) and junior developers(jdev). Junior developers after a period of time cannot merge their code changes into a master branch directly as they are reporting to the senior developers so it is the responsibility of the senior developer to review the code of junior developers and then merge the code into the master branch.
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
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 to sdev1 branch. It should have a new commit id once the 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 the master branch does not have the commit details which is for the sdev1.java file and also sdev1.java file should exist in the 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 the master branch. After merging the commit id for sdev1.java addition should be reflected in git log and also sdev1.java file should exist in the 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 the master branch.
git checkout master
git merge jdev1
Step 13:- A new sdev2 joined the company and he resigned after a few days. So we need to create a branch and then delete it.
git checkout master
git branch sdev2
git branch -D sdev2
Step 14:- Update sdev1 and jdev1 branches with latest code ( master branch)
git checkout sdev1
git merge master
ls
git checkout jdev1
git merge master
ls
Step 15: jdev is reporting to sdev and all the code changes by jdev are 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 to work on different modules of the test.java file and once they finish their work then that code will be reviewed and merged in sdev1 branch and then the 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
Step 5:- Update all the branches
git checkout jdev1
git merge sdev1
cat test.java
git checkout jdev2
git merge sdev1
cat test.java
git checkout master
git merge sdev1
cat test.java
Git Workflow
COMMENTS