本地創(chuàng)建分支
$ git branch v-1.2$ git branch* master v-1.2
切換本地分支
$ git branch* master v-1.2$ git checkout v-1.2Switched to branch 'v-1.2'$ git branch master* v-1.2
刪除本地分支
$ git branch -D v-1.2
把本地分支提交到遠程分支倉庫
$ git add --all$ git commit -m "ASO design for Galaxy version 1.2"$ git push origin v-1.2Username for 'https://git.dev.tencent.com': shenweiyanPassword for 'https://shenweiyan@git.dev.tencent.com':Counting objects: 19, done.Delta compression using up to 2 threads.Compressing objects: 100% (17/17), done.Writing objects: 100% (18/18), 29.87 KiB | 0 bytes/s, done.Total 18 (delta 3), reused 0 (delta 0)To https://git.dev.tencent.com/shenweiyan/aso_design.git * [new branch] v-1.2 -> v-1.2
查看一下遠程倉庫有幾個分支
$ git branch -a master* v-1.2 remotes/origin/HEAD -> origin/master remotes/origin/master remotes/origin/v-1.2 $ git remote show origin
刪除遠程分支和 tag
在 Git v1.7.0 之后,可以使用這種語法刪除遠程分支:
git push origin --delete <branchName>
刪除 tag 這么用:
git push origin --delete tag <tagname>
否則,可以使用這種語法,推送一個空分支到遠程分支,其實就相當于刪除遠程分支:
git push origin :<branchName>
這是刪除 tag 的方法,推送一個空 tag 到遠程 tag:
git tag -d <tagname>git push origin :refs/tags/<tagname>
兩種語法作用完全相同。
git 本地回滾到之前某一次的 commit
$ git logcommit 610d4417c04ce2c53cfb7b77a0525ddb7695b869Author: shenweiyan <ishenweiyan@qq.com>Date: Fri Aug 23 09:08:06 2019 +0800 fix ipadcommit 8938003aa309ab3cd98af43e67ad4f4aaff5e672Author: shenweiyan <ishenweiyan@qq.com>Date: Fri Aug 23 09:00:22 2019 +0800 add new post at 2019-08-23-09:00:22commit 31107425b1488b49d7a1a75f183149e40ba81223Author: shenweiyan <ishenweiyan@qq.com>Date: Thu Aug 22 15:50:56 2019 +0800 add topic page urlscommit bbf9d6803864dffd7e7965036a638b17be0eeda7Author: shenweiyan <ishenweiyan@qq.com>Date: Thu Aug 22 15:47:02 2019 +0800 add about page images......$ git reset --hard 8938003aa309ab3cd98af43e67ad4f4aaff5e672HEAD is now at 8938003 add new post at 2019-08-23-09:00:22
git 遠程回滾到之前某一次的 commit
# 方法一,先 git reset 回滾到本地,然后再強制 push 到遠程。$ git reset --hard 8938003aa309ab3cd98af43e67ad4f4aaff5e672# 其中,-f:force(謹慎操作,備份預備)$ git push -u origin master -f
刪除 GitHub 的分支
You can have head branches automatically deleted after pull requests are merged in your repository. For more information, see "Managing the automatic deletion of branches."
On GitHub, navigate to the main page of the repository.
Above the list of files, click NUMBER branches.
Scroll to the branch that you want to delete, then click delete icon.
參考:https://help.github.com/en/articles/creating-and-deleting-branches-within-your-repository
刪除在本地有但在遠程庫中已經不存在的分支
可以查看遠程庫的一些信息,及與本地分支的信息。有時候可能遇到如下情況:
$ git remote show origin* remote origin Fetch URL: https://shenweiyan:git-sz-0201@github.com/shenweiyan/galaxy.git Push URL: https://shenweiyan:git-sz-0201@github.com/shenweiyan/galaxy.git HEAD branch: dev Remote branches: dev tracked master tracked refs/remotes/origin/release_17.09 stale (use 'git remote prune' to remove) refs/remotes/origin/release_18.05 stale (use 'git remote prune' to remove) release_13.01 tracked release_13.02 tracked release_13.04 tracked
其中 release_17.09, release_18.05 兩個分支在遠程庫已經不存在了(你之前從遠程庫拉取過,可能之后被其他人刪除了,你用 git branch -a
也是不能看出它們是否已被刪除的),這時候我們可以刪除本地庫中這些相比較遠程庫中已經不存在的分支:
$ git remote prune originPruning originURL: https://shenweiyan:git-sz-0201@github.com/shenweiyan/galaxy.git * [pruned] origin/release_17.09 * [pruned] origin/release_18.05