文章目录
  1. 1. 把代码推送到远程仓库
    1. 1.1. 简单的push
    2. 1.2. 删除远端分支所有的代码
    3. 1.3. 创建远程分支
    4. 1.4. 删除远程分支
    5. 1.5. 合并远程分支
  2. 2. 一般企业使用的分支管理方法
  3. 3. git标签
  4. 4. git子模块
  5. 5. git使用http代理服务器
    1. 5.1. 如果你有翻墙的代理地址的话
    2. 5.2. shadowsocks翻墙
  6. 6. 推荐阅读

把代码推送到远程仓库

简单的push

就是在git addgit commit之后执行

1
git push origin master

这样就把代码提交到远程master分支上了

删除远端分支所有的代码

1
2
3
git rm *
git commit -m"DEL:delete all code"
git push origin master

其实就是在的git仓库删除所有代码,然后再push,但是代码其实并没有真正删除,因为可以使用git reset --hard来恢复到没删除之前的版本

创建远程分支

就是在git addgit commit之后执行

1
git push origin master:from_author

这样就把本地master分支上的代码push到远程from_author分支上。就算远程分支from_author原本不存在也没关系,git会自动创建远程分支。
使用

1
git push origin from_author:from_author

是把本地from_author分支上的代码push到远程from_author分支上

删除远程分支

直接执行

1
git push origin :from_author

冒号右边是from_author,左边留空,就是删除远程分支

合并远程分支

远程分支一般来说只能push,那么策略就是在本地先把分支合并,再push到远程分支上
比如说有四个分支,本地master,本地from_author,origin/master,origin/from_author

在merge之前,先保证本地的master和from_author的代码是最新的

1
2
3
4
git checkout master
git pull origin master
git checkout from_author
git pull origin from_author

先切换到本地master分支

1
git checkout master

merge本地from_author分支

1
git merge from_author

push到origin/master

1
git push origin master

一般企业使用的分支管理方法

1,最初的代码分支有两个:master或者stable,dev。
2,代码部署在git服务器上
3,在dev分支下checkout出每个员工的专属代码分支:from_alice,from_bob等等
4,员工能够pull master分支的代码和dev分支的代码,但是只能够push到自己的远程分支上面
5,每周的固定一天,比如说星期三,在分支管理QQ群当中提交将自己的远程分支合并到dev分支上的合并申请。
6,QQ群的管理员,同时也是唯一有push到dev权限的管理员,在他的本地将代码merge之后,push到dev分支。
7,进过内部测试团队测试无bug之后,向CTO提交push到master分支的申请,CTO批准后,从dev分支当中选取一个版本push到master分支。

综上,就是员工有pull的权限,但是push的权限受到严格限制。

使用gitolite可以做权限配置。但是这个目前我没有做研究。

git标签

打上tagname的标签

1
git tag <tagname>

查看标签

1
git tag

删除标签

1
git tag -d <tagname>

git子模块

就是说一个project依赖于其他的project,一种方法是用文档的方式把每个依赖的project记下来,然后开发人员一个一个clone下来。更简单的方法是使用git submodule

添加依赖

1
git submodule add git@github.com:username/lib1.git

可以发现新增了一个.gitmodules文件。这是一个配置文件,保存了项目 URL 和你拉取到的本地子目录。

然后当你把你的这个仓库push上去之后,然后另外一个开发人员来了,从git仓库下面clone了这个项目;
但是clone下来的代码上面并没有外部项目的代码,接下来就是这样做的:

1
2
git submodule init
git submodule update

用这两条命令之后就会发现外部项目的代码已经出现了!
其实这个git submodule就是一个高级一点的cp命令,就是把外部文件clone下来
记住:这个时候git并不在任何分支上面,需要切换回一个分支:

1
git checkout master

我也正在看这个git submodule使用完整教程

git使用http代理服务器

大天朝的墙太厚了!
虽然没有封github,但是对github限速了,在github上clone一个项目速度简直太慢了。

如果你有翻墙的代理地址的话

在命令行执行

1
2
3
git config --global http.proxy http://proxyuser:proxypwd@proxy.server.com:8080

git config --global https.proxy https://proxyuser:proxypwd@proxy.server.com:8080

这样就设置了http和https的代理服务器。
用一下命令来检查是否生效:

1
2
3
git config --global --get http.proxy

git config --global --get https.proxy

如果想取消掉,那么

1
2
3
git config --global --unset http.proxy

git config --global --unset https.proxy

以上做法仅仅只是让git使用代理,如果想要让wget和curl也能使用代理的话,执行以下命令:

1
2
3
export http_proxy=http://proxyuser:proxypwd@proxy.server.com:8080

export https_proxy=https://proxyuser:proxypwd@proxy.server.com:8080

如果要取消代理设置,可以:

1
2
3
unset http_proxy

unset https_proxy

还有一点要注意的是,使用 http_proxy 和 https_proxy 只对 HTTP 和 HTTPS 请求有效,所以当你 ping www.google.com 的时候如果 ping 不通的话,也就没什么大惊小怪的了。

shadowsocks翻墙

但是如果使用的是shadowsocks,由于shadowsocks没有用户名,那么只有用个方法来配置http_proxy。

首先安装polipo

1
sudo apt-get -y install polipo

ubuntu下启动shadowsocks,由于会打印出大量的日志信息,使用/dev/null让世界变得清净一点。

1
/opt/shadowsocks/start.sh 2>/dev/null & 

设置http服务器

1
polipo socksParentProxy=localhost:1080 & 

接下来是git设置

1
2
git config --global http.proxy 127.0.0.1:8123
git config --global https.proxy 127.0.0.1:8123

借鉴了Convert Shadowsocks into an HTTP proxy这篇文章

推荐阅读

文章目录
  1. 1. 把代码推送到远程仓库
    1. 1.1. 简单的push
    2. 1.2. 删除远端分支所有的代码
    3. 1.3. 创建远程分支
    4. 1.4. 删除远程分支
    5. 1.5. 合并远程分支
  2. 2. 一般企业使用的分支管理方法
  3. 3. git标签
  4. 4. git子模块
  5. 5. git使用http代理服务器
    1. 5.1. 如果你有翻墙的代理地址的话
    2. 5.2. shadowsocks翻墙
  6. 6. 推荐阅读