- Git branch usage
- How it works
- Common options
- Creating branches
- Creating remote branches
- Deleting Branches
The git branch command creates, lists and deletes branches. It doesn’t allow switching between branches or putting a forked history back together again. Thus, git branch is integrated with the git checkout and git merge commands.
Git branch usage
Branching is an available feature in most version control systems. Git branches are a pointer to a snapshot of the changes you have made. A new branch is created to encapsulate the changes when you want to fix bugs or add new features. This helps you to clean up the future's history before merging it. Git branches are an essential part of everyday workflow. Git does not copy files from one directory to another, it stores the branch as a reference to a commit.
How it works
Branches represent an isolated line of development. They are accepted as a way to request a new working directory, staging area and project history. Developing isolated lines of development for two features in branches will enable to operate on them in parallel and make the master branch free from questionable code.
|git branch||Lists all of the branches in the repository (the same as git branch --list).|
|git branch <branch>||Creates a new branch called <branch> but does not checks out the new branch.|
|git branch -d <branch>||Deletes a branch. If there are unmerged changes, Git does not allow you to delete it.|
|git branch -D <branch>||Forces delete the branch, even if there are unmerged changes. Execute this command when you are sure to delete it permanently.|
|git branch -m <branch>||Moves or renames the current branch to <branch>.|
|git branch -a||Lists all the remote branches.|
Branches are just pointers to commits. When you create a branch, Git will create a new pointer. It does not change the history of the repository.
You can create a branch with the git branch command:
git branch test_branch
The history of the repository will remain unchanged. A new pointer to the current commit will be created:
Creating remote branches
The git branch command not only operates on the local branches but also remote branches. Creating a branch on a remote repository must be configured and added to the local repository config:
git remote add new-remote-repo https://hostname/user/repo.git # Add remote repo to local repo config git push <new-remote-repo> test_branch~ # pushes the test_branch branch to new-remote-repo
After finishing the work on a branch and merging it into the main master, you can delete it:
git branch -d test_branch
If unmerged changes remain, an error will occur:
error: The branch 'test_branch' is not fully merged. If you are sure you want to delete it, run 'git branch -D test_branch'.
If you are sure that you want to delete the branch permanently you can use the git branch command with the capital -D option:
git branch -D test_branch
The above-mentioned commands only delete the local copy of a branch. The branch may still exist in remote repositories. Run the following to delete the remote branch:
git push origin --delete test_branch
git push origin :test_branch