GitHub常用命令工作中的使用

  • Git克隆一个浅分支(depth=1)如何获取其他的分支或tag
  • 1、git clone $url --depth 1
  • 2、git pull --depth=1 origin $远程tag:$本地tag
  • 3、查看远程tag: git ls-remote
  • 4、tag转branch: git checkout $tag名字 -b $分支名字

0、克隆分支

git clone <url> -b <分支名称>  --depth 1

eg.  git clone https://xxx.git -b master

##clone Tag
git clone -b '3.28.0.20170327' --single-branch --depth 1 https://github.com/huifenqi/payment-2.0.git

1、git push命令用于将本地分支的更新,推送到远程主机

git push <远程主机名> <本地分支名>:<远程分支名>

参考地址:http://www.yiibai.com/git/git_push.html

2、为了便于管理,Git要求每个远程主机都必须指定一个主机名。git remote命令就用于管理主机名。

$ git remote    ##列出所有远程主机
$ git remote -v ##使用-v选项,可以参看远程主机的网址。
$ git remote show <主机名>  ##可以查看该主机的详细信息。
$ git remote add <主机名> <网址>    ##命令用于添加远程主机

参考地址:http://www.yiibai.com/git/git_remote.html

2-1 Git使用Tag

$ git tag #查看
$ git tag <tag_name> #eg.  git tag v1.1
$ git push origin --tags # 推送线上
$ git push origin :refs/tags/<标签名>   ##删除远程标签
$ git tag -d <标签名> ##删除本地标签

3、分支管理

git branch new_branch   ##创建分支
git checkout new_branch ##切换分支
git checkout -b test_branch ##创建和切换分支的快捷方式
git branch -D test_branch   ##删除分支  
git branch -m new_branch wchar_support  ##重命名分支
git merge origin/wchar_support  ##合并两个分支(在 master上操作此时合并到master分支上)

#设置分支跟踪远程分支
$ git branch -u origin/master
Branch master set up to track remote branch master from origin.

##重订分支
#########################
Git 的 rebase命令的一个分支合并的命令,但不同的是,它修改提交的顺序

Git merge命令,试图把从其他分支提交当前的本地分支的HEAD上。例如
本地的分支已经提交A-> B-> C-> D和合并分支已提交A-> B-> X> Y,
则Git合并将当前转换像这样的本地分行A-> B-> C-> D-> X-> Y

Git 的rebase命令试图找到当前的本地分支和合并分支之间的共同祖先。
然后把修改提交的顺序,在当前的本地分支提交中的本地分支。
例如,如果当地的分支已提交A-> B-> C-> D和合并分支已提交A-> B-> X-> Y,
Git衍合的类似A-> B转换成当前的本地分支A−>B−>X−>Y−>C−>D

当多个开发人员在一个单一的远程资源库的工作,你不能在远程仓库提交修改订单。
在这种情况下,可以使用变基操作把本地提交的远程仓库之上的提交,可以推送这些变化。
#########################

参考地址: http://www.yiibai.com/git/git_managing_branches.html

4、其他命令

git status -s   ##以精简的方式显示文件状态
###########################
A:新文件第一次被添加进版本管理 
M:红色为修改过未被添加进暂存区的,绿色为已经添加进暂存区的
###########################
git diff <file> ##比较修改的不同之处
git log -1  ##(参数是 横杠数字1)
git show <commit id>    ##审核查看变化
git branch -a   ##查看所有的分支,包括远程分支

5、修改远程仓库地址

git remote set-url origin [url]
eg. git remote set-url origin https://xxx.git 
$ git remote set-url origin https://github.com/hfq-shimanqiang/payment-2.0.git

参考地址:http://www.cnblogs.com/merray/p/5698331.html

参考地址中,最后一条 命令顺序不正确啊!!看我的

参考地址2 : http://blog.csdn.net/u013260551/article/details/51317777

6-0、GitHub pull requests说明

base fork 是你将要pull requests的上游仓库
head fork 是新模块或者新特性分支仓库

6-1、如何同步 Github fork 出来的分支,跟踪上游仓库变化

##添加并指向上游仓库
'git remote add upstream <上游仓库的地址>'
eg. git remote add upstream https://github.com/huifenqi/payment-2.0.git
#################################
$ git remote -v
origin  https://github.com/hfq-shimanqiang/payment-2.0.git (fetch)
origin  https://github.com/hfq-shimanqiang/payment-2.0.git (push)

$ git remote add upstream https://github.com/huifenqi/payment-2.0.git

$ git remote -v
origin  https://github.com/hfq-shimanqiang/payment-2.0.git (fetch)
origin  https://github.com/hfq-shimanqiang/payment-2.0.git (push)
upstream        https://github.com/huifenqi/payment-2.0.git (fetch)
upstream        https://github.com/huifenqi/payment-2.0.git (push)

##################################

##从上游仓库 fetch 分支和提交点,传送到本地,并会被存储在一个本地分支 upstream/master
'git fetch upstream'
##################################
$ git fetch upstream
From https://github.com/huifenqi/payment-2.0
 * [new branch]      2017031001 -> upstream/2017031001
 * [new branch]      20170321   -> upstream/20170321
 * [new branch]      master     -> upstream/master
 
$ git branch -a
* 2017031001
  20170321
  develop
  master
  remotes/origin/20170321
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/upstream/2017031001
  remotes/upstream/20170321
  remotes/upstream/master

#################################

##切换到本地分支master,合并代码
'git checkout master'
'git merge upstream/master'

##如果想更新到 GitHub 的 fork 上,
直接 'git push origin master' 就好了

参考地址:https://gaohaoyang.github.io/2015/04/12/Syncing-a-fork/

参考地址:http://jinlong.github.io/2015/10/12/syncing-a-fork/

6-2、git远程删除分支后,本地git branch -a 依然能看到的解决办法

git remote show origin,可以查看remote地址,远程分支,还有本地分支与之相对应关系等信息
根据提示,使用 git remote prune origin 命令
$ git remote prune origin

参考地址:http://blog.csdn.net/qq_16885135/article/details/52777871

7、使用操作(8)之后:命令总结

#从远程分支checkout并且跟踪
$ git checkout --track remotes/origin/2017031001

#删除远程分支,本地分支删除git branch -D <barnch_name>
$ git push origin --delete 2017031001


8、使用操作

###########展示origin主机的信息################
$ git remote show origin
* remote origin
  Fetch URL: https://github.com/hfq-shimanqiang/payment-2.0.git
  Push  URL: https://github.com/hfq-shimanqiang/payment-2.0.git
  HEAD branch: master
  Remote branches:
    2017031001 tracked
    20170321   tracked
    master     tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local refs configured for 'git push':
    20170321 pushes to 20170321 (up to date)
    master   pushes to master   (up to date)
    
    
###########查看具体的远程分支#############
$ git branch -a
* 20170321
  develop
  master
  remotes/origin/2017031001
  remotes/origin/20170321
  remotes/origin/HEAD -> origin/master
  remotes/origin/master


##########删除远程分支################竟然发生了错误??why
$ git push origin --delete remotes/origin/2017031001
error: unable to delete 'remotes/origin/2017031001': remote ref does not exist
error: failed to push some refs to 'https://github.com/hfq-shimanqiang/payment-2.0.git'
####继续操作
$ git branch -r
  origin/2017031001
  origin/20170321
  origin/HEAD -> origin/master
  origin/master
######难道是因为本地和github上不一致吗?
$ git pull
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/<branch> 20170321

###按照提示执行一下###不行啊?
$ git branch --set-upstream-to=origin/master master
Branch master set up to track remote branch master from origin.

###继续#
$ git checkout --track remotes/origin/2017031001
Switched to a new branch '2017031001'
Branch 2017031001 set up to track remote branch 2017031001 from origin.

$ git branch
* 2017031001
  20170321
  develop
  master

$ git push origin --delete 2017031001
To https://github.com/hfq-shimanqiang/payment-2.0.git
 - [deleted]         2017031001
##########删除成功啦#################################


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值