Git Branch

Categories: Notes

Git Branch

branch ai geneated  picture

Certainly! Here are a few examples of using Git branches:

  1. Creating a branch: To create a new branch called “feature” based on the current commit, you can use the following command:
git branch feature
  1. Switching to a branch: To switch to the “feature” branch you just created, you can use the following command:
git checkout feature

Note: In Git versions 2.23 and later, you can use git switch instead of git checkout to switch branches.

  1. Creating and switching to a branch in one step: If you want to create a new branch and switch to it in a single command, you can use:
git checkout -b feature

This is a shorthand for creating the branch and then switching to it.

  1. Viewing branches: To see a list of all branches in your repository and see which branch you’re currently on, you can use:
git branch

The branch you’re currently on will have an asterisk (*) next to it.

  1. Merging branches: Once you’ve made changes on a branch and want to incorporate those changes into another branch (usually the main branch), you can merge the branches. For example, to merge the “feature” branch into the main branch, you would switch to the main branch (git checkout main) and then use the merge command:
git merge feature

This integrates the changes made in the “feature” branch into the main branch.

  1. Deleting a branch: Once a branch is no longer needed, you can delete it using the following command:
git branch -d feature

This deletes the “feature” branch. However, make sure you have merged or pushed any important changes on the branch before deleting it.

These are just a few examples of using Git branches. Branching allows you to work on different features or experiments concurrently, switch between branches, merge changes, and collaborate effectively within a Git repository.

Written on June 28, 2023