Advanced Branch Management
While the basic commands for checking the current branch are essential, Git also provides more advanced features for managing branches. These features can help you streamline your development workflow and collaborate more effectively with your team.
Creating and Switching Branches
To create a new branch, use the git branch
command followed by the name of the new branch:
$ git branch feature/new-functionality
To switch to the new branch, use the git checkout
command:
$ git checkout feature/new-functionality
Switched to branch 'feature/new-functionality'
You can also create and switch to a new branch in a single step using the git checkout -b
command:
$ git checkout -b feature/new-functionality
Switched to a new branch 'feature/new-functionality'
Merging Branches
When you've completed work on a feature branch, you can merge it back into the main branch using the git merge
command:
$ git checkout main
$ git merge feature/new-functionality
Updating 123abc..def456
Fast-forward
file1.txt | 2 +-
file2.txt | 4 ++--
2 files changed, 3 insertions(+), 3 deletions(-)
Deleting Branches
After a branch has been merged, you can safely delete it using the git branch -d
command:
$ git branch -d feature/new-functionality
Deleted branch feature/new-functionality (was def456).
If the branch has not been merged, you can use the -D
option to force the deletion:
$ git branch -D feature/unmerged-functionality
Deleted branch feature/unmerged-functionality (was abc123).
Tracking Remote Branches
When working in a team, it's common to have branches that exist on a remote repository. You can track these branches locally using the git checkout
command with the -b
option:
$ git checkout -b remote-feature origin/remote-feature
Branch 'remote-feature' set up to track 'origin/remote-feature'.
Switched to a new branch 'remote-feature'
This will create a new local branch remote-feature
that tracks the remote-feature
branch on the remote repository.
By mastering these advanced branch management techniques, you can streamline your Git workflow and collaborate more effectively with your team.