
- Git - Home
- Git - Version Control
- Git - Basic Concepts
- Git - Command Line
- Git - Installation
- Git - First Time Setup
- Git - Basic Commands
- Git - Getting Help
- Git - Tools
- Git - Cheat Sheet
- Git - Terminology
- Git - Life Cycle
- Git - Get a Repository
- Git - Adding New Files
- Git - Recording Changes
- Git - Viewing Commit History
- Git Branching
- Git - Branches in a Nutshell
- Git - Creating a New Branch
- Git - Switching Branches
- Git - Branching and Merging
- Git - Merge Conflicts
- Git - Managing Branches
- Git - Branching Workflows
- Git - Remote Branches
- Git - Tracking Branches
- Git - Rebasing
- Git - Rebase vs. Merge
- Git - Squash Commits
- Git Operations
- Git - Clone Operation
- Git - Tagging Operation
- Git - Aliases Operation
- Git - Commit Operation
- Git - Stash Operation
- Git - Move Operation
- Git - Rename Operation
- Git - Push Operation
- Git - Pull Operation
- Git - Fork Operation
- Git - Patch Operation
- Git - Diff Operation
- Git - Status Operation
- Git - Log Operation
- Git - Head Operation
- Git - Origin Master
- Git Undoing
- Git - Undoing Changes
- Git - Checkout
- Git - Revert
- Git - Reset
- Git - Restore Operation
- Git - Rm
- Git - Switch Operation
- Git - Cherry-pick
- Git - Amend
- Git on the Server
- Git - Local Protocol
- Git - Smart HTTP Protocol
- Git - Dumb HTTP Protocol
- Git - The SSH Protocol
- Git - The Git Protocol
- Git - Getting Git on a Server
- Git - Setting up the Server
- Git - Daemon
- Git - GitWeb
- Git - GitLab
- Git - Third Party Hosted Options
- Distributed Git
- Git - Distributed Workflows
- Git - Contributing to a Project
- Git - Maintaining a Project
- Customizing Git
- Git - Configuration
- Git - Hooks
- Git - Attributes
- Git - Init
- Git - Commit
Git - Managing Branch
In the earlier chapters we learned how to add and remove branches, this chapter discusses about branch management.
There are more uses for the git branch command than merely adding and removing branches.
The git branch when run with no arguments displays all of the repository's current branches when called without any arguments:
$ git branch auth-module * master feature-x
The branch that is now checked out (i.e., where HEAD points) is indicated by an asterisk (*).
This state's commits will push the checked-out branch forward.
Branch`s Recent Commit
Use git branch -v to see each branch's most recent commit.
$ git branch -v auth-module abcd123 Latest commit on auth-module * master 7a1b2c3d Latest commit on master feature-x e4f5g6h7 Latest commit on feature-x
Branch Filtering
With the git branch command, we can use the --merged and --no-merged arguments to filter the list of branches according to whether they have been merged into the current branch.
For instance, we could execute the following command to find out which branches have previously been combined into the master branch we are now on:
$ git branch --merged auth-module * master
Use git branch --merged to view auth-module listed among branches that have already been merged after merging it into the main branch (master).
Using git branch -d, we can securely delete branches that are listed without the * (asterisk) symbol.
This shows that their modifications have already been incorporated into a different branch, guaranteeing that there will be no work loss upon their removal.
Use git branch --no-merged to view branches that still have work in them that hasn't been merged into the current branch. For Example:
$ git branch --no-merged feature-x
This command displays branches with changes that haven't been merged, such as feature-x. Such a branch cannot be deleted with git branch -d because an error stating that the branch is not fully merged will appear.
$ git branch -d feature-x error: The branch 'feature-x' is not fully merged. If you are sure you want to delete it, run 'git branch -D feature-x'
Use git branch -D feature-x to forcefully delete such a branch, guaranteeing that all changes are eliminated permanently.
Changing a branch name
To change a branch name , say for example rename a branch called fix-typos to typo-fixes while keeping its whole history unchanged. use git branch --move. This renames the branch locally:
$ git branch --move fix-typos typo-fixes
Fix-typos on our local repository is now called typo-fixes.
To update the remote repository, push the renamed branch:
$ git push --set-upstream origin typo-fixes
The new branch name typo-fixes are updated in the remote repository using this command.
Check both locally and remotely for the branch names:
$ git branch --all * typo-fixes main remotes/origin/fix-typos remotes/origin/typo-fixes remotes/origin/main
The branch we are now working on locally is typo-fixes, which is also accessible from the remote repository.
On the remote, the branch fix-typos is still listed under the previous name. Delete the previous name of the branch from the remote as follows:
$ git push origin --delete fix-typos
With this command, the remote repository's previous branch name fix-typos is removed, keeping only typo-fixes.
Now, both locally and on the remote repository, the branch fix-typos has been successfully renamed to typo-fixes.
Changing the master branch name
Use the following steps to update our local master branch on the remote repository and rename it to feature-new:
Use git branch --move to rename the local branch:
$ git branch --move master feature-new
The above command renames the local master branch as feature-new.
To update the remote repository, push the renamed branch:
$ git push --set-upstream origin feature-new
With the new branch feature-new, the remote repository is updated.
Use the command as follows to check branch names both on local and remote:
$ git branch --all * feature-new remotes/origin/HEAD -> origin/master remotes/origin/feature-new remotes/origin/master
Our current local branch is now called feature-new, and it is present on the remote repository as well.
However on the remote, the previous branch name, master, is still present.
After updating it on the remote and renaming our local master branch to feature-new.
Other collaborators are still permitted to utilize the previous master branch.
To complete this transition we have further tasks to perform. The tasks include merging and closing pull requests that are directed at the old branch, as well as upgrading dependencies, configurations, scripts, repository host settings, and documentation references.
We can safely delete the master branch once all required tasks have been completed and we are certain that the feature-new branch works in the same way as the previous master branch.
Use the following command to delete the previous name of the branch from the remote:
$ git push origin --delete master
The remote repository's master branch is deleted, keeping just feature-new.
The branch master has now been successfully renamed to feature-new on the remote repository as well as locally.