Git的簡單使用
1.Git的介紹
Git是一款免費、開源的分佈式版本控制系統,用於敏捷高效地處理任何或小或大的項目。
Git是一個開源的分佈式版本控制系統,可以有效、高速的處理從很小到非常大的項目版本管理。Git 是 Linus Torvalds 爲了幫助管理 Linux 內核開發而開發的一個開放源碼的版本控制軟件。
Torvalds 開始着手開發 Git 是爲了作爲一種過渡方案來替代 BitKeeper,後者之前一直是 Linux 內核開發人員在全球使用的主要源代碼工具。開放源碼社區中的有些人覺得BitKeeper 的許可證並不適合開放源碼社區的工作,因此 Torvalds 決定着手研究許可證更爲靈活的版本控制系統。儘管最初 Git 的開發是爲了輔助 Linux 內核開發的過程,但是我們已經發現在很多其他自由軟件項目中也使用了 Git。例如 很多 Freedesktop 的項目遷移到了 Git 上。
2.Git的安裝
如果是window就要到網上下載一個Git For Window,可到下述網站下載:https://git-for-windows.github.io/ 點擊 Download,跳轉到 Github ,下載對應安裝包。然後使用Git bash來使用Git命令。
如果你的系統是Linux的話,直接打開shell輸入安裝:
sudo apt-get install git
3.Git的用法
1.創建代碼倉庫
配置全局簽名:
git config --global user.name "xxx"
git config --global user.email "[email protected]"
接下來使用命令生成SSH Keys:
ssh-keygen -t rsa -C “[email protected]”
創建本地倉庫:
git init
接下來會產生一個.git文件夾,默認是隱藏的,使用ls -al可看。
2.提交本地代碼
創建完代碼倉庫,接下來說下如何提交代碼,我們是先用add命令把要提交的內容都加進來,然後commit纔是真的去執行提交操作!命令例子如下,你可以一次次慢慢添加,當然也可以全部提交,直接git add .即可完成! 我們現在工程目錄下創建一個readme.txt的文件試試,隨便寫點東西,然後依次輸入下述指令:
git add readme.txt
git commit -m "Wrote a readme file"
-m的意思是提交的時候價格記錄一下變動
3.與遠程github賬戶同步
先配置SSH keys,進到本地的c:/users/xxxx/.ssh/找到id_rsa.pub,然後在github上添加SSH keys,把裏面的公鑰複製上去。
用自己賬號在github網站上新建一個repository名字爲rname,你的賬號名字爲username,然後用該命令與遠程倉庫建立同步連接,使用一次後以後不用在同步
git remote add origin [email protected]:username/name.git
接下來使用命令將本地的版本庫推送到github的倉庫上。
git push -u origin master
-u是第一次推送的時候加上的,以後的推送不用該參數
如果這裏報錯
To [email protected]:t617/Android.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to '[email protected]:t617/Android.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
則是因爲建立resposity的時候初始化了README.md與本地不一致導致的。這時候用命令:
git pull --rebase origin master
或者
git push --force origin master
4.克隆代碼到本地
使用該命令來克隆github上的庫到本地的位置
git clone git://github.com/username/rname.git
5. 刪除遠程庫
git rm -r --cached .
接下來跟git add的步驟一樣
git commit -m "delete"
然後push
git push
常見錯誤:
fatal: remote origin already exists.
解決方法:
git remote rm origin