Branch renaming and deletion
Branch Renaming
Git allows developers to rename local branches, which is useful when branch names are non-standard or when adjusting project structure. Renaming a branch does not affect the remote repository unless the modified branch name is explicitly pushed.
To rename the currently checked-out branch, use the following command:
git branch -m <new-branch-name>
To rename another local branch, first switch to a non-target branch, then execute:
git branch -m <old-branch-name> <new-branch-name>
For example, to rename feature-login
to feature-auth
:
# Currently on the main branch
git branch -m feature-login feature-auth
After renaming, to synchronize with the remote repository:
git push origin :<old-branch-name> <new-branch-name>
git push origin -u <new-branch-name>
Branch Deletion
Deleting branches is a routine operation in Git workflows, especially for merged feature branches or temporary experimental branches. Before deletion, ensure the branch content has been merged or is no longer needed.
To delete a local branch:
git branch -d <branch-name>
To force-delete an unmerged branch (losing unmerged changes):
git branch -D <branch-name>
To delete a remote branch:
git push origin --delete <branch-name>
# Or use the empty branch push syntax
git push origin :<branch-name>
Batch Operation Tips
When handling multiple branches, combine grep
and xargs
for batch operations:
Delete all local branches merged into the current branch:
git branch --merged | grep -v '\*' | xargs -n 1 git branch -d
Delete all remote branches with the "temp-" prefix:
git branch -r | grep 'origin/temp-' | sed 's/origin\///' | xargs -n 1 git push origin --delete
Branch Recovery
To recover a mistakenly deleted branch, use reflog
:
# View operation history to find the commit hash before deletion
git reflog
# Recreate the branch based on a specific commit
git branch <branch-name> <commit-hash>
For example, to recover the deleted feature-payment
branch:
git reflog | grep 'feature-payment'
# Assuming the found hash is a1b2c3d
git branch feature-payment a1b2c3d
Branch Naming Conventions
Good branch naming habits improve team collaboration efficiency. Recommended patterns include:
feature/
for new feature developmentbugfix/
for issue fixeshotfix/
for urgent fixesrelease/
for version releasesexperiment/
for experimental features
Examples:
git branch feature/user-profile
git branch bugfix/login-validation
git branch release/v1.2.0
GUI Tool Operations
For developers unfamiliar with the command line, mainstream Git clients provide visual operations:
VS Code Git Extension:
- Right-click the target branch in the branch view
- Select "Rename Branch" or "Delete Branch"
- Confirm the action
GitKraken Workflow:
- Locate the target branch in the left branch list
- Click the more options button next to the branch
- Select "Rename" or "Delete"
Team Collaboration Considerations
When operating branches in a multi-person project, note the following:
- Notify the team channel before deleting remote branches
- Update references in related documentation after renaming branches
- CI/CD pipelines may contain branch name configurations
- Other team members need to update local branch tracking relationships:
git fetch --prune
git remote set-head origin -a
Hook Script Applications
Use Git hooks to implement branch operation restrictions, such as a pre-push
hook to prevent accidental deletion of main branches:
#!/bin/sh
protected_branches=('main' 'master' 'develop')
branch=$(git rev-parse --abbrev-ref HEAD)
for protected in "${protected_branches[@]}"
do
if [ "$branch" = "$protected" ]; then
echo "Error: Pushing delete operations to protected branch $protected is prohibited"
exit 1
fi
done
exit 0
本站部分内容来自互联网,一切版权均归源网站或源作者所有。
如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn
上一篇:长期分支与特性分支
下一篇:Webpack的持久化缓存机制