Branch

Utilize common version control operations with Git

📘Cisco DevNet Associate (200-901 DEVASC)


1. What is a Git Branch?

A branch in Git is like a separate workspace or timeline for your code.

  • It allows you to work on a new feature, bug fix, or experiment without affecting the main code.
  • Think of it as a parallel path where you can make changes safely.

Key points for the exam:

  • The default branch is usually called main or master.
  • You can create multiple branches to work on different tasks simultaneously.

2. Why Branches are Important

  • Isolate work: Your new code doesn’t break existing code in main.
  • Collaboration: Multiple developers can work on different branches at the same time.
  • Experiment safely: Test new ideas without risk to the stable codebase.
  • Prepare for deployment: You can merge a tested branch into main when ready.

3. Common Git Branch Commands

Here are the commands you must know for the exam:

  1. View all branches
git branch
  • Shows a list of branches. The current branch has an asterisk (*) next to it.
  1. Create a new branch
git branch <branch_name>
  • Example:
git branch feature-login
  • Creates a branch called feature-login but does not switch to it yet.
  1. Switch to a branch
git checkout <branch_name>
  • Example:
git checkout feature-login
  • Now you are working inside the feature-login branch.
  1. Create and switch in one command
git checkout -b <branch_name>
  • Example:
git checkout -b feature-login
  • This creates feature-login and switches to it immediately.
  1. Merge a branch into another
git checkout main
git merge <branch_name>
  • Example:
git checkout main
git merge feature-login
  • This takes all the changes from feature-login and integrates them into main.
  1. Delete a branch
git branch -d <branch_name>
  • Example:
git branch -d feature-login
  • Deletes the branch after its changes are merged.
  • If not merged yet, Git will warn you. Use -D to force delete if needed.

4. Branching Workflow in IT

In an IT environment, branches are used to organize development. Common strategies:

  1. Feature Branching
    • Each new feature has its own branch.
    • Example: feature-api-authentication, feature-ui-dashboard.
  2. Bug Fix Branches
    • Quick fixes go into separate branches.
    • Example: bugfix-login-error.
  3. Release Branches
    • Prepare a stable release.
    • Example: release-v1.2.
  4. Hotfix Branches
    • Urgent fixes to production.
    • Example: hotfix-critical-security.

Tip for exam: Know that branches help parallelize development and keep main stable.


5. Visualizing Branches

  • The git log --oneline --graph --all command shows branches in a tree-like diagram.
  • It’s helpful to see where branches diverge and merge.

Example command:

git log --oneline --graph --all

6. Best Practices

  • Name branches clearly: feature-login, bugfix-auth-error.
  • Keep branches small and focused for easier merging.
  • Regularly merge main into your branch to stay updated.
  • Delete branches after merging to keep the repository clean.

7. Exam Tips

  • Understand what a branch is and why it is used.
  • Memorize the basic branch commands:
    • git branch → list branches
    • git branch <name> → create branch
    • git checkout <name> → switch branch
    • git checkout -b <name> → create + switch
    • git merge <branch> → merge branch
    • git branch -d <name> → delete branch
  • Be ready to identify scenarios in IT where branching is used (features, bug fixes, releases, hotfixes).

Branches are fundamental to team-based development in Git and a core topic for the DEVASC exam. Understanding them clearly will make you confident in version control tasks.

Buy Me a Coffee