Skip to main content

Branching and Merging Code with Git Source Control

Hands-On Lab

 

Photo of Terrence Cox

Terrence Cox

Senior Vice President of Content

Length

01:00:00

Difficulty

Beginner

When using Git for source control and versioning, the ability to branch that repository while you make changes off the mainline is key. However, eventually, that branch will need to rejoin the master branch and you will need to perform a merge. In this activity, the student will get an opportunity to perform a branch and then learn how that branch differs from the master, how they can move back and forth between the branch and master and finally, how to merge it back into mainline.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Branching and Merging Code with Git Source Control

Introduction

When using Git for source control and versioning, the ability to branch that repository while you make changes off the mainline is key. However, eventually, that branch will need to rejoin the master branch and you will need to perform a merge. In this activity, the student will get an opportunity to perform a branch and then learn how that branch differs from the master, how they can move back and forth between the branch and master and finally, how to merge it back into mainline.

Solution

  1. Begin by logging in to the lab server using the credentials provided on the hands-on lab page:

    ssh cloud_user@PUBLIC_IP_ADDRESS

Create the fix-readme Branch

  1. Navigate to the web-api directory.

    cd web-api
  2. Create the fix-readme branch.

    git checkout -b fix-readme

Correct the README.md File

  1. View the contents of the README.md file.

    cat README.md
  2. Edit the README.md file.

    vi README.md
  3. Correct the spelling error by replacing "Reamde" with "Readme".

  4. Save the file.

Merge the Corrected README.md File Into the Master Branch

  1. Check in the corrected README.md file.

    git commit -a -m "corrected README.md"
  2. Change to the master branch.

    git checkout master
  3. Merge the fix into the master branch.

    git merge fix-readme
  4. Verify the file has been corrected.

    cat README.md

Conclusion

Congratulations — you've completed this hands-on lab!