Day 10 of My 100-Day DevOps Challenge: Git Branching Strategy

Day 10 of My 100-Day DevOps Challenge: Git Branching Strategy

·

4 min read

What is a Branch?

A branch is an independent line of development within a repository. It allows developers to work on specific features, fixes, or experiments without impacting the stability of the main branch. Once changes are finalized and tested, they can be merged back into the main branch, and the temporary branch can be deleted.

Why Use Branches?

Branches enable developers to work concurrently and isolate their work. This ensures that unfinished or experimental changes don’t disrupt ongoing projects or production environments.

Common Types of Branches:

  • Master/Main: The default branch that contains stable, production-ready code.

  • Feature: For developing new features or functionalities.

  • Release: For final testing and preparation of a release version.

  • Hotfix: For urgent fixes to address critical production issues.

Working with Git and GitHub

Using GitHub UI

The GitHub UI simplifies repository management. Here’s how to create a repository:

  1. Log in to your GitHub account.

  2. Navigate to the Repositories tab and click New.

  3. Fill in the repository name, optional description, and visibility (public/private).

  4. Initialize the repository with optional files like a README, .gitignore, or license file.

  5. Click Create Repository.

Your repository is now ready to use.

Using Git CLI

The Command Line Interface (CLI) offers flexibility and control for repository management. Here’s a detailed guide:

Repository Initialization and Cloning

  1. Initialize a Git Repository:

     git init
    

    This command sets up a new Git repository in your current directory. A hidden .git folder is created to store metadata and objects for version control. Use this when starting a new project.

  2. Clone an Existing Repository:

     git clone <repository-url>
    

    Example:

     git clone https://github.com/your-username/example-repo.git
    

    This creates a local copy of a remote repository, including all files, commit history, and branches.

Staging and Committing Changes

  1. Check Repository Status:

     git status
    

    Displays the state of your working directory and staging area, listing untracked, modified, or staged files.

  2. Stage Changes:

    • Stage specific files:

        git add <file-name>
      
    • Stage all files:

        git add .
      
  3. Commit Changes:

     git commit -m "Your descriptive commit message"
    

    Saves staged changes with a descriptive message.

  4. Undo Staging (if needed):

     git reset <file-name>
    

    Removes a file from the staging area while retaining changes in the working directory.

Managing Branches

Creating and Switching Branches

  1. Create a New Branch:

     git branch <branch-name>
    
  2. Switch to a Branch:

     git checkout <branch-name>
    

    Alternatively, create and switch in one step:

     git checkout -b <branch-name>
    
  3. List All Branches:

     git branch
    

Deleting Branches

  1. Delete Locally:

     git branch -d <branch-name>
    

    Use -D to force-delete an unmerged branch.

  2. Delete Remotely:

     git push origin --delete <branch-name>
    

Working with Remotes

  1. View Remote Repositories:

     git remote -v
    
  2. Add a Remote Repository:

     git remote add origin <repository-url>
    
  3. Rename a Remote:

     git remote rename <old-name> <new-name>
    
  4. Remove a Remote:

     git remote remove <remote-name>
    

Pushing and Pulling Changes

  1. Push Changes to a Remote Branch:

     git push origin <branch-name>
    
  2. Pull Changes from a Remote Repository:

     git pull origin <branch-name>
    

    Combines git fetch and git merge to update your local branch with changes from the remote.

  3. Fetch Changes Without Merging:

     git fetch
    

    Downloads updates without merging them.

Resolving Conflicts

  1. Merge Another Branch:

     git merge <branch-name>
    
  2. Handle Merge Conflicts:

    • Open conflicting files and manually resolve issues.

    • Stage resolved files:

        git add <resolved-file>
      
    • Complete the merge:

        git commit
      

Advanced Git Commands

Rebasing Changes

  1. Reapply Changes on Top of Another Base:

     git rebase <branch-name>
    

    Example:

     git checkout feature-branch
     git rebase main
    
  2. Interactive Rebase:

     git rebase -i HEAD~3
    

    Modify, squash, or reorder commits interactively.

  3. Caution: Avoid rebasing shared branches as it rewrites commit history.

Picking Specific Commits

  1. Apply a Specific Commit to Another Branch:

     git cherry-pick <commit-hash>
    

    Example:

     git checkout main
     git cherry-pick abc123
    
  2. Resolve Conflicts During Cherry-Pick:

    • Resolve conflicts manually.

    • Complete the cherry-pick:

        git cherry-pick --continue
      
  3. Pro Tip: Apply multiple commits:

     git cherry-pick <commit-hash1> <commit-hash2>
    

Useful Debugging Commands

  1. View Commit History:

     git log
    

    For a concise view:

     git log --oneline
    
  2. Check Differences Between Commits:

     git diff <commit-hash1> <commit-hash2>
    
  3. Revert a Commit:

     git revert <commit-hash>
    
  4. Discard Local Changes:

     git checkout -- <file-name>
    

    Resets a file to its last committed state.

Conclusion

Git branching strategies and commands empower developers to work efficiently and collaboratively. By isolating changes, maintaining clear commit histories, and managing conflicts effectively, teams can ensure code stability and seamless integration. Adopting a structured approach to Git usage not only streamlines development but also fosters better collaboration and project organization.