Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Git & GitHub - Advanced Branching

Advanced techniques for branching in Git

Branching is a powerful feature in Git that allows you to work on different parts of a project simultaneously. This guide covers advanced branching techniques, including branch management, rebasing, and complex workflows.

Key Points:

  • Understand advanced branching strategies to manage complex projects.
  • Learn how to rebase branches to keep a clean commit history.
  • Explore different branching workflows for better collaboration and productivity.

Branch Management

Effective branch management is essential for maintaining a clean and organized repository. Here are some advanced techniques for managing branches:

Listing All Branches

Use the git branch command to list all branches in your repository:


# List all local branches
$ git branch

# List all remote branches
$ git branch -r

# List all branches, both local and remote
$ git branch -a
                

Renaming Branches

To rename a branch, use the git branch -m command:


# Rename the current branch
$ git branch -m new-branch-name

# Rename a specific branch
$ git branch -m old-branch-name new-branch-name
                

Deleting Branches

To delete a branch, use the git branch -d command for local branches and git push --delete for remote branches:


# Delete a local branch
$ git branch -d branch-name

# Force delete a local branch (if it hasn't been merged)
$ git branch -D branch-name

# Delete a remote branch
$ git push origin --delete branch-name
                

Rebasing

Rebasing is a technique that allows you to integrate changes from one branch into another, creating a linear commit history:

Interactive Rebase

Use interactive rebase to edit, combine, or reorder commits:


# Start an interactive rebase for the last n commits
$ git rebase -i HEAD~n

# Example: Rebase the last 3 commits interactively
$ git rebase -i HEAD~3
                

Rebase a Branch

To rebase a branch onto another branch:


# Checkout the branch you want to rebase
$ git checkout feature-branch

# Rebase onto the target branch
$ git rebase target-branch
                

Handling Rebase Conflicts

If there are conflicts during a rebase, Git will pause and allow you to resolve them:


# Resolve conflicts in the affected files
$ git add resolved-file

# Continue the rebase process
$ git rebase --continue

# If you want to abort the rebase
$ git rebase --abort
                

Branching Workflows

Different projects and teams may use different branching workflows to manage their development process. Here are some common workflows:

Git Flow

Git Flow is a popular branching model that defines a strict branching structure:

  • master: The main branch where releases are made.
  • develop: The branch where feature branches are merged and integration happens.
  • feature/*: Branches for developing new features.
  • release/*: Branches for preparing releases.
  • hotfix/*: Branches for fixing bugs in production.

# Example commands for Git Flow
$ git checkout -b feature/awesome-feature develop
$ git checkout develop
$ git merge feature/awesome-feature
$ git checkout -b release/1.0 develop
$ git checkout master
$ git merge release/1.0
$ git tag -a v1.0
$ git checkout hotfix/1.0.1
$ git merge hotfix/1.0.1
                

GitHub Flow

GitHub Flow is a simpler workflow that emphasizes continuous delivery:

  • master: The only long-lived branch, always in a deployable state.
  • feature branches: Short-lived branches for new features or fixes, merged into master via pull requests.

# Example commands for GitHub Flow
$ git checkout -b feature/awesome-feature
$ git push origin feature/awesome-feature
# Open a pull request on GitHub
# Merge the pull request into master
$ git checkout master
$ git pull origin master
                

Summary

This guide covered advanced branching techniques in Git, including branch management, rebasing, and different branching workflows. Understanding these techniques can help you manage complex projects more effectively and maintain a clean and organized repository.