git創建遠程倉庫
首先到github頁面上創建倉庫(repository)如下:
然後初始化文件夾爲倉庫,並提交到遠程倉庫,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | [root@hxy aa] # git init Initialized empty Git repository in /data/mydata/aa/ .git/ [root@hxy aa] # git add . [root@hxy aa] # git commit -m "first commit" [master (root-commit) 606ee64] first commit 2 files changed, 293 insertions(+), 0 deletions(-) create mode 100644 README create mode 100644 README.md [root@hxy aa] # git remote add origin [email protected]:mghxy123/test.git [root@hxy aa] # git push -u origin master The authenticity of host 'github.com (192.30.255.113)' can't be established. RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb: df :a6:48. Are you sure you want to continue connecting ( yes /no )? yes Warning: Permanently added 'github.com,192.30.255.113' (RSA) to the list of known hosts. Permission denied (publickey). fatal: The remote end hung up unexpectedly [root@hxy aa] # git push -u origin master Warning: Permanently added the RSA host key for IP address '192.30.255.112' to the list of known hosts. Counting objects: 4, done . Delta compression using up to 2 threads. Compressing objects: 100% (3 /3 ), done . Writing objects: 100% (4 /4 ), 1.63 KiB, done . Total 4 (delta 0), reused 0 (delta 0) * [new branch] master -> master Branch master set up to track remote branch master from origin. [root@hxy aa] # |
我這裏報錯的原因是因爲沒有把公鑰放到github裏面導致的,報錯後我把公鑰放到了github裏面就沒問題了.
或者通過克隆的方式把庫拉下來,然後再提交如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | [root@hxy aa] # git clone [email protected]:mghxy123/test1.git Initialized empty Git repository in /data/mydata/aa/test1/ .git/ warning: You appear to have cloned an empty repository. [root@hxy aa] # ls -al 28 drwxr-xr-x 3 root root 4096 8 22 16:10 . drwxr-xr-x 5 root root 4096 8 22 14:56 .. -rw-r--r-- 1 root root 9657 8 22 14:57 README -rw-r--r-- 1 root root 7 8 22 15:55 README.md drwxr-xr-x 3 root root 4096 8 22 16:10 test1 [root@hxy aa] # cd test1/ [root@hxy test1] # ls -al 12 drwxr-xr-x 3 root root 4096 8 22 16:10 . drwxr-xr-x 3 root root 4096 8 22 16:10 .. drwxr-xr-x 7 root root 4096 8 22 16:10 .git [root@hxy test1] # cp ../README ./ [root@hxy test1] # ls README [root@hxy test1] # ls README [root@hxy test1] # git add . [root@hxy test1] # git commit -m "add README" [master (root-commit) d57f411] add README 1 files changed, 292 insertions(+), 0 deletions(-) create mode 100644 README [root@hxy test1] # git push No refs in common and none specified; doing nothing. Perhaps you should specify a branch such as 'master' . error: failed to push some refs to '[email protected]:mghxy123/test1.git' [root@hxy test1] # git push origin master Counting objects: 3, done . Delta compression using up to 2 threads. Compressing objects: 100% (2 /2 ), done . Writing objects: 100% (3 /3 ), 1.60 KiB, done . Total 3 (delta 0), reused 0 (delta 0) * [new branch] master -> master |
我這裏的報錯是因爲首次提交需要 git commit -m "add README"
不然就是我這樣的報錯
我常用的一些git命令:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | git簡寫 git st # git status git ci # git commit git br # git branch git co # git checkout git mg # git merge git line # git log --oneline git init #初始化gi庫 git add file #增加庫文件,也可以用git add .意思爲添加所有 git commit -m "update" #提交併註釋 git push origin -u master master #首次推送到遠程倉庫 git push origin test #推送到test分支 git branch #查看本地分支 git branch -a #查看所有分支 git branch test #創建test分支,但不切換 git checkout -b test #如果test分支存在就切換到test分支,如果不存在就創建並且換到test分支 git checkout test #切換到test分支 git push #推送 git push -u origin /test #提交本地庫到遠程test分支 git push origin test : test #提交本test分支作爲test分支 git push origin test :master #提交本test分支作爲master分支 git branch -d test #刪除本地分支 git push origin --delete crond #刪除遠程分支 git push origin : test #刪除遠程分支 git branch -m | -M oldbranch newbranch #重命名分支,如果newbranch名字分支已經存在,則需要使用-M強制重命名,否則,使用-m進行重命名 git branch -d | -D test #刪除本地test分支 git branch -d -r test #刪除遠程分支 git remote - v #查看遠程倉庫 git remote add [name] [url] #添加遠程倉庫 git remote rm [name] #刪除遠程倉庫 git remote set -url --push[name][newUrl] #修改遠程倉庫 |
下面是網上找的git常用命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | 查看、添加、提交、刪除、找回,重置修改文件 git help < command > # 顯示command的help git show # 顯示某次提交的內容 git show $id git co -- < file > # 拋棄工作區修改 git co . # 拋棄工作區修改 git add < file > # 將工作文件修改提交到本地暫存區 git add . # 將所有修改過的工作文件提交暫存區 git rm < file > # 從版本庫中刪除文件 git rm < file > --cached # 從版本庫中刪除文件,但不刪除文件 git reset < file > # 從暫存區恢復到工作文件 git reset -- . # 從暫存區恢復到工作文件 git reset --hard # 恢復最近一次提交過的狀態,即放棄上次提交後的所有本次修改 git ci < file > git ci . git ci -a # 將git add, git rm和git ci等操作都合併在一起做 git ci -am "some comments" git ci --amend # 修改最後一次提交記錄 git revert <$ id > # 恢復某次提交的狀態,恢復動作本身也創建次提交對象 git revert HEAD # 恢復最後一次提交的狀態 查看文件 diff git diff < file > # 比較當前文件和暫存區文件差異 git diff git diff <id1><id2> # 比較兩次提交之間的差異 git diff <branch1>..<branch2> # 在兩個分支之間比較 git diff --staged # 比較暫存區和版本庫差異 git diff --cached # 比較暫存區和版本庫差異 git diff --stat # 僅僅比較統計信息 查看提交記錄 git log git log < file > # 查看該文件每次提交記錄 git log -p < file > # 查看每次詳細修改內容的diff git log -p -2 # 查看最近兩次詳細修改內容的diff git log --stat #查看提交統計信息 Git 本地分支管理 查看、切換、創建和刪除分支 git br -r # 查看遠程分支 git br <new_branch> # 創建新的分支 git br - v # 查看各個分支最後提交信息 git br --merged # 查看已經被合併到當前分支的分支 git br --no-merged # 查看尚未被合併到當前分支的分支 git co <branch> # 切換到某個分支 git co -b <new_branch> # 創建新的分支,並且切換過去 git co -b <new_branch> <branch> # 基於branch創建新的new_branch git co $ id # 把某次歷史提交記錄checkout出來,但無分支信息,切換到其他分支會自動刪除 git co $ id -b <new_branch> # 把某次歷史提交記錄checkout出來,創建成一個分支 git br -d <branch> # 刪除某個分支 git br -D <branch> # 強制刪除某個分支 (未被合併的分支被刪除的時候需要強制) 分支合併和rebase git merge <branch> # 將branch分支合併到當前分支 git merge origin /master --no-ff # 不要Fast-Foward合併,這樣可以生成merge提交 git rebase master <branch> # 將master rebase到branch, 相當於:git co <branch> && git rebase master && git co master && git merge <branch> Git補丁管理(方便在多臺機器上開發同步時用) git diff > .. /sync .patch # 生成補丁 git apply .. /sync .patch # 打補丁 git apply --check .. /sync .patch #測試補丁能否成功 Git暫存管理 git stash # 暫存 git stash list # 列所有stash git stash apply # 恢復暫存的內容 git stash drop # 刪除暫存區 Git遠程分支管理 git pull # 抓取遠程倉庫所有分支更新併合併到本地 git pull --no-ff # 抓取遠程倉庫所有分支更新併合併到本地,不要快進合併 git fetch origin # 抓取遠程倉庫更新 git merge origin /master # 將遠程主分支合併到本地當前分支 git co --track origin /branch # 跟蹤某個遠程分支創建相應的本地分支 git co -b <local_branch> origin/<remote_branch> # 基於遠程分支創建本地分支,功能同上 git push # push所有分支 git push origin master # 將本地主分支推到遠程主分支 git push -u origin master # 將本地主分支推到遠程(如無遠程主分支則創建,用於初始化遠程倉庫) git push origin <local_branch> # 創建遠程分支, origin是遠程倉庫名 git push origin <local_branch>:<remote_branch> # 創建遠程分支 git push origin :<remote_branch> #先刪除本地分支(git br -d <branch>),然後再push刪除遠程分支 Git遠程倉庫管理 git remote - v # 查看遠程服務器地址和倉庫名稱 git remote show origin # 查看遠程服務器倉庫狀態 git remote add origin git@ github:robbin /robbin_site .git # 添加遠程倉庫地址 git remote set -url origin git@ github.com:robbin /robbin_site .git # 設置遠程倉庫地址(用於修改遠程倉庫地址) git remote rm <repository> # 刪除遠程倉庫 創建遠程倉庫 git clone --bare robbin_site robbin_site.git # 用帶版本的項目創建純版本倉庫 scp -r my_project.git git@ git.csdn.net:~ # 將純倉庫上傳到服務器上 mkdir robbin_site.git && cd robbin_site.git && git --bare init # 在服務器創建純倉庫 git remote add origin git@ github.com:robbin /robbin_site .git # 設置遠程倉庫地址 git push -u origin master # 客戶端首次提交 git push -u origin develop # 首次將本地develop分支提交到遠程develop分支,並且track git remote set - head origin master # 設置遠程倉庫的HEAD指向master分支 也可以命令設置跟蹤遠程庫和本地庫 git branch -- set -upstream master origin /master git branch -- set -upstream develop origin /develop |
下面是借鑑的博客
http://www.cnblogs.com/x_wukong/p/5408275.html
http://blog.csdn.net/xiaoputao0903/article/details/23933589
http://www.cnblogs.com/cspku/articles/Git_cmds.html
本文出自 “Forand” 博客,請務必保留此出處http://853056088.blog.51cto.com/12966870/1958420