1、克隆/下載項目
1)git clone git@git.soydai.cn:liuxuewen/static-file-3.0.git 或者
2)git clone http://git.soydai.cn/liuxuewen/static-file-3.0.git
區別:
a: 第1種使用ssh協議下載,第二種使用http協議,而Git支持多種協議包括 ssh、http、https;
b: 通過http、https協議下載時,需要輸入 Username、Password 登錄成功才會下載到本地;
c: 通過ssh支持的原生git協議速度最快。
2、git提交的常見用法
1)git add . 或者 git add www/js/a.js ------添加(所有改動文件/具體某個文件)到暫存區
2)git commit -m '修改xxxbug' ------描述並提交到本地當前分支
3)git push origin develop ------提交到服務器
分析 1)第1步 git add xxx 的目的是什么,為什么要先提交到暫存區?
答:暫存區存在的主要目的是,為了把當前的確定好已完成的改動暫存先存儲起來,來以下幾種常見情況,比如:
a. 改某一次的bug涉及到 a.js、b.js、c.js,修改完a.js后通過 git add a.js 暫時存儲到暫存區。此后修改b.js,改到中途改亂了,想回退,怎么辦?第一種是 ctrl + z,但可能不巧的是,中間改一半的時候關閉過頁面,此時ctrl+z到中間就退不回去了。此時,暫存區就可以發揮作用了。git checkout -- b.js,b.js 回退到修改之前的版本。ctrl + z 完全不用管。
b. 改某一次的bug涉及到 a.js 一個文件,但是修改非常復雜,涉及到其中的多處地方。快修改結束時,發現有點問題,想回退,由於修改的地方太多,此時 使用 ctrl+z 看得眼花繚亂,怎么辦?可以在修改到某一個關鍵節點時,可以 git add a.js 暫存起前面已經確定的修改。然后可以往后修改,確定改好一處,就暫存一次,這樣想回退時,就可以 git checkout -- a.js 退到上次已確定的地方。
順便說下,git add . 和 git stage ./xxx 的作用是一樣的,stage 翻譯過來是階段,Git是非常鼓勵上面幾種情況的分階段暫存的。推薦使用 git stage ...
分析 2)第二步 git commit -m 'xxx' 的意義是什么?最好什么時候使用該命令?
答:該命令是提交到本地當前分支,-m 'xxx' 是描述這次提交所完成的內容描述。什么時候使用?在某次大的功能開發中,在完成每個小功能模塊的開發時,可以先commit到本地分支。
比如,自己某次的功能開發涉及到三個任務,每個任務的開發過程中,可以通過 git add xxx 暫存的形式小步前進,在完成一個個具體的任務時,通過 git commit -m 'xxx' 描述並提交到本地分支。
注意:有時候,只是一個很小的bug改動,如果先 git add xxx 再 git commit -m 'xxx' 覺得兩步有點麻煩,可以簡寫成一步:git commit -a -m 'xxxx' 即可。但是,要注意的是,如果你有新增的 文件,一步可是不能到位的,必須要 git add xxx 來一下。
分析 3)第三步 git push origin develop 的幾種命令方式?可能遇到的問題?
答:該命令是將在本地分支上的代碼,提交到服務器。
git push 的幾種命令方式:
1: git push origin xxx 提交到服務器的xxx分支
2: git push 直接提交,你會發現,有時候是可以直接 git push ,但有時候會有提示:
fatal: The current branch test has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin test
會讓你先通過 --set-upstream 提交,什么意思?簡單點說,提交時如果不指定分支,在服務器那邊,是沒有一個上游分支去對接。所以需要你先--set-upstream 設置上游分支到服務器,告訴他當前 test 分支的服務器上游分支就是 test 分支。此后在test分支的提交,直接:git push 即可。
只要之前,有人設置過某個上游分支,其他人就可以直接通過 git push 的形式提交。
可能遇到的問題有?
a. 提交時,服務器上的文件有改動,git push 時,會提示:
To git@git.soydai.cn:liuxuewen/soeasy-complete-event-static.git ! [rejected] test -> test (fetch first) error: failed to push some refs to 'git@git.soydai.cn:liuxuewen/soeasy-complete-event-static.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. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
因為該項目服務器下已經有過改動,然后本地在提交時,需要先 git pull 下來。目的是:在提交時做好代碼合並工作。
b. git pull origin xxx 后,如果本地修改的文件 正好其他人也修改了並且提交了,此時,git 會在pull下來后 會出現沖突,提示如下:
remote: Counting objects: 3, done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (3/3), done. From git.soydai.cn:liuxuewen/soeasy-complete-event-static 96916d0..a7224ce test -> origin/test Auto-merging a.txt CONFLICT (content): Merge conflict in a.txt Automatic merge failed; fix conflicts and then commit the result.
告訴你:在 merge a.txt 時發生 沖突。在a.txt里的顯示如下:
<<<<<<< HEAD 111122233hahha3 ======= 111122aa2333 >>>>>>> a7224ce63514c4a5f92f5341fe53f8bf66c86653
通過 ====== 區分,上面是本地的改動,下面是服務器當前分支的改動。此時需要手動合並,自己確認哪個是需要修改的。修改完后,再重復上面 1、2、3步
關於git pull
該命令是拉服務器的最新代碼,有兩種形式:
1: git pull origin xxx 直接拉取xxx分支代碼到本地
2: git pull 以默認行為拉取當前分支代碼到本地,但是有時候會拉不下來,並會提示:
490d665..69a9c93 test2 -> origin/test2 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> test2
讓你要么用1的形式指定分支,或者是 先設置上游分支的形式,然后再通過 git pull 來拉取最新代碼。如下:
liuxuewens-MacBook-Pro:soeasy-complete-event-static liuxuewen$ git branch --set-upstream-to=origin/test2 test2 Branch test2 set up to track remote branch test2 from origin. liuxuewens-MacBook-Pro:soeasy-complete-event-static liuxuewen$ git pull Updating 5dc29be..69a9c93 Fast-forward d.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
為什么要這么設置一下?原因是:git pull 意味着 git fetch + git merge,先把服務器代碼fetch拉到本地,並且要merge合並到某個分支。如果沒有設定上游分支,服務器並不知道 pull 的這個分支,需要合並到你本地的 哪一個分支上!所以,
git branch --set-upstream-to=origin/test2 test2
設置后,意味着,git pull 當前test2分支時,把服務器的 test2 分支代碼 合並到 本地的test2分支。
