Post

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 and checkout 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.
  • 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:

  1. 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
  1. 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 use git 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.

  1. 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
  1. Next, merge the branch into the main branch.
1
2
3
4
git merge <branch-name>

// for example
git merge login-feature
  1. 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

  1. Create a new branch using the git branch or git checkout -b command.
  2. Push the branch to the remote repository using the git -u origin <branch> command.(for the first time)
  3. Make changes and commit them to the branch and push the branch to the remote repository using the git push command.
  4. Switch to the main branch and pull the latest changes from the remote repository using the git switch main and git pull commands.
  5. Merge the branch into the main branch using the git merge <branch-name> command.
  6. Push the changes to the remote repository using the git push command.
  7. 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!😌

This post is licensed under CC BY 4.0 by the author.