簡單總結版本在第四節基礎命令入門教程中呈現
零、Git命令大全網址
下面總結一下如何使用github更新文件,溫馨提示:由於windows系統中的git會包含諸多的bug,建議使用Linux系統進行git操作。
一、安裝git客戶端
在 Linux 上安裝
如果你想在 Linux 上用二進制安裝程序來安裝 Git,可以使用發行版包含的基礎軟件包管理工具來安裝。 如果以 Fedora 上為例,你可以使用 yum:
$ sudo yum install git
如果你在基於 Debian 的發行版上,請嘗試用 apt-get:
$ sudo apt-get install git
在 Mac 上安裝
在 Mac 上安裝 Git 有多種方式。 最簡單的方法是安裝 Xcode Command Line Tools。 Mavericks (10.9) 或更高版本的系統中,在 Terminal 里嘗試首次運行 git 命令即可。 如果沒有安裝過命令行開發者工具,將會提示你安裝。
在 Windows 上安裝
在 Windows 上安裝 Git 也有幾種安裝方法。 官方版本可以在 Git 官方網站下載。 打開 http://git-scm.com/download/win,下載會自動開始。 要注意這是一個名為 Git for Windows 的項目(也叫做 msysGit),和 Git 是分別獨立的項目;更多信息請訪問 http://msysgit.github.io/。
另一個簡單的方法是安裝 GitHub for Windows。 該安裝程序包含圖形化和命令行版本的 Git。 它也能支持 Powershell,提供了穩定的憑證緩存和健全的換行設置。 稍后我們會對這方面有更多了解,現在只要一句話就夠了,這些都是你所需要的。 你可以在 GitHub for Windows 網站下載,網址為 http://windows.github.com。
二、GitHub賬戶綁定並更新文件
在本地新建一個非中文的文件名如:Local,並把GitHub相應的項目clone到Local
windows系統通過cmd進入Local文件夾,並開始clone
git clone URL //clone命令
登陸git命令
git config --global user.name "username" git config --global user.email "useremail"
三、 將本地文件與項目的緩沖區綁定並提交
在clone后的文件夾中進行修改,更改后的文件加入GitHub緩沖區
git add *
將文件加入GitHub項目的緩沖區
git commit -m "first" // 將文件加入GitHub項目的緩沖區,並將緩沖區命名為first
將GitHub緩沖區的內容更新到GitHub主頁面
git push // 更新操作
相應的有pull操作,可以把當前文件夾綁定分支(下文有操作)的文件直接copy到本地
git pull // 下載項目最新文件
四、基礎命令入門教程
在熟悉了上述幾種操作之后,匯總一下git的基本操作命令
Git 全局設置:
git config --global user.name "USERNAME" git config --global user.email "USEREMAIL"
Git 倉庫創建:
mkdir Project cd Project git init touch README.md git add README.md (僅對README文檔進行提交或更改)
git add . (對全部的文檔進行提交或更改) git commit -m "first commit" git remote add origin https://github.com/USERNAME/Project.git (如果倉庫已經存在,則不用此步操作) git push -u origin master
Git 修改已經提交代碼:
Step1: git init
Step2: git pull <url> // 這里url是目標倉庫的鏈接
Step3: 本地修改已經下載的文件
Step4: git add .
Step5: git commit -m "Update"
Step6: git push
五、分支操作
git branch NewBranch // 新建一個分支 git checkout NewBranch // 選擇當前分支為NewBranch git branch //查看當前分支 git remote add NewBranch URL // 將新分支與URL鏈接,此處URL指的是GitHub上的項目地址 git push --set-upstream NewBranch NewBranch // 本地更新到GitHub對應項目分支的緩沖區中 git checkout master // 選擇主分支
注意,一定要將本地文件與GitHub倉庫鏈接。
常見Git操作的報錯及處理:
很重要:如果經歷以下錯誤之一,搞定后應重新執行 step 3!
當輸入
git push -u origin master
報錯
origin does not to be a git repository
解決辦法:
git remote add origin URL git push -u origin master
push遠程倉庫時,經常報出下面的錯誤,導致操作失敗,讓我們來看看怎么解決。
To github.com:zwkkkk1/chatroom.git ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to 'git@github.com:zwkkkk1/chatroom.git' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Integrate the remote changes (e.g. hint: 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details. 錯誤:non-fast-forward
遠程倉庫:origin
遠程分支:master
本地分支:master
解決方案
Git 已經提示我們,先用 git pull 把最新的內容從遠程分支(origin/master)拉下來,然后在本地 merge,解決 conflict,再 push。
不過,在 git pull 時,還有其他的錯誤,我們分別看看可能出現的錯誤。
fatal: refusing to merge unrelated histories
此項錯誤是由於本地倉庫和遠程有不同的開始點,也就是兩個倉庫沒有共同的 commit 出現的無法提交。這里我們需要用到 --allow-unrelated-histories。也就是我們的 pull 命令改為下面這樣的:
git pull origin master --allow-unrelated-histories
如果設置了默認分支,可以這樣寫
git pull --allow-unrelated-histories
There is no tracking information for the current branch.
完整報錯代碼可能是這樣的:
There is no tracking information for the current branch. Please specify which branch you want to merge with. See git-pull(1) for details. git pull <remote> <branch> If you wish to set tracking information for this branch you can do so with: git branch --set-upstream-to=origin/<branch> master
原因是沒有指定本地 master 分支和遠程 origin/master 的連接,這里根據提示:
git branch --set-upstream-to=origin/master master
報錯
fatal: The current branch master has no upstream branch.
按照提示輸入以下代碼,可以解決
git push --set-upstream origin master
報錯
warning: adding embedded git repository
當前目錄下面有.git文件夾------默認是隱藏的,直接將.git文件夾掉,再重新git add .
則不再有報警提示,按正常的上傳步驟上傳代碼即可
參考:
git 無法push遠程倉庫 Note about fast-forwards
