使用git创建空分支

来源:互联网 发布:淘宝无法打开登录界面 编辑:程序博客网 时间:2024/05/17 22:45

来源:http://www.ooso.net/archives/636

           Creating Project Pages manually

有时候我们需要在Git下创建一个空分支,从头开始Coding —— 这大概是那些重构帝最喜欢的事情。参考了github,才找到一个合适的方法。

怎样安全的进行这项操作

我们需要建一个“孤立”的空分支,为了尽可能的保证数据安全,最好还是重新clone一份代码。

$git clone https://github.com/user/repo.git# Clone our repo# Cloning into 'repo'...# remote: Counting objects: 2791, done.# remote: Compressing objects: 100% (1225/1225), done.# remote: Total 2791 (delta 1722), reused 2513 (delta 1493)# Receiving objects: 100% (2791/2791), 3.77 MiB | 969 KiB/s, done.# Resolving deltas: 100% (1722/1722), done.

开工

这里以github的操作为例,下面试图创建一个名为gh-pages的空分支

$cd repo$ git checkout --orphan gh-pages# 创建一个orphan的分支,这个分支是独立的Switched to a new branch 'gh-pages'git rm -rf .# 删除原来代码树下的所有文件rm '.gitignore'

注意这个时候你用git branch命令是看不见当前分支的名字的,除非你进行了第一次commit。

下面我们开始添加一些代码文件,例如这里新增了一个index.html

$ echo "My GitHub Page" > index.html$ git add .$ git commit -a -m "First pages commit"$ git push origin gh-pages

在commit操作之后,你就可以用git branch命令看到新分支的名字了,然后push到远程仓库。


原创粉丝点击