本地创建分支


$ git branch v-1.2
$ git branch
* master
  v-1.2



切换本地分支


$ git branch
* master
  v-1.2

$ git checkout v-1.2
Switched 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.2
Username for 'https://git.dev.tencent.com': shenweiyan
Password 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 log
commit 610d4417c04ce2c53cfb7b77a0525ddb7695b869
Author: shenweiyan <ishenweiyan@qq.com>
Date:   Fri Aug 23 09:08:06 2019 +0800

    fix ipad

commit 8938003aa309ab3cd98af43e67ad4f4aaff5e672
Author: shenweiyan <ishenweiyan@qq.com>
Date:   Fri Aug 23 09:00:22 2019 +0800

    add new post at 2019-08-23-09:00:22

commit 31107425b1488b49d7a1a75f183149e40ba81223
Author: shenweiyan <ishenweiyan@qq.com>
Date:   Thu Aug 22 15:50:56 2019 +0800

    add topic page urls

commit bbf9d6803864dffd7e7965036a638b17be0eeda7
Author: shenweiyan <ishenweiyan@qq.com>
Date:   Thu Aug 22 15:47:02 2019 +0800

    add about page images
......

$ git reset --hard 8938003aa309ab3cd98af43e67ad4f4aaff5e672
HEAD 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."


  1. On GitHub, navigate to the main page of the repository.
  2. Above the list of files, click  NUMBER branches.

  1. 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 origin
Pruning origin
URL: https://shenweiyan:git-sz-0201@github.com/shenweiyan/galaxy.git
 * [pruned] origin/release_17.09
 * [pruned] origin/release_18.05



同步 GitHub fork 后新增加的分支


参考:https://segmentfault.com/q/1010000004228020