GitHub操作總結 : 總結看不明白就看下面的詳細講解.
GitHub操作流程 :
第一次提交 :
-- 初始化git倉庫 :git init ;
-- 提交改變到緩存 :git commit -m 'description' ;
-- 本地git倉庫關聯GitHub倉庫 : git remote add origin [email protected]:han1202012/TabHost_Test.git ;
-- 提交到GitHub中 : git push -u origin master ;
之後修改提交 :
-- 與GitHub遠程倉庫同步 :git pull ;
-- 查看文件變更 : git status ;
-- 提交代碼到本地緩存 : git commit -m 'description';
--提交代碼到遠程GitHub倉庫 : git push ;
.gitignore用法 : 開放模式 註明忽略的文件 直接列出文件名, 保守模式 註明保留的文件 !文件名 ;
Git標籤操作 : 輕量級標籤, 帶註釋標籤;
--查看標籤 : git tag ;
--添加標籤 : 輕量級標籤 git tag tagName , 帶註釋標籤git tag -a tagName -m 'description' ;
--刪除標籤 : git tag -d tagName ;
--提交標籤到GitHub中 : git push origin --tags ;
Git分支操作: 創建分支後, 分支操作不會影響master分支, 但是master分支改變會影其它分支;
--列出分支 : git branch ;
--切換分支 : git checkout master ;
--提交分支 : git push origin branchName ;
--刪除分支 : git branch -d branchName , 強制刪除分支 git branch -D branchName ;
--合併分支 : git merge branchName ;
.
一. Git介紹
分佈式 : Git版本控制系統是一個分佈式的系統, 是用來保存工程源代碼歷史狀態的命令行工具;
保存點 : Git的保存點可以追蹤源碼中的文件, 並能得到某一個時間點上的整個工程項目額狀態; 可以在該保存點將多人提交的源碼合併, 也可以會退到某一個保存點上;
Git離線操作性 :Git可以離線進行代碼提交, 因此它稱得上是完全的分佈式處理, Git所有的操作不需要在線進行; 這意味着Git的速度要比SVN等工具快得多, 因爲SVN等工具需要在線時才能操作, 如果網絡環境不好, 提交代碼會變得非常緩慢;
Git基於快照 : SVN等老式版本控制工具是將提交點保存成補丁文件, Git提交是將提交點指向提交時的項目快照, 提交的東西包含一些元數據(作者, 日期, GPG等);
Git的分支和合並 : 分支模型是Git最顯著的特點, 因爲這改變了開發者的開發模式, SVN等版本控制工具將每個分支都要放在不同的目錄中, Git可以在同一個目錄中切換不同的分支;
分支即時性 : 創建和切換分支幾乎是同時進行的, 用戶可以上傳一部分分支, 另外一部分分支可以隱藏在本地, 不必將所有的分支都上傳到GitHub中去;
分支靈活性 : 用戶可以隨時 創建 合併 刪除分支, 多人實現不同的功能, 可以創建多個分支進行開發, 之後進行分支合併, 這種方式使開發變得快速, 簡單, 安全;
二. Git通用客戶端(msysgit)
1. 下載Git客戶端
Git客戶端下載地址 :https://code.google.com/p/msysgit/downloads/list 將地址複製到瀏覽器欄即可下載.
2. 安裝Git客戶端
歡迎界面 : 直接下一步;
協議 : 必須接受;
安裝位置 : 預留100M空間, 自定義安裝位置;
選擇安裝組件 :也可以默認選擇;
-- 圖標組件(Addition icons) : 選擇是否創建快速啓動欄圖標 或者 是否創建桌面快捷方式;
-- 桌面瀏覽(Windows Explorer integration) : 瀏覽源碼的方法, 單獨的上下文瀏覽 只使用bash 或者 只用Git GUI工具; 高級的上下文瀏覽方法 使用git-cheetah plugin插件;
-- 關聯配置文件 : 是否關聯git配置文件, 該配置文件主要顯示文本編輯器的樣式;
-- 關聯shell腳本文件 : 是否關聯Bash命令行執行的腳本文件;
-- 使用TrueType編碼 : 在命令行中是否使用TruthType編碼, 該編碼是微軟和蘋果公司制定的通用編碼;
開始菜單快捷方式目錄 : 設置開始菜單中快捷方式的目錄名稱, 也可以選擇不再開始菜單中創建快捷方式;
設置環境變量 : 選擇使用什麼樣的命令行工具, 一般情況下我們默認使用Git Bash即可, 默認選擇;
-- Git自帶 : 使用Git自帶的Git Bash命令行工具;
-- 系統自帶CMD : 使用Windows系統的命令行工具;
-- 二者都有 : 上面二者同時配置, 但是注意, 這樣會將windows中的find.exe 和 sort.exe工具覆蓋, 如果不懂這些儘量不要選擇;
選擇換行格式 :
-- 檢查出windows格式轉換爲unix格式 : 將windows格式的換行轉爲unix格式的換行在進行提交;
-- 檢查出原來格式轉爲unix格式 : 不管什麼格式的, 一律轉爲unix格式的換行在進行提交;
-- 不進行格式轉換 : 不進行轉換, 檢查出什麼, 就提交什麼;
開始安裝 :
安裝結束 : over;
3. 配置GitHub
修改Git Bash的配置 : 將Git Bash設置爲快速編輯模式, 可以更好的使用該命令行工具 :
(1) 創建本地ssh
使用命令 : 創建本地ssh
- ssh-keygen -t rsa -C "[email protected]"
路徑選擇 : 使用該命令之後, 會出現提示選擇ssh-key生成路徑, 這裏直接點回車默認即可, 生成的ssh-key在默認路徑中;
密碼確認 : 這裏我們不使用密碼進行登錄, 用密碼太麻煩;
(2) 將ssh配置到GitHub中
進入生成的ssh目錄 : C:\Documents and Settings\Administrator\.ssh 中, 使用記事本打開 id_rsa.pub 文件, 將該文件中的內容複製;
id_rsa.pub 文件內容 :
- ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAtT1YCeaNulpfC+ARqAWrCdfpi6CpW3gkGT0hp6Q8by7NnEfy4dah9CwSrNbWJH5eS4tiqckE+bdbSVNvAboFD1MtGZjtzE4GDweG/6J/SDYV/ADFN/RLWGb+5rQ8wMCjc/fODgLJDFxk1Fwk/TTqTcbtLab1toLcts3zGIW5DstA3RQ0CCX/sPew5m7vh7DcKXluj2TBd9hw== [email protected]
進入GitHub網站 : 登錄GitHub, 選擇Account Setting 用戶設置 :
選擇左側的SSH-KEY選項 :
點擊右側的Add SSH key :
將上面複製好的ssh-key複製進去 :
驗證是否配置成功 :
使用命令 :
- ssh -T [email protected]
成功提示 : 如果出現Hi han1202012! You've successfully authenticated, but GitHub does not provide shell access. 就說明配置成功, 可以連接上GitHub;
(3) 配置本地用戶和郵箱
用戶名郵箱作用 : 我們需要設置一個用戶名 和 郵箱, 這是用來上傳本地倉庫到GitHub中, 在GitHub中顯示代碼上傳者;
使用命令 :
- git config --global user.name "HanShuliang" //設置用戶名
- git config --global user.email "[email protected]" //設置郵箱
4. Git Bash提交源碼到GitHub
(1) GitHub中創建一個工程
工程的https地址: https://github.com/han1202012/TabHost_Test.git .
工程的SSH地址 : [email protected]:han1202012/TabHost_Test.git .
GitHub提示生成的命令 :
-- Create a new repository on the command line :
- touch README.md
- git init
- git add README.md
- git commit -m "first commit"
- git remote add origin [email protected]:han1202012/TabHost_Test.git
- git push -u origin master
-- Push an existing repository from the command line :
- git remote add origin [email protected]:han1202012/TabHost_Test.git
- git push -u origin master
(2) 初始化git目錄
使用命令 :
- git init
如果不執行這條命令, 就會出現錯誤 : fatal: Not a git repository (or any of the parent directories): .git .
(3) 添加文件
使用命令 :
- git add ./*
- warning: LF will be replaced by CRLF in AndroidManifest.xml.
- The file will have its original line endings in your working directory.
- warning: LF will be replaced by CRLF in bin/AndroidManifest.xml.
- The file will have its original line endings in your working directory.
- warning: LF will be replaced by CRLF in gen/shuliang/han/tabhost_test/BuildConfig.java.
將倉庫中的所有文件添加到緩存中;
(4) 提交緩存
使用命令 :
- git remote add origin [email protected]:han1202012/TabHost_Test.git
將添加或者改變的內容提交到緩存中;
(5) 將Git本地緩存提交到GitHub中
使用命令 :
- git push -u origin master
執行該命令, 源碼就被提交到了GitHub 中;
三. Git的一些用法
1. .gitignore文件
屏蔽文件 : .gitignore文件是告訴Git哪些目錄或者文件需要忽略, 這些文件將不被提交;
常用場景 : 寫完代碼後會執行變異調試等操作, 使用 .gitignore 文件將這些編譯後的文件屏蔽, 這些文件不需要Git工具進行管理;
Android中的.gitignore : 在Android中 bin 和 gen 兩個目錄可以忽略;
.gitignore位置 : 項目根目錄下;
過濾模式 : Git中的 .gitignore 中有兩種模式,開放模式 和保守模式,保守模式的優先級要高於開放模式;
開放模式 : 設置哪些文件 活 目錄 被過濾, 凡是在文件中列出的文件或者目錄都要被過濾掉;
-- 過濾目錄 : /bin/ 就是將bin目錄過濾, 該文件下的所有目錄和文件都不被提交;
-- 過濾某個類型文件 : *.zip *.class 就是過濾zip 和 class 後綴的文件, 這些文件不被提交;
-- 過濾指定文件 : /gen/R.java, 過濾該文件, 該文件不被提交;
保守模式 : 設置哪些文件不被過濾, 凡是列在其中的文件都要完整的提交上去;
-- 跟蹤目錄 : !/src , 該目錄下的所有文件都要被提交;
-- 跟蹤某類文件 : !*.java , 凡是java文件都要保留;
-- 跟蹤指定文件 : !/AndroidManifest.xml , 該文件需要保留, 提交上去;
配置原則 : 一般情況下采用開放模式魚保守模式共同使用;
eg : 一個目錄下有很多目錄和文件, 當我們只需要保留其中的一個文件的時候, 先用開放模式不保留這些文件, 然後用保守模式將這個文件留下來, 保守模式的優先級要高於開放模式;
2. 標籤使用(Tag)
標籤作用: 在開發的一些關鍵時期,使用標籤來記錄這些關鍵時刻, 例如發佈版本, 有重大修改, 升級的時候, 會使用標籤記錄這些時刻, 來永久標記項目中的關鍵歷史時刻;
查看標籤: 列出的標籤安裝ASCII字母順序確定, 排序沒有很明確的意義;
-- 列出所有的標籤 :
- git tag
- git tag -l v1.*
標籤分類 : Git中的標籤分爲 輕量級標籤(lightweight) 和 帶註釋的標籤(annotated), 一般情況下推薦使用帶註釋的標籤, 如果標籤是臨時的可以採用輕量級標籤;
-- 輕量級標籤 : 輕量級標籤中的信息含量很少, 這種標籤只代表某時刻代碼的提交, 相當於指向這個提交的指針;
-- 帶註釋標籤 : 這種標籤是一種校驗和, 包含標籤名, 郵箱, 日期, 標籤信息, GPG簽名 和 驗證, 它相當於一個對象, 封裝了這些信息;
創建標籤 :
-- 創建輕量級標籤 : 這樣的標籤沒有附帶其它的信息;
- git tag v2.0
- git tag -a v2.1 -m 'first version'
-- 創建GPG私鑰的註釋標籤 :
- git tag -s v2.1 -m 'GPG version'
在本機上實驗不成功 出現下面的錯誤 :
- Administrator@XRDPTJ9ILK6IWRA /storage/TabHost_Test (master)
- $ git tag -s v2.2 -m 'GPG version'
- gpg: error loading `iconv.dll': 找不到指定的模塊。
- gpg: please see http://www.gnupg.org/download/iconv.html for more information
- gpg: keyblock resource `c:/Documents and Settings/Administrator/.gnupg\secring.g
- pg': file open error
- gpg: keyblock resource `c:/Documents and Settings/Administrator/.gnupg\pubring.g
- pg': file open error
- gpg: skipped "HanShuliang <[email protected]>": secret key not available
- gpg: signing failed: secret key not available
- error: gpg failed to sign the data
- error: unable to sign the tag
-- 爲之前的提交添加標籤 : 先使用 git log --oneline 命令列出之前的提交, 會有一個七位的十六進制數進行標記, 使用git tag -a v3.1 f1bb97a 命令即可爲這個提交添加標籤;
- $ git log --oneline
- f1bb97a first commit
- git tag -a v3.1 f1bb97a
刪除標籤 : 使用命令 git tag -d 標籤名 命令刪除標籤;
- git tag -d v0.1
驗證標籤 : 提交了GPG帶註釋標籤纔可以驗證, 因爲上面沒有提交成功, 這裏留下一個命令;
- git tag -v v1.0
共享標籤 : 即將標籤提交到GitHub中;
- git push origin --tags
3. 分支和合並
(1) 查看現存分支
查看現存分支 : git branch命令;
- git branch
從結果可以看出, 現在只有一個分支master;
(2) 創建分支
創建分支 : git branch 分之名稱, 就可以創建一個分支, 創建完分支以後可以查看分支, 當前使用的分支會顯示成爲綠色, 前面帶有 "*", 如果不是當前使用的分支, 顯示的是白色, 並且沒有 "*" 前綴;
- git branch branch1
(3) 切換分支
切換分支 : git checkout 分支名稱, 切換分支以後, 在分支進行操作,文件的改變不會體現在master主分支中, 主分支改變, 會體現在其它分支中;
- git checkout branch1
關於主分支和其它分支 :
-- 分支編輯 : 現有兩個分支 master 主分支, 和剛創建的branch1 分支, 切換到 branch1 分支, 在branch1 中創建一個文件 description_branch1.txt;
-- 其它分支文件不會影響主分支 : 然後在切換回 master 主分支, 發現沒有description_branch1.txt文件, 創建一個文件 description_master.txt;
-- 主分支會影響其它分支 : 在切換回 branch1 分支中, 發現存在 description_master.txt文件;
.
對比過程 :
查看該工程項目分支情況 :
- git branch
-- 分支結果 :
- branch1
- master
- git checkout branch1
- Switched to branch 'branch1'
- ls
- AndroidManifest.xml ic_launcher-web.png res
- assets libs src
- bin proguard-project.txt
- gen project.properties
創建一個文件 :
- touch description_branch1.txt
添加這個文件到本地緩存 :
- git add description_branch1.txt
提交本地緩存 : 注意, 這裏必須提交, 如果不提交緩存, 這個文件在主分支也會出現;
- git commit -m 'add a description of branch1'
- [branch1 7f5785e] add a description of branch1
- 1 file changed, 0 insertions(+), 0 deletions(-)
- create mode 100644 description_branch1.txt
查看該分支下的文件 :
- ls
- AndroidManifest.xml libs
- assets proguard-project.txt
- bin project.properties
- <strong>description_branch1.txt</strong> res
- gen src
- ic_launcher-web.png
切換回主分支 :
- git checkout master
-- 切換分支結果 :
- Switched to branch 'master'
- Your branch is up-to-date with 'origin/master'.
- ls
- AndroidManifest.xml ic_launcher-web.png res
- assets libs src
- bin proguard-project.txt
- gen project.properties
.
(4) 提交分支
提交分支命令 : 將本地的分支提交到 GitHub中;
- git push origin experiment
(5) 分支合併移除
合併分支命令 : 合併分支之後, 分支中有的文件在 主分支中也會顯示, 相當於將branch1 分支中的文件拷貝了一份到master分支中;
- git merge branch1
- Updating f1bb97a..7f5785e
- Fast-forward
- description_branch1.txt | 0
- 1 file changed, 0 insertions(+), 0 deletions(-)
- create mode 100644 description_branch1.txt
.
(6) 刪除分支
刪除分支命令 :
- git branch -d branch1
強制刪除分支命令 : 如果branch1 分支還沒有被合併的話, Git是不允許刪除這個分支的, 此時要想刪除該分支, 就只能使用下面的命令強制刪除該分支 :
- git branch -D branch1
4. Git工作流程
(1) 兩種工作流程
協作開發工作流程 : 這種情況是最複雜的情況, 多人團隊共同開發一個項目;
-- 與遠程倉庫同步 : git pull ;
-- 修改文件 : 添加 刪除 修改文件;
-- 查看變更 : git status ;
-- 載入變更 :添加文件, 先使用git add fileName, 在使用 git commit -m 'note' 載入變更; 如果是刪除 修改文件, 直接使用 git commit -m 'note' 提交;
-- 重複 : 重複執行 修改文件 查看變更 載入變更 提交載入動作;
-- 上傳 : 使用 git push 命令將項目源碼提交帶GitHub中去;
單獨開發工作流程 : 如果是個人獨立開發, 僅追蹤本地文件變更, 就不需要提交到服務器上, 因爲Git是分佈式的;
-- 修改文件 :
-- 提交變更 :
-- 重複 :
(2) 簡單示例
1> 獨立開發示例
該示例不會上傳, 僅在本地進行操作;
從GitHub中檢出項目源碼 : 注意, 檢出的源碼是根目錄源碼, 我們在總倉庫的根目錄檢出即可,不同再爲項目創建目錄;
- git clone [email protected]:han1202012/AndroidPictureViewer.git
- Cloning into 'AndroidPictureViewer'...
- remote: Counting objects: 86, done.
- remote: Compressing objects: 100% (62/62), done.
- remote: Total 86 (delta 3), reused 86 (delta 3)
- Receiving objects: 100% (86/86), 1.67 MiB | 109.00 KiB/s, done.
- Resolving deltas: 100% (3/3), done.
- Checking connectivity... done.
刪除bin和gen目錄 : 這兩個目錄是Android工程編譯產生的臨時文件, 沒有必要上傳到GitHub中去;
- rm -rf bin gen
- git status
- On branch master
- Your branch is up-to-date with 'origin/master'.
- Changes not staged for commit:
- (use "git add/rm <file>..." to update what will be committed)
- (use "git checkout -- <file>..." to discard changes in working directory)
- deleted: bin/AndroidManifest.xml
- deleted: bin/ImageView_Test.apk
- deleted: bin/classes.dex
- deleted: bin/res/crunch/drawable-xxhdpi/ic_launcher.png
- deleted: bin/resources.ap_
- deleted: gen/shuliang/han/imageview_test/BuildConfig.java
- deleted: gen/shuliang/han/imageview_test/R.java
- git commit -a
- [master e4377ba] delete bin and gen directory.
- 28 files changed, 2 insertions(+), 110 deletions(-)
- create mode 100644 .gitignore
- delete mode 100644 bin/AndroidManifest.xml
- delete mode 100644 bin/ImageView_Test.apk
- delete mode 100644 bin/classes.dex
- delete mode 100644 bin/resources.ap_
- delete mode 100644 gen/shuliang/han/imageview_test/BuildConfig.java
- delete mode 100644 gen/shuliang/han/imageview_test/R.java
2> 協作開發示例
與遠程倉庫同步 :
- git pull
-- 如果其他人沒有提交項目到GitHub上, 就會出現下面結果 :
- Already up-to-date.
修改文件後查看變更 :
- git status
- On branch master
- Your branch is ahead of 'origin/master' by 1 commit.
- (use "git push" to publish your local commits)
- nothing to commit, working directory clean
載入變更 : 如果有變更, 那麼使用命令載入變更. 使用 git commit -a 或者 git add 命令;
-- 添加文件 : 先使用 git add 文件名 來添加文件到緩存, 之後使用 git commit -m '' 命令提交代碼到本地緩存;
-- 刪除改變文件 : 直接使用 git commit -m '', 提交刪除 或則 改變 到本地緩存;
提交項目源碼到服務器 :
- git push
- warning: push.default is unset; its implicit value is changing in
- Git 2.0 from 'matching' to 'simple'. To squelch this message
- and maintain the current behavior after the default changes, use:
- git config --global push.default matching
- To squelch this message and adopt the new behavior now, use:
- git config --global push.default simple
- See 'git help config' and search for 'push.default' for further information.
- (the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
- 'current' instead of 'simple' if you sometimes use older versions of Git)
- Counting objects: 4, done.
- Delta compression using up to 2 threads.
- Compressing objects: 100% (2/2), done.
- Writing objects: 100% (3/3), 292 bytes | 0 bytes/s, done.
- Total 3 (delta 1), reused 0 (delta 0)
- To [email protected]:han1202012/AndroidPictureViewer.git
- 1ddf8c7..e4377ba master -> master
查看GitHub中的源碼情況 : gen 和 bin 目錄果然被刪除了;