码迷,mamicode.com
首页 > Windows程序 > 详细

如何在windows上使用Git.

时间:2015-05-22 19:12:19      阅读:287      评论:0      收藏:0      [点我收藏+]

标签: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能跟踪到谁对代码做了修改,我们需要设置用户名和邮箱.


iluckysi@ILUCKYSI-PC /d/ilucky/git/workspace (master)
$ git config --global user.name sidongxue


iluckysi@ILUCKYSI-PC /d/ilucky/git/workspace (master)
$ git config --global user.email sidongxue@sohu.com


设置完Git后,可以到系统盘的用户目录下查找.gitconfig文件,打开.gitconfig文件,有如下内容:
[user]
name = sidongxue
email = sidongxue@sohu.com




**********第四步:创建本地Git工作空间.
iluckysi@ILUCKYSI-PC /d/ilucky/git/workspace (master)
$ cd /d/ilucky/git/core


iluckysi@ILUCKYSI-PC /d/ilucky/git/core
$ git init
Initialized empty Git repository in d:/ilucky/git/core/.git/


iluckysi@ILUCKYSI-PC /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工作空间.
iluckysi@ILUCKYSI-PC /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.


iluckysi@ILUCKYSI-PC /d/ilucky/git/core (master)
$




**********第六步:提交已加载到本地Git工作空间的项目文件.
提交时通常都写上注释,例如:用test来作为第一次提交的注释,
提交完成后,随时可以回滚到这个状态,另外如果你需要检查Git的状态,你可以通过git status指令查询.
iluckysi@ILUCKYSI-PC /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


iluckysi@ILUCKYSI-PC /d/ilucky/git/core (master)
$ git status
On branch master
nothing to commit, working directory clean




**********第七步:将提交到Git工作空间的文件上传到GitHub.
注意:需要输入GitHub注册的用户名和密码.
iluckysi@ILUCKYSI-PC /d/ilucky/git/core (master)
$ git remote add origin https://github.com/IluckySi/core.git


iluckysi@ILUCKYSI-PC /d/ilucky/git/core (master)
$ git push origin master
Username for ‘https://github.com‘: IluckySi
Password for ‘https://IluckySi@github.com‘:
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.


iluckysi@ILUCKYSI-PC /d/ilucky/git/core (master)
最后,登陆GitHub,就会看到提交信息.




**********第八步:创建分支.
创建分支是创建项目的独立版本,独立于你的主分支.
在主分支上创建分支,开发新的功能,当功能开发完成并通过测试后,再合并到主分支.
iluckysi@ILUCKYSI-PC /d/ilucky/git/core (master)
$ git checkout -b core_branch
Switched to a new branch ‘core_branch‘


iluckysi@ILUCKYSI-PC /d/ilucky/git/core (core_branch)
$ git branch
* core_branch
  master


iluckysi@ILUCKYSI-PC /d/ilucky/git/core (core_branch)
经过如上操作,你现在在IDE上的所有操作都是在分支下进行的,不会影响主分支.




**********第九步:合并分支.
测试:在分支上添加一个UserType类.
合并分支之前首先执行第五步和第六步,加载项目文件和提交项目文件.
然后是切换到主分支上,最后合并分支.
iluckysi@ILUCKYSI-PC /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.


iluckysi@ILUCKYSI-PC /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


iluckysi@ILUCKYSI-PC /d/ilucky/git/core (core_branch)
$ git checkout master
Switched to branch ‘master‘
Your branch is up-to-date with ‘origin/master‘.


iluckysi@ILUCKYSI-PC /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


iluckysi@ILUCKYSI-PC /d/ilucky/git/core (master)
$




**********第十步:丢弃分支
测试:在分支上创建一个文件TestDel.
iluckysi@ILUCKYSI-PC /d/ilucky/git/core (core_branch)
$ git add .


iluckysi@ILUCKYSI-PC /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


iluckysi@ILUCKYSI-PC /d/ilucky/git/core (core_branch)
$ git checkout master
Switched to branch ‘master‘
Your branch is up-to-date with ‘origin/master‘.


iluckysi@ILUCKYSI-PC /d/ilucky/git/core (master)
$
最后到IED上看,TestDel文件没有.




**********第十一步:删除分支
测试:在分支上添加一个TestDel2类.
假如分支没有合并,你会得到一个错误信息,如下:
iluckysi@ILUCKYSI-PC /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‘.


iluckysi@ILUCKYSI-PC /d/ilucky/git/core (master)
$
也可以强制删除分支,如下:
iluckysi@ILUCKYSI-PC /d/ilucky/git/core (master)
$ git branch -D core_branch
Deleted branch core_branch (was 57f8eb6).




**********第十二步:回滚到之前的状态.
首先查看日志,然后查看会滚到哪个状态,这是每次提交时的注释就尤其重要了.
iluckysi@ILUCKYSI-PC /d/ilucky/git/core (master)
$ git log
commit d0bdfba72d1ec57c611e3673b6edafddd8385556
Author: sidongxue <sidongxue@sohu.com>
Date:   Fri May 22 17:51:14 2015 +0800


    aa


commit 99812a9193f675ae233fecedcc2469ec521ff7fd
Author: sidongxue <sidongxue@sohu.com>
Date:   Fri May 22 17:28:47 2015 +0800


    test


commit b2aeac18798a01437e8a8c7a7d86e94739a9ccd7
Author: sidongxue <sidongxue@sohu.com>
Date:   Fri May 22 17:23:26 2015 +0800


    ss


commit b0bf3a3ff823e5f23b2d4cf0f617cba67370cda6
Author: sidongxue <sidongxue@sohu.com>
Date:   Fri May 22 17:18:36 2015 +0800


    hello


commit c143b5a950099580a31840b5045196851015c7fc
Author: sidongxue <sidongxue@sohu.com>
Date:   Fri May 22 16:10:00 2015 +0800


    core_branch add UserType


commit b8b64766c6f66511ca8061911e019b3066c157f5
Author: sidongxue <sidongxue@sohu.com>
Date:   Fri May 22 15:41:07 2015 +0800


    test


iluckysi@ILUCKYSI-PC /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


iluckysi@ILUCKYSI-PC /d/ilucky/git/core ((b8b6476...))
$




**********第十二步:下载远程代码库.
iluckysi@ILUCKYSI-PC /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.


iluckysi@ILUCKYSI-PC /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


iluckysi@ILUCKYSI-PC /d/ilucky/git/core ((d0bdfba...))






问题:如果checkout master时报如下错误,明本地的master和远程的master的head节点已经不在一个commit节点上了,
需要重新push.
iluckysi@ILUCKYSI-PC /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)


iluckysi@ILUCKYSI-PC /d/ilucky/git/core (master)
$ git push origin master
Username for ‘https://github.com‘: IluckySi
Password for ‘https://IluckySi@github.com‘:
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


iluckysi@ILUCKYSI-PC /d/ilucky/git/core (master)
$ git checkout master
Already on ‘master‘
Your branch is up-to-date with ‘origin/master‘.


iluckysi@ILUCKYSI-PC /d/ilucky/git/core (master)

如何在windows上使用Git.

标签:windows   git   github   

原文地址:http://blog.csdn.net/sidongxue2/article/details/45920167

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!