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/

相關文章
相關標籤/搜索