github入门

1、先了解css

相比CVS\SVN优点:html

- 支持离线开发,离线Repository
- 强大的分支功能,适合多个独立开发者协做
- 速度快java

github 本地有仓库,储存着全部repository的历史;git

 本地有缓冲区,指向你最近一次提交后的结果,改动一个文件,就是拿你改动的文件和缓冲区的文件进行 进行差别化比较 。github

2、注册与安装
一、注册 https://github.com/
三、安装完成后,桌面出现两个图标
 
四、登陆
五、在Git Shell中设定本地用户信息
git config --global user.name "Your Name Here"
# Sets the default name for git to use when you commit
git config --global user.email "your_email@example.com"
# Sets the default email for git to use when you commit
3、初始化和创建项目
一、在 网站上建立一repository
二、本地经过命令行 建立
     
    $ makdir ~/hello-world    //建立一个项目hello-world
    $ cd ~/hello-world       //打开这个项目
    $ git init             //初始化 
    $ touch README //建立说明文件
    $ git add README        //更新README文件
    $ git commit -m 'first commit'     //提交更新,并注释信息“first commit”
  $ git remote add origin https://github.com/defnngj/Hello-World.git  //链接远程github项目
    $ git push -u origin master     //将本地项目更新到github项目上去
 
具体以下
Windows PowerShell
版权全部 (C) 2009 Microsoft Corporation。保留全部权利。
 
> mkdir ~/Test123
 
 
    目录: C:\Users\sprying
 
 
Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----         2013/8/23      8:01            Test123
 
 
> git init
  Initialized empty Git repository in C:/Users/sprying/Documents/GitHub/.git/
> touch README
> git add README
> git commit -m 'first commit'
  [master (root-commit) 730016b] first commit
   1 file changed, 0 insertions(+), 0 deletions(-)
   create mode 100644 README
> git remote add origin https://github.com/sprying/Test123.git
> git remote -v
  origin  https://github.com/sprying/Test123.git (fetch)
  origin  https://github.com/sprying/Test123.git (push)
> git push origin master
  Counting objects: 3, done.
  Writing objects: 100% (3/3), 206 bytes | 0 bytes/s, done.
  Total 3 (delta 0), reused 0 (delta 0)
  To https://github.com/sprying/Test123.git
   * [new branch]      master -> master

 

 
4、Ps
一、能够经过图形化界面来查看历史
 
2.下载一个repository相关命令
mkdir jekyll_demo
cd jekyll_demo
git init
 
git checkout --orphan gh-pages
 
  
Cloning into 'XXXX'...
remote: Counting objects: 29510, done.
remote: Compressing objects: 100% (8313/8313), done.
remote: Total 29510 (delta 21676), reused 28255 (delta 20584)
Receiving objects: 100% (29510/29510), 14.78 MiB | 157.00 KiB/s, done.
Resolving deltas: 100% (21676/21676), done.
 
git add helloworld.naxsu 进行添加到本地缓冲区
git add . 添加当前目录下的全部文件
git add *.c 添加以 .c 为后缀的文件
git add index.jsp        添加指定文件
 
git commit -m 'first commit' 提交到本地仓库
 
建立一个指向你repository的origin
 
 
三、 相关命令详细中文说明
-------2013年9月13日0:36:05添加-----

1) 远程仓库相关命令web

查看远程仓库:$ git remote -vshell

添加远程仓库:$ git remote add [name] [url]vim

删除远程仓库:$ git remote rm [name] 如 git remote rm origin 删除远程的origin链接app

修改远程仓库:$ git remote set-url --push [name] [newUrl]webapp

拉取远程仓库:$ git pull [remoteName] [localBranchName]

推送远程仓库:$ git push [remoteName] [localBranchName]

* 若是想把本地的某个分支test提交到远程仓库,并做为远程仓库的master分支,或者做为另一个名叫test的分支,以下:

$ git push origin test:master         // 提交本地test分支做为远程的master分支

$ git push origin test:test              // 提交本地test分支做为远程的test分支

2)分支(branch)操做相关命令

查看本地分支:$ git branch

查看远程分支:$ git branch -r (若是仍是看不到就先 git fetch origin 先)

建立本地分支:$ git branch [name] ----注意新分支建立后不会自动切换为当前分支

切换分支:$ git checkout [name]

建立新分支并当即切换到新分支:$ git checkout -b [name]

直接检出远程分支:$ git checkout -b [name] [remoteName] (如:git checkout -b myNewBranch origin/dragon)

删除分支:$ git branch -d [name] ---- -d选项只能删除已经参与了合并的分支,对于未有合并的分支是没法删除的。若是想强制删除一个分支,可使用-D选项

合并分支:$ git merge [name] ----将名称为[name]的分支与当前分支合并

合并最后的2个提交:$ git rebase -i HEAD~2 ---- 数字2按需修改便可(若是需提交到远端$ git push -f origin master 慎用!

建立远程分支(本地分支push到远程):$ git push origin [name]

删除远程分支:$ git push origin :heads/[name] 或 $ git push origin :[name] 

* 建立空的分支:(执行命令以前记得先提交你当前分支的修改,不然会被强制删干净没得后悔)

$ git symbolic-ref HEAD refs/heads/[name]

$ rm .git/index

$ git clean -fdx

3)版本(tag)操做相关命令

查看版本:$ git tag

建立版本:$ git tag [name]

删除版本:$ git tag -d [name]

查看远程版本:$ git tag -r

建立远程版本(本地版本push到远程):$ git push origin [name]

删除远程版本:$ git push origin :refs/tags/[name]

合并远程仓库的tag到本地:$ git pull origin --tags

上传本地tag到远程仓库:$ git push origin --tags

建立带注释的tag:$ git tag -a [name] -m 'yourMessage'

4) 子模块(submodule)相关操做命令

添加子模块:$ git submodule add [url] [path]

    如:$ git submodule add git://github.com/soberh/ui-libs.git src/main/webapp/ui-libs

初始化子模块:$ git submodule init  ----只在首次检出仓库时运行一次就行

更新子模块:$ git submodule update ----每次更新或切换分支后都须要运行一下

删除子模块:(分4步走哦)

1) $ git rm --cached [path]

2) 编辑“.gitmodules”文件,将子模块的相关配置节点删除掉

3) 编辑“ .git/config”文件,将子模块的相关配置节点删除掉

4) 手动删除子模块残留的目录

5)忽略一些文件、文件夹不提交

echo "class1" > class1.class :添加元素
echo "java1" > java1.java
echo "class1.class" >.gitignore :添加东东到提交忽视文件定义中
vim .gitignore :添加自身到gitignore
cat .gitignore :展现文件内容
class1.class
.gitignore
 
或者

在仓库根目录下建立名称为“.gitignore”的文件,写入不须要的文件夹名或文件,每一个元素占一行便可,如

target

bin

*.db

 
6)查看历史记录
git log :显示全部的提交( commit )记录
git whatchanged :显示的信息比 git-log 更详细一些,能够显示具体的文件名
 
7)其它命令
查看相关设置
git config --list
git config user.name 
 
获取对config命令的手册页帮助
git help config
 
 
echo "hello world" >> helloworld.naxsu
ls *.naxsu
helloworld.naxsu
 
git status
 
  后悔药

删除当前仓库内未受版本管理的文件:$ git clean -f

恢复仓库到上一次的提交状态:$ git reset --hard

回退全部内容到上一个版本:$ git reset HEAD^

回退a.py这个文件的版本到上一个版本:$ git reset HEAD^ a.py

回退到某个版本:$ git reset 057d 

 

将本地的状态回退到和远程的同样:$ git reset –hard origin/master  

 

向前回退到第3个版本:$ git reset –soft HEAD~3

 

四、如何将本地一文件夹(已有许多文件)加入github控制
打开shell
git init
git add . 
git commit -m ''
git checkout -b gh-pages //新建分支并设置为当前
git branch-r //查看远程的分支,remote命令已执行,但仍是未空,直到push以后
git remote //查看远程
     // origin 发现只显示这个,且没法删除,查了缘由,貌似全局设置文件etc/gitconfig有问题,我回避后以下设定,
git remote rm origin // 报错 could not remove config section 'remote.origin'。既然没法删除,就只有在已有基础上修正了。
git remote set-url
git remote -v //查看远程name和url
git push origin gh-pages
 
至于远程上,只是创建了空的repository
若是里面有内容,先要拿下来
git pull origin gh-pages 再上传
 

------------------------------------关于可能出现的错误----------------------------------

1.在执行

git remote addorigin git@github.com:defnngj/hello-world.git

错误提示:fatal: remote origin already exists.

解决办法:

git remote rm origin

 

若是输入$ git remote rm origin 仍是报错的话,error: Could not remove config section 'remote.origin'. 

咱们须要修改gitconfig文件的内容

找到你的github的安装路径,如C:\Users\ASUS\AppData\Local\GitHub\PortableGit_ca477551eeb4aea0e4ae9fcd3358bd96720bb5c8\etc

找到一个名为gitconfig的文件,打开它把里面的[remote "origin"]那一行删掉就行了!

 

参考文章

https://help.github.com/articles/set-up-git

http://rogerdudler.github.io/git-guide/index.zh.html

http://www.ruanyifeng.com/blog/2012/08/blogging_with_jekyll.html

经常使用命令主要来自 http://rongjih.blog.163.com/blog/static/335744612010112562833316/