博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

git branch

Posted on 2017-07-18 14:47  bw_0927  阅读(223)  评论(0)    收藏  举报

查看分支是基于哪个分支checkout出来的

 git reflog --date=local | grep 'ppc_9.26'  

 

git branch -vv

 

git branch --set-upstream-to origin/develop  local_branch
git branch --unset-upstream

 

git checkout 实际上是修改HEAD文件的内容,让其指向不同的branch。HEAD文件指向的branch就是当前branch.


一般来讲,HEAD的内容是指向staging(暂存区)的master文件的。
如果让HEAD文件指向一个commit id,那就变成了detached HEAD。
git checkout 6d5d216出现了一个新分支:
(detached from 6d5d216)
这个状态还不算是一个新分支吧,叫做 detached 状态。如果你这时做了个 commit, 那么这个 commit 就是属于一个新分支的,但如果不给这个新分支命名,然后又 checkout 了其他分支,那么这个新分支的引用计数就变为0了,会被回收。
切换分支前留心你的暂存区或者工作目录里,那些还没有提交的修改,它会和你即将检出的分支产生冲突从而阻止 Git 为你切换分支。切换分支的时候最好保持一个清洁的工作区域。稍后会介绍几个绕过这种问题的办法(分别叫做 stashing 和 commit amending)

 

git branch命令的-r选项,可以用来查看远程分支,-a选项查看所有分支。

 

$ git checkout -b newBrach origin/master

上面命令表示,在origin/master的基础上,创建一个新分支。

 

=============

查看分支之间的关系,当前分支的上游分支

1.  使用git log命令

git log --graph --decorate --oneline --simplify-by-decoration --all

说明:

--decorate 标记会让git log显示每个commit的引用(如:分支、tag等) 

--oneline 一行显示

--simplify-by-decoration 只显示被branch或tag引用的commit

--all 表示显示所有的branch,这里也可以选择,比如我指向显示分支ABC的关系,则将--all替换为branchA branchB branchC

 

2. 使用gitk工具

gitk --simplify-by-decoration --all

 

-v, --verbose
Show sha1 and commit subject line for each head, along with relationship to upstream branch (if any). If given twice, print the name of the upstream branch, as well.

--contains <commit>
Only list branches which contain the specified commit.

--merged [<commit>]
Only list branches whose tips are reachable from the specified commit (HEAD if not specified).

--no-merged [<commit>]
Only list branches whose tips are not reachable from the specified commit (HEAD if not specified).

 

git branch -r  查看远程分支

 

 

  1. 首先, clone 一个远端仓库,到其目录下:
$ git clone git://example.com/myproject
$ cd myproject
  1. 然后,看看你本地有什么分支:
$ git branch

会出现:
* master
  1. 但是有些其他分支你在的仓库里面是隐藏的,你可以加上-a选项来查看它们:
$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/develop
  remotes/origin/feature
  remotes/origin/feature-im
  remotes/origin/master
  remotes/origin/newbranch

5.如果你想在那个分支工作的话,你就需要创建一个本地分支:

$ git checkout -b feature origin/feature

现在,如果你看看你的本地分支,你会看到:

$ git branch
会出现
* feature
  master

或者使用-t参数,它默认会在本地建立一个和远程分支名字一样的分支

$ git checkout -t origin/feature

 


 

设置本地分支和远端分支的跟踪关系,可以简化git命令,比如git pull和git push时可以省略分支名称,git自动识别跟踪关系并将代码更新同步到本地或者远端分支

那么设置分支的跟踪关系有以下两种情况:

 

1、新建一个分支时设置跟踪关系

git  checkout -b new_branch_name  [--track] origin/remote_branch_name

--track选项可以省略

 

2、设置已有分支和远端分支的跟踪关系

git  branch -u  origin/remote_branch_name  local_branch_name

注意:-u选项是--set-upstream-to的简写,因此上面这条命令可以写作

git  branch --set-upstream-to=origin/remote_branch_name  local_branch_name

local_branch_name可以省略,默认值为当前分支

git diff 本地分支 origin/xxxx 

 

$ git clone --depth 1 https://github.com/dogescript...
$ git remote set-branches origin 'remote_branch_name'
$ git fetch --depth 1 origin remote_branch_name
$ git checkout remote_branch_name

stackoverflow上的一个答案, 个人问题可以解决
https://stackoverflow.com/que...