Git - Working with Branches
Working with branches is a fundamental part of using Git. Branches allow you to work on different features or bug fixes in isolation from each other. This post will cover the basics of working with branches in Git.
Branch Workflow
After you have initialized a Git repository, you will have a default branch called main
. This is the branch that contains the latest stable version of your code.
- Before you start working on a new feature or bug fix, it is a good practice to
create a new branch
andcheckout
to it.- This will allow you to work on the new feature or bug fix without affecting the
main
branch. - I recommend naming the branch after the feature or bug fix you are working on.
- This will allow you to work on the new feature or bug fix without affecting the
- Next, publish the branch to the remote repository so that other team members can see it.
- Make your changes and commit them to the branch and push the branch to the remote repository.
- when you are done with the feature or bug fix, you can merge the branch back into the
main
branch. - This is done using a
pull request
.
Creating a New Branch
There are two ways to create a new branch in Git:
- Using the
git branch
command, which creates a new branch but does not switch to it.
1
2
3
4
git branch <branch-name>
// for example
git branch login-feature
- Using the
git checkout
command with the-b
flag, which creates a new branch and switches to it.
1
git checkout -b <branch-name>
Pushing a local Branch to the Remote Repository
After you have made changes and committed them to the branch, you can push the branch to the remote repository using the git push
command.
Use this command to push the branch to the remote repository for the first time.
1
git push -u origin <branch>
- The
-u
flag sets the upstream branch for the local branch. This means that in the future, you can simply usegit push
without specifying the remote and branch name.
Merging a Branch into the Main Branch
After you have completed the feature or bug fix, you can merge the branch back into the main
branch using a pull request
.
- First, ensure that the
main
branch is up to date with the remote repository.
1
2
3
4
5
// switch to the main branch
git switch main
// pull the latest changes from the remote repository
git pull
- Next, merge the branch into the
main
branch.
1
2
3
4
git merge <branch-name>
// for example
git merge login-feature
- Finally, push the changes to the remote repository.
1
git push
Deleting a Branch
This is an optional step.
After you have merged a branch into the main
branch, you can delete the branch using the git branch -d
command.
1
2
3
4
git branch -d <branch-name>
// for example
git branch -d login-feature
Summary
- Create a new branch using the
git branch
orgit checkout -b
command. - Push the branch to the remote repository using the
git -u origin <branch>
command.(for the first time) - Make changes and commit them to the branch and push the branch to the remote repository using the
git push
command. - Switch to the
main
branch and pull the latest changes from the remote repository using thegit switch main
andgit pull
commands. - Merge the branch into the
main
branch using thegit merge <branch-name>
command. - Push the changes to the remote repository using the
git push
command. - Delete the branch using the
git branch -d
command (optional).
I hope this post has given you a good understanding of how to work with branches in Git. If you have any questions or feedback, feel free to reach out to me. Happy coding!😌