標籤:windows git github
如何在windows上使用Git.
**********第一步:註冊GitHub帳號.
註冊GitHub帳號,並建立資源倉庫core.
**********第二步:安裝Git.
安裝Git,我使用的Git版本是Git-1.9.5-preview20150319.exe,Git安裝完後在開始菜單可以看到Git Bash和Git Gui(可視化操作).
如下的所有操都是在GitBash下.另外/d/ilucky/git/workspace (master)目錄是上次測試git的工作空間.
**********第三步:配置Git使用者.
配置Git使用者,目的是為了Git能跟蹤到誰對代碼做了修改,我們需要設定使用者名稱和郵箱.
[email protected] /d/ilucky/git/workspace (master)
$ git config --global user.name sidongxue
[email protected] /d/ilucky/git/workspace (master)
$ git config --global user.email [email protected]
設定完Git後,可以到系統硬碟的使用者目錄下尋找.gitconfig檔案,開啟.gitconfig檔案,有如下內容:
[user]
name = sidongxue
email = [email protected]
**********第四步:建立本地Git工作空間.
[email protected] /d/ilucky/git/workspace (master)
$ cd /d/ilucky/git/core
[email protected] /d/ilucky/git/core
$ git init
Initialized empty Git repository in d:/ilucky/git/core/.git/
[email protected] /d/ilucky/git/core (master)
建立完本地Git工作空間後,會看到在d/ilucky/git/core目錄下多了一個隱藏檔案.git.
////////////////同時到系統硬碟的使用者目錄下尋找.gitconfig檔案,開啟.gitconfig檔案,有如下內容:
[gui]
recentrepo = D:/ilucky/git/gittest
**********第五步:附加元件目檔案到本地Git工作空間.
將建立的測試專案git拷貝到D:/ilucky/git/core目錄下,執行如下指令將項目載入到本地的Git工作空間.
[email protected] /d/ilucky/git/core (master)
$ git add .
warning: LF will be replaced by CRLF in git/.mymetadata.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in git/WebRoot/META-INF/MANIFEST.MF.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in git/WebRoot/WEB-INF/web.xml.
The file will have its original line endings in your working directory.
[email protected] /d/ilucky/git/core (master)
$
**********第六步:提交已載入到本地Git工作空間的專案檔.
提交時通常都寫上注釋,例如:用test來作為第一次提交的注釋,
提交完成後,隨時可以復原到這個狀態,另外如果你需要檢查Git的狀態,你可以通過git status指令查詢.
[email protected] /d/ilucky/git/core (master)
$ git commit -m "test"
[master (root-commit) b8b6476] test
warning: LF will be replaced by CRLF in git/.mymetadata.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in git/WebRoot/META-INF/MANIFEST.MF.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in git/WebRoot/WEB-INF/web.xml.
The file will have its original line endings in your working directory.
14 files changed, 171 insertions(+)
create mode 100644 git/.classpath
create mode 100644 git/.mymetadata
create mode 100644 git/.project
create mode 100644 git/.settings/.jsdtscope
create mode 100644 git/.settings/org.eclipse.jdt.core.prefs
create mode 100644 git/.settings/org.eclipse.wst.common.component
create mode 100644 git/.settings/org.eclipse.wst.common.project.facet.core.xml
create mode 100644 git/.settings/org.eclipse.wst.jsdt.ui.superType.container
create mode 100644 git/.settings/org.eclipse.wst.jsdt.ui.superType.name
create mode 100644 git/WebRoot/META-INF/MANIFEST.MF
create mode 100644 git/WebRoot/WEB-INF/classes/com/ilucky/git/User.class
create mode 100644 git/WebRoot/WEB-INF/web.xml
create mode 100644 git/WebRoot/index.jsp
create mode 100644 git/src/com/ilucky/git/User.java
[email protected] /d/ilucky/git/core (master)
$ git status
On branch master
nothing to commit, working directory clean
**********第七步:將提交到Git工作空間的檔案上傳到GitHub.
注意:需要輸入GitHub註冊的使用者名稱和密碼.
[email protected] /d/ilucky/git/core (master)
$ git remote add origin https://github.com/IluckySi/core.git
[email protected] /d/ilucky/git/core (master)
$ git push origin master
Username for ‘https://github.com‘: IluckySi
Password for ‘https://[email protected]‘:
Counting objects: 29, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (16/16), done.
Writing objects: 100% (29/29), 4.01 KiB | 0 bytes/s, done.
Total 29 (delta 0), reused 0 (delta 0)
To https://github.com/IluckySi/core.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
[email protected] /d/ilucky/git/core (master)
最後,登陸GitHub,就會看到提交資訊.
**********第八步:建立分支.
建立分支是建立項目的獨立版本,獨立於你的主分支.
在主分支上建立分支,開發新的功能,當功能開發完成並通過測試後,再合并到主分支.
[email protected] /d/ilucky/git/core (master)
$ git checkout -b core_branch
Switched to a new branch ‘core_branch‘
[email protected] /d/ilucky/git/core (core_branch)
$ git branch
* core_branch
master
[email protected] /d/ilucky/git/core (core_branch)
經過如上操作,你現在在IDE上的所有操作都是在分支下進行的,不會影響主分支.
**********第九步:合并分支.
測試:在分支上添加一個UserType類.
合并分支之前首先執行第五步和第六步,附加元件目檔案和提交專案檔.
然後是切換到主分支上,最後合并分支.
[email protected] /d/ilucky/git/core (core_branch)
$ git add .
warning: LF will be replaced by CRLF in git/.mymetadata.
The file will have its original line endings in your working directory.
[email protected] /d/ilucky/git/core (core_branch)
$ git commit -m "core_branch add UserType"
[core_branch c143b5a] core_branch add UserType
4 files changed, 20 insertions(+)
rewrite git/WebRoot/WEB-INF/classes/com/ilucky/git/User.class (100%)
create mode 100644 git/WebRoot/WEB-INF/classes/com/ilucky/git/UserType.class
create mode 100644 git/src/com/ilucky/git/UserType.java
[email protected] /d/ilucky/git/core (core_branch)
$ git checkout master
Switched to branch ‘master‘
Your branch is up-to-date with ‘origin/master‘.
[email protected] /d/ilucky/git/core (master)
$ git merge core_branch
Updating b8b6476..c143b5a
Fast-forward
git/WebRoot/WEB-INF/classes/com/ilucky/git/User.class | Bin 723 -> 1191 bytes
.../WEB-INF/classes/com/ilucky/git/UserType.class | Bin 0 -> 873 bytes
git/src/com/ilucky/git/User.java | 15 +++++++++++++++
git/src/com/ilucky/git/UserType.java | 5 +++++
4 files changed, 20 insertions(+)
create mode 100644 git/WebRoot/WEB-INF/classes/com/ilucky/git/UserType.class
create mode 100644 git/src/com/ilucky/git/UserType.java
[email protected] /d/ilucky/git/core (master)
$
**********第十步:丟棄分支
測試:在分支上建立一個檔案TestDel.
[email protected] /d/ilucky/git/core (core_branch)
$ git add .
[email protected] /d/ilucky/git/core (core_branch)
$ git commit -m "drop branch"
[core_branch 57f8eb6] drop branch
2 files changed, 5 insertions(+)
create mode 100644 git/WebRoot/WEB-INF/classes/com/ilucky/git/TestDel.class
create mode 100644 git/src/com/ilucky/git/TestDel.java
[email protected] /d/ilucky/git/core (core_branch)
$ git checkout master
Switched to branch ‘master‘
Your branch is up-to-date with ‘origin/master‘.
[email protected] /d/ilucky/git/core (master)
$
最後到IED上看,TestDel檔案沒有.
**********第十一步:刪除分支
測試:在分支上添加一個TestDel2類.
假如分支沒有合并,你會得到一個錯誤資訊,如下:
[email protected] /d/ilucky/git/core (master)
$ git branch -d core_branch
error: The branch ‘core_branch‘ is not fully merged.
If you are sure you want to delete it, run ‘git branch -D core_branch‘.
[email protected] /d/ilucky/git/core (master)
$
也可以強制移除分支,如下:
[email protected] /d/ilucky/git/core (master)
$ git branch -D core_branch
Deleted branch core_branch (was 57f8eb6).
**********第十二步:復原到之前的狀態.
首先查看日誌,然後查看會滾到哪個狀態,這是每次提交時的注釋就尤其重要了.
[email protected] /d/ilucky/git/core (master)
$ git log
commit d0bdfba72d1ec57c611e3673b6edafddd8385556
Author: sidongxue <[email protected]>
Date: Fri May 22 17:51:14 2015 +0800
aa
commit 99812a9193f675ae233fecedcc2469ec521ff7fd
Author: sidongxue <[email protected]>
Date: Fri May 22 17:28:47 2015 +0800
test
commit b2aeac18798a01437e8a8c7a7d86e94739a9ccd7
Author: sidongxue <[email protected]>
Date: Fri May 22 17:23:26 2015 +0800
ss
commit b0bf3a3ff823e5f23b2d4cf0f617cba67370cda6
Author: sidongxue <[email protected]>
Date: Fri May 22 17:18:36 2015 +0800
hello
commit c143b5a950099580a31840b5045196851015c7fc
Author: sidongxue <[email protected]>
Date: Fri May 22 16:10:00 2015 +0800
core_branch add UserType
commit b8b64766c6f66511ca8061911e019b3066c157f5
Author: sidongxue <[email protected]>
Date: Fri May 22 15:41:07 2015 +0800
test
[email protected] /d/ilucky/git/core (master)
$ git checkout b8b64766c6f66511ca8061911e019b3066c157f5
Note: checking out ‘b8b64766c6f66511ca8061911e019b3066c157f5‘.
You are in ‘detached HEAD‘ state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b new_branch_name
HEAD is now at b8b6476... test
[email protected] /d/ilucky/git/core ((b8b6476...))
$
**********第十二步:下載遠程程式碼程式庫.
[email protected] /d/ilucky/git/core ((b8b6476...))
$ git clone https://github.com/IluckySi/core.git
Cloning into ‘core‘...
remote: Counting objects: 108, done.
remote: Compressing objects: 100% (47/47), done.
remote: Total 108 (delta 14), reused 104 (delta 10), pack-reused 0
Receiving objects: 100% (108/108), 10.19 KiB | 0 bytes/s, done.
Resolving deltas: 100% (14/14), done.
Checking connectivity... done.
[email protected] /d/ilucky/git/core ((b8b6476...))
注意:如果你已經在本地的項目上工作了,只是想從遠程程式碼程式庫上取得它最新的版本,移動到項目的根目錄下,還需要執行如下指令:
$ git pull origin master
From https://github.com/IluckySi/core
* branch master -> FETCH_HEAD
Updating b8b6476..d0bdfba
Fast-forward
.../WEB-INF/classes/com/ilucky/git/TestDrop.class | Bin 0 -> 279 bytes
.../WEB-INF/classes/com/ilucky/git/TestUser.class | Bin 0 -> 279 bytes
git/WebRoot/WEB-INF/classes/com/ilucky/git/User.class | Bin 723 -> 1191 bytes
.../WEB-INF/classes/com/ilucky/git/UserType.class | Bin 0 -> 873 bytes
git/src/com/ilucky/git/TestDrop.java | 5 +++++
git/src/com/ilucky/git/TestUser.java | 5 +++++
git/src/com/ilucky/git/User.java | 15 +++++++++++++++
git/src/com/ilucky/git/UserType.java | 5 +++++
8 files changed, 30 insertions(+)
create mode 100644 git/WebRoot/WEB-INF/classes/com/ilucky/git/TestDrop.class
create mode 100644 git/WebRoot/WEB-INF/classes/com/ilucky/git/TestUser.class
create mode 100644 git/WebRoot/WEB-INF/classes/com/ilucky/git/UserType.class
create mode 100644 git/src/com/ilucky/git/TestDrop.java
create mode 100644 git/src/com/ilucky/git/TestUser.java
create mode 100644 git/src/com/ilucky/git/UserType.java
[email protected] /d/ilucky/git/core ((d0bdfba...))
問題:如果checkout master時報如下錯誤,明本地的master和遠端master的head節點已經不在一個commit節點上了,
需要重新push.
[email protected] /d/ilucky/git/core (master)
$ git checkout master
Already on ‘master‘
Your branch is ahead of ‘origin/master‘ by 3 commits.
(use "git push" to publish your local commits)
[email protected] /d/ilucky/git/core (master)
$ git push origin master
Username for ‘https://github.com‘: IluckySi
Password for ‘https://[email protected]‘:
Counting objects: 59, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (23/23), done.
Writing objects: 100% (47/47), 3.29 KiB | 0 bytes/s, done.
Total 47 (delta 9), reused 0 (delta 0)
To https://github.com/IluckySi/core.git
c143b5a..99812a9 master -> master
[email protected] /d/ilucky/git/core (master)
$ git checkout master
Already on ‘master‘
Your branch is up-to-date with ‘origin/master‘.
[email protected] /d/ilucky/git/core (master)
如何在windows上使用Git.