Skip to content

Latest commit

 

History

History
166 lines (123 loc) · 4.25 KB

1. Branch Basics.md

File metadata and controls

166 lines (123 loc) · 4.25 KB

Branch Basics

Branches in Git are a powerful feature that allow you to work on different parts of a project simultaneously without affecting the main codebase. This page explains the basics of branches and how to use them effectively.


1. What is a Branch?

A branch in Git is a pointer to a specific commit in your project’s history. It enables you to:

  • Develop features or fix bugs in isolation.
  • Experiment without disrupting the main codebase.
  • Collaborate on separate tasks within a team.

Default Branch

When you initialize a Git repository, a default branch is created. It is usually named main or master.


2. Why Use Branches?

  • Isolation: Keep changes for new features or bug fixes separate from the main project.
  • Collaboration: Multiple team members can work on different branches simultaneously.
  • Versioning: Easily switch between different versions or states of your project.

3. Working with Branches

View Existing Branches

To list all branches in your repository:

git branch

Create a New Branch

To create a new branch:

git branch branch_name

Switch to a Branch

To move to an existing branch:

git checkout branch_name

Or, create and switch to a new branch in one command:

git checkout -b branch_name

4. Managing Branches

Rename a Branch

To rename the branch you are currently on:

git branch -m new_branch_name

Delete a Branch

To delete a branch locally:

git branch -d branch_name

Use -D to force-delete a branch:

git branch -D branch_name

5. Merging Branches

Merging integrates changes from one branch into another. For example, merging a feature branch into main:

  1. Switch to the target branch:

    git checkout main
  2. Merge the feature branch:

    git merge feature_branch

Resolve Merge Conflicts

If there are conflicts during merging:

  1. Open the affected files and resolve the conflicts.
  2. Mark conflicts as resolved:
    git add file_with_conflicts
  3. Complete the merge:
    git commit

6. Viewing Branch History

To see a graphical view of branches and commits:

git log --oneline --graph --all

7. Best Practices for Branching

  • Use Descriptive Names: Name branches based on their purpose, e.g., feature/user-auth or bugfix/login-error.
  • Work Incrementally: Make small, manageable changes on branches.
  • Keep main Stable: Only merge tested and working code into the main branch.
  • Regularly Sync with main: Pull the latest changes from main into your branch to avoid large conflicts.

Common Commands

Command Description
git branch List all branches.
git branch branch_name Create a new branch.
git checkout branch_name Switch to an existing branch.
git checkout -b branch_name Create and switch to a new branch.
git merge branch_name Merge a branch into the current branch.
git branch -d branch_name Delete a branch.
git log --oneline --graph --all View a graphical history of branches.

Example Workflow

Step 1: Create and Switch to a New Branch

git checkout -b feature/new-feature

Step 2: Make Changes and Commit

echo "New feature code" > feature.txt
git add feature.txt
git commit -m "Add new feature"

Step 3: Merge the Branch into main

git checkout main
git merge feature/new-feature

Step 4: Delete the Feature Branch

git branch -d feature/new-feature

Conclusion

Branches are an essential part of Git workflows, allowing you to isolate changes, collaborate with others, and maintain a clean and stable codebase. Mastering branch management is key to efficient version control.


Next Steps: Merging Changes