Troubleshooting Branches
Git branch operations can sometimes lead to complex situations. This section covers common problems and their solutions.
Merge Conflicts
Identifying Merge Conflicts
## Check current merge status
git status
## Show conflicting files
git diff
Resolving Merge Conflicts
## Manually edit conflicting files
nano conflicting-file.txt
## Mark conflicts as resolved
git add conflicting-file.txt
## Complete merge
git merge --continue
Workflow Conflict Resolution
graph TD
A[Merge Conflict Detected] --> B{Choose Action}
B --> |Manual Resolution| C[Edit Conflicting Files]
B --> |Abort Merge| D[git merge --abort]
B --> |Accept Current Branch| E[git checkout --theirs file]
B --> |Accept Incoming Branch| F[git checkout --ours file]
Branch Management Challenges
Issue |
Symptoms |
Solution |
Orphaned Branches |
Unused branches |
git branch -d branch-name |
Detached HEAD |
Commits not on any branch |
git checkout -b new-branch |
Stale Remote Branches |
Outdated remote tracking |
git fetch --prune |
Advanced Troubleshooting Commands
Recovering Lost Commits
## Find lost commits
git reflog
## Restore lost commit
git checkout <lost-commit-hash>
Cleaning Up Branches
## Delete local branch
git branch -d branch-name
## Force delete unmerged branch
git branch -D branch-name
## Delete remote branch
git push origin --delete branch-name
Preventing Branch Issues
- Always pull before pushing
- Use feature branches
- Communicate with team members
- Regularly prune unnecessary branches
Diagnostic Commands
## Show branch relationships
git branch -vv
## List merged/unmerged branches
git branch --merged
git branch --no-merged
LabEx Tip
Practice branch troubleshooting in LabEx's safe, interactive Git environments to build confidence in handling complex scenarios.
Best Practices
- Commit frequently
- Use descriptive branch names
- Keep branches short-lived
- Regularly synchronize with main branch
- Use pull requests for code review
Emergency Recovery Techniques
## Recover deleted branch
git reflog
git checkout -b recovered-branch <commit-hash>
Common Error Messages and Solutions
Error Message |
Meaning |
Solution |
error: pathspec |
Invalid branch or path |
Verify branch name |
merge conflict |
Conflicting changes |
Manually resolve conflicts |
branch already exists |
Duplicate branch name |
Use unique branch names |