Followers

Git Branches With Merge Conflict

  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

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 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 

            git mergetool
            update the changes 
            cat test.java
            rm test.java.orig
            git commit -m "final test.java file"

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

Name

Ansible,6,AWS,1,Azure DevOps,1,Containerization with docker,2,DevOps,2,Docker Quiz,1,Docker Swarm,1,DockerCompose,1,ELK,2,git,2,Jira,1,Kubernetes,1,Kubernetes Quiz,5,SAST DAST Security Testing,1,SDLC Quiz,3,SonarQube,3,Splunk,2,vagrant kubernetes,1,YAML Basics,1,
ltr
item
DevOpsWorld: Git Branches With Merge Conflict
Git Branches With Merge Conflict
https://blogger.googleusercontent.com/img/a/AVvXsEjYnI008Tp3bQcRq0aLCcfGyfUISjo2SsGSmPosvdbAzUhFiiJVOMLZNEzAbNuv2hSsJW4QraJUxLY7jaWcT9Jfeke9wEA0pMn4TWt29HxYMeRO_mNgjgTFB1yLw2sWwH0cn-_fC3H7BMpeL-l4wnOxnXYjlihSWouToaiIHfz2Aqt8wMF327Laz4Jh=w479-h338
https://blogger.googleusercontent.com/img/a/AVvXsEjYnI008Tp3bQcRq0aLCcfGyfUISjo2SsGSmPosvdbAzUhFiiJVOMLZNEzAbNuv2hSsJW4QraJUxLY7jaWcT9Jfeke9wEA0pMn4TWt29HxYMeRO_mNgjgTFB1yLw2sWwH0cn-_fC3H7BMpeL-l4wnOxnXYjlihSWouToaiIHfz2Aqt8wMF327Laz4Jh=s72-w479-c-h338
DevOpsWorld
https://www.devopsworld.co.in/2022/02/git-branches.html
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/2022/02/git-branches.html
true
5997357714110665304
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content