Git Branching
Understanding Git Branches
Branches in Git serve as alternate timelines within your project, allowing you to:
- Experiment with new features
- Simultaneously develop multiple ideas
- Isolate changes from the main branch until they're ready
Changes made on one branch remain isolated and don't affect others unless explicitly merged.
Common Use-Cases:
- Testing different designs or color schemes
- Fixing bugs without impacting the stable version
- Experimenting with new implementations
- Facilitating collaborative teamwork
Master (or Main) Branch
- Git's default branch is typically called
master, renamed tomainby platforms like GitHub since 2020. - It's often treated as the project's official or "source of truth" branch, though technically it functions like any other branch.
Understanding HEAD
HEAD is a special reference (or pointer) used by Git to represent your current working location in a repository.
What does HEAD point to?
- In most situations,
HEADpoints to the latest commit on the current branch. - It acts as a symbolic reference to the branch you're currently working on.
Example:
HEAD -> main
This means you're on the main branch, and HEAD is pointing to the most recent commit on main.
Detached HEAD
Sometimes HEAD points directly to a commit instead of a branch. This is known as a detached HEAD state.
How does this happen?
Detached HEAD states occur when you:
- Check out a specific commit:
git checkout 5d6ef1c
- Use switch in detached mode:
git switch --detach 5d6ef1c
In these cases, HEAD is not pointing to any branch, just to a single commit.
Example:
HEAD -> 5d6ef1c (no branch)
You can still make changes and commits, but they will be on a temporary timeline. If you switch branches again without saving, those changes could be lost.
Use Cases for Detached HEAD
- Inspecting an older commit without affecting branches
- Running tests or debugging a historical version
- Creating a hotfix or quick patch starting from a past commit
Returning from Detached HEAD
To safely exit a detached HEAD state:
- Switch back to a branch:
git switch main
- Or create a new branch from the current detached state:
git switch -c temp-fix
This preserves your work by anchoring it to a named branch.
Common Git Branching Commands
Viewing Branches
List all branches, with an asterisk (*) showing the current branch:
git branch
For detailed branch information:
git branch -v
Example Output:
* master 88df51f add documentation
bugfix 7c2a586 fix hover bug
chatdemo afadcf9 add chat widget
Creating Branches
Create a new branch based on the current position (HEAD):
git branch <branch-name>
Note: This command creates the branch but doesn't switch to it immediately.
Switching Branches
Switch to an existing branch:
git switch <branch-name>
Historical alternative (still widely used):
git checkout <branch-name>
Create and switch to a new branch simultaneously:
git switch -c <branch-name>
Example: Creating & Switching Branches
Create a branch named bugfix from master:
git branch bugfix
Switch to the bugfix branch:
git switch bugfix
Or perform both steps simultaneously:
git switch -c bugfix
Committing on Branches
Commits made to a branch are isolated:
- Changes committed to a branch (e.g.,
bugfix) do not affect other branches until explicitly merged.
Typical Branch Workflow
- Create and switch to a new branch for a feature or fix.
- Commit changes on this branch.
- Merge the finished branch back into
masterormain.
Branching from Another Branch
Branches inherit commits from their parent branch:
- Creating a branch from an existing branch (e.g., branching from
bugfix) includes all the commits from the original branch.
Merging Branches
After completing work on a branch, merge it back into master:
git switch master
git merge bugfix
Let me know if you need further edits or additional details!