Skip to content

DevOps Day 24: Managing Development with Git Branches

Today's task was about a core practice in any collaborative software project: creating a new branch. This is the fundamental technique that allows developers to work on new features or bug fixes in a safe, isolated environment without disrupting the stability of the main project.

I learned how to create a new branch from the existing master branch. The task also reinforced the importance of understanding the underlying Linux file permissions, as I needed to use sudo to perform the Git operation in a system-owned directory. This document is my first-person guide to that process.

Table of Contents


The Task

My objective was to create a new Git branch on the Storage Server. The specific requirements were: 1. Navigate to the /usr/src/kodekloudrepos/games Git repository. 2. Create a new branch named xfusioncorp_games from the master branch. 3. I was not to switch to the new branch or make any code changes.


My Step-by-Step Solution

The process was very straightforward and performed entirely on the command line of the Storage Server.

  1. Connect to the Server: I first logged into the Storage Server as the natasha user.

    ssh natasha@ststor01
    

  2. Navigate to the Repository: It's a critical rule that Git commands must be run from inside the repository's directory.

    cd /usr/src/kodekloudrepos/games
    

  3. Create the Branch: I knew that the repository was in a system directory (/usr/src) and likely owned by root. Therefore, to create a branch (which writes to the .git directory), I would need sudo.

    sudo git branch xfusioncorp_games
    

  4. Verification: The final and most important step was to confirm that the new branch was created. I used the git branch command without any arguments to list all local branches.

    sudo git branch
    
    The output clearly showed both branches, with the asterisk * indicating I was still on the master branch, which was the correct state for this task.
      * master
        xfusioncorp_games
    
    This was the definitive proof that I had successfully completed the task.


Why Did I Do This? (The "What & Why")

- Git Branch: A branch is an independent line of development. I like to think of it as a movable pointer to a specific commit. When I created my new branch, I was essentially creating a new pointer named xfusioncorp_games that started at the exact same point as the master branch. - Isolation and Safety: This is the primary reason for branching. The master branch is typically considered the stable, production-ready version of the code. By creating a separate "feature branch," developers can experiment, build, and test new features without any risk of breaking the main codebase. - Parallel Development: Branching allows multiple developers or teams to work on different features at the same time, each on their own isolated branch. - Foundation for Collaboration: Once the work on a feature branch is complete, it is typically merged back into the master branch through a "Pull Request" or "Merge Request." This process allows for code review and ensures that only tested and approved code gets into the main project.


Deep Dive: git branch vs. git checkout -b

This task helped me understand the subtle but important difference between two common branch-related commands.

[Image of a Git branching diagram]

  • git branch <branch-name> (What I used):

    • This command creates the new branch.
    • It does not switch you to the new branch. You remain on the branch you were on before (in my case, master).
    • This was the perfect command for my task, as the requirement was only to create the branch, not to start working on it.
  • git checkout -b <branch-name> (A Common Shortcut):

    • This command is a convenient shortcut that does two things at once:
      1. It creates the new branch (the -b part).
      2. It immediately switches (checkout) you to that new branch.
    • This is the command a developer would typically use when they are ready to start working on a new feature right away.

Exploring the Commands Used

- ssh natasha@ststor01: The standard command to Secure SHell into the storage server. - cd /usr/src/kodekloudrepos/games: The change directory command, used to navigate into the correct Git repository. - sudo git branch: When used with no arguments, this command lists all the local branches in the repository. The currently active branch is marked with an asterisk *. - sudo git branch xfusioncorp_games: When used with an argument, this command creates a new branch with the specified name. It branches off from the current HEAD commit (the commit I was on in master).