1 Linux下Git和GitHub環境的搭建
- 安裝Git, 使用命令sudo apt-get install git
- 創建GitHub帳號
- 生成ssh key,使用命令 ssh-keygen -t rsa -C "your_email@youremail.com",your_email是你的email
- 回到github,進入Account Settings,左邊選擇SSH Keys,Add SSH Key,粘貼key。
- 測試ssh key是否成功,使用命令ssh -T git@github.com,如果出現You’ve successfully authenticated, but GitHub does not provide shell access ,這就表示已成功連上github。
- 配置Git的配置文件:配置用戶名:git config --global user.name "your name" ,配置email:git config --global user.email "your email"
2 利用Git從本地上傳到GitHub
- 進入要所要上傳文件的目錄, 輸入命令 git init
- 創建一個本地倉庫origin,使用命令 git remote add origin git@github.com:your_name/yourRepo.git,your_name是你的GitHub的用戶名,yourRepo是你要上傳到GitHub的倉庫
- 比如你要添加一個文件xxx到本地倉庫,使用命令 git add xxx,可以使用 git add . 自動判斷添加哪些文件
- 然后把這個添加提交到本地的倉庫,使用命令 git commit -m "說明這次的提交"
- 最后把本地倉庫origin提交到遠程的GitHub倉庫,使用命令 git push origin master
3 從GitHub克隆項目到本地
- 到GitHub的某個倉庫,然后復制右邊的那個(HTTPS clone url)
- 回到要存放的目錄下,使用命令 git clone https://github.com/your_name/yourRepo.git,your_name是你的GitHub的用戶名,yourRepo是你要clone的倉庫
- 如果本地的版本不是最新的,可以使用命令 git fetch origin,origin是本地倉庫
- 把更新的內容合並到本地分支,可以使用命令 git merge origin/master
- 如果你不想手動去合並,那么你可以使用: git pull <本地倉庫> master 這個命令來拉去最新版本並自動合並
4 GitHub的分支管理
創建分支
- 創建一個本地分支: git branch <新分支名字>
- 將本地分支同步到GitHub上面: git push <本地倉庫名> <新分支名>
- 切換到新建立的分支: git checkout <新分支名>
- 為你的分支加入一個新的遠程端: git remote add <遠程端名字> <地址>
- 查看當前倉庫有幾個分支: git branch
刪除分支
- 從本地刪除一個分支: git branch -d <分支名稱>
- 同步到GitHub上面刪除這個分支: git push <本地倉庫名> :<GitHub端分支>
5 常見錯誤
如果出現報錯為ERROR: Repository not found.fatal: The remote end hung up unexpectedly則代表你的 origin 的url 鏈接有誤,可能是創建錯誤,也可能是這個 git@github.com:xxx/new-project.git url 指定不正確。重新創建。