git-branch

neevop 七月 2, 2023


分支管理

创建

# creating a local branch
git checkout -b $branch_name

# push new branch to origin
git push origin $branch_name --set-upstream

仓库拉取分支

git fetch origin 

git checkout --track origin/$branch_name

删除本地所有分支

# delete origin/* branches in local copy, don't affact remote.
git remote prune origin

展示所有分支

# current branch will be highlighted with asterisk
git branch --list 

展示已合并分支

# list outdated branches that have been merged into current one
git branch -a --merged

删除一个本地分支

git branch -d $branch_name

强制删除一个分支

# which is synonymous with --delete --force instead of -d
git branch -D $branch_name

删除远端分支

# works for tag too.
git push origin --delete :$branch_name

重制分支

# reset branch and remove all changes
git reset --hard

回退提交至某个提交

git reset --hard $commit_id

# push safely
git push --force-with-lease

# push brutally
git push --force