Git 使用初体验
http://my.oschina.net/moooofly/blog/228608
很久之前在 http://git.oschina.net/ 上创建了一个私有项目 modb ,目的主要是用来学习如何使用 GIT 来开源自己写的东东,中间由于种种原因停顿了很长时间,但是今天,我下定决心一定要将这个事情完成,于是乎,探索之旅又开始了……
(本文以 windows 平台上的操作进行说明)
最初创建 modb 项目时,默认会产生如下 3 个文件:
- .gitignore
- LICENSE
- README.md
其中 .gitignore 文件的作用可以参考: 《 .gitignore 文件使用说明 》
接下来只要从官网下载了最新的 Git 客户端安装使用就可以了,我安装的是最新的 Git-1.9.2-preview20140411.exe 。
首先,将 modb.git 获取到本地。
|
1
2
3
4
5
6
7
8
9
10
11
12
|
D:\myGIT>git clone https://git.oschina.net/moooofly/modb.gitCloning into 'modb'...Username for 'https://git.oschina.net': mooooflyPassword for 'https://moooofly@git.oschina.net':remote: Counting objects: 5, done.remote: Compressing objects: 100% (4/4), done.remote: Total 5 (delta 0), reused 0 (delta 0)Unpacking objects: 100% (5/5), done.Checking connectivity... done.D:\myGIT> |
之后,通过编辑器创建文件 helloworld.txt。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
D:\myGIT>cd modbD:\myGIT\modb>D:\myGIT\modb>dir 驱动器 D 中的卷是 DSK1_VOL2 卷的序列号是 121D-11F5 D:\myGIT\modb 的目录2014-04-28 14:05 <DIR> .2014-04-28 14:05 <DIR> ..2014-04-28 14:05 156 .gitignore2014-04-28 14:05 1,094 LICENSE2014-04-28 14:05 7 README.md2014-04-28 14:14 0 helloworld.txt 4 个文件 1,257 字节 2 个目录 6,756,630,528 可用字节D:\myGIT\modb> |
通过 add 命令添加新建的文件,通过 status 命令查看此时的状态信息,通过 commit 命令在本地提交变更状态。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
D:\myGIT\modb>git add .D:\myGIT\modb>D:\myGIT\modb>git statusOn branch masterYour branch is up-to-date with 'origin/master'.Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: helloworld.txtD:\myGIT\modb>D:\myGIT\modb>git commit -m "add helloworld.txt"[master 8576fc3] add helloworld.txt Committer: unknown <sunfei@sunfei.kdcrd.com>Your name and email address were configured automatically basedon your username and hostname. Please check that they are accurate.You can suppress this message by setting them explicitly: git config --global user.name "Your Name" git config --global user.email you@example.comAfter doing this, you may fix the identity used for this commit with: git commit --amend --reset-author 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 helloworld.txtD:\myGIT\modb> |
看上面的提示,以系统默认的用户名和密码来进行代码的管理似乎不妥。按照 oschina/git-osc 和《Git初体验》的说法,应该使用在 GIT@OSC 上注册的用户名和邮箱。
|
1
2
3
4
5
6
7
8
9
10
11
12
|
D:\myGIT\modb>D:\myGIT\modb>git config --global user.name "moooofly"D:\myGIT\modb>git config --global user.email "centos.sf@gmail.com"D:\myGIT\modb>D:\myGIT\modb>git commit --amend --reset-author[master 12699ba] add helloworld.txt 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 helloworld.txtD:\myGIT\modb> |
在输入命令 git commit --amend --reset-author 时,会以 VIM 编辑器的形式打开如下内容的文件。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
add helloworld.txt# Please enter the commit message for your changes. Lines starting# with '#' will be ignored, and an empty message aborts the commit.# On branch master# Your branch is ahead of 'origin/master' by 1 commit.# (use "git push" to publish your local commits)## Changes to be committed:# new file: helloworld.txt#~~~ |
直接执行 wq 保存后退出即可。
重新执行 status 命令查看状态,并使用 push 命令向服务器提交。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
D:\myGIT\modb>D:\myGIT\modb>git statusOn branch masterYour branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits)nothing to commit, working directory cleanD:\myGIT\modb>git push origin masterUsername for 'https://git.oschina.net': mooooflyPassword for 'https://moooofly@git.oschina.net':Counting objects: 4, done.Delta compression using up to 2 threads.Compressing objects: 100% (2/2), done.Writing objects: 100% (3/3), 276 bytes | 0 bytes/s, done.Total 3 (delta 1), reused 0 (delta 0)To https://git.oschina.net/moooofly/modb.git 8fb8c63..136a5da master -> masterD:\myGIT\modb> |
此时刷新项目网址,可以看到新的文件已经成功提交了(项目为私有,目前只有我自己能看到)。
接着测试修改文件内容的情况,在 helloworld.txt 文件中增加
|
1
|
hello world! haha! |
之后查看状态
|
1
2
3
4
5
6
7
8
9
10
11
|
D:\myGIT\modb>git statusOn branch masterYour branch is up-to-date with 'origin/master'.Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: helloworld.txtno changes added to commit (use "git add" and/or "git commit -a") |
从输出信息中可以得知,我的修改 git 是感知的,但在我未执行 add 前,git 认为我本地代码的状态仍旧是 up-to-date with 'origin/master' 。同时 git 提示,我的修改尚未 staged for commit ,因为只有 add 后才能 commit ,所以 git 给出的结论为 no changes added to commit 。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
D:\myGIT\modb>D:\myGIT\modb>git logcommit 136a5da602fbba228c51cb7f680f1784bea1e6afAuthor: moooofly <centos.sf@gmail.com>Date: Mon Apr 28 15:11:53 2014 +0800 add helloworld.txtcommit 8fb8c6358323d3213b244355fa0e9df0e28a3b0dAuthor: 摩云飞 <centos.sf@gmail.com>Date: Thu Jan 2 18:23:10 2014 +0800 Initial commitD:\myGIT\modb> |
此时查看 log 信息,可以看到仅有最初创建和刚刚添加 helloworld.txt 文件时的日志内容。
再次执行 add 和 commit 命令,并查看相关状态信息。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
D:\myGIT\modb>D:\myGIT\modb>git add .D:\myGIT\modb>git commit -m "add string in helloworld.txt"[master 1c01bff] add string in helloworld.txt 1 file changed, 1 insertion(+)D:\myGIT\modb>D:\myGIT\modb>git statusOn branch masterYour branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits)nothing to commit, working directory cleanD:\myGIT\modb> |
上述打印可以看出,我的本地代码版本已经超前 'origin/master' 分支 1 个 commit 了。此时已经没有其他需要 commit 的修改,只需要执行 push 操作将本地修改推到 GIT 服务器端。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
D:\myGIT\modb>git logcommit 1c01bff84483507b428eecd4fff7bbe89467dcceAuthor: moooofly <centos.sf@gmail.com>Date: Mon Apr 28 16:38:16 2014 +0800 add string in helloworld.txtcommit 136a5da602fbba228c51cb7f680f1784bea1e6afAuthor: moooofly <centos.sf@gmail.com>Date: Mon Apr 28 15:11:53 2014 +0800 add helloworld.txtcommit 8fb8c6358323d3213b244355fa0e9df0e28a3b0dAuthor: 摩云飞 <centos.sf@gmail.com>Date: Thu Jan 2 18:23:10 2014 +0800 Initial commitD:\myGIT\modb> |
从上述打印可以知道,只要执行过 commit 就会在 log 中体现出来。
此时不执行 push 动作,而是再次修改文件的内容,增加
|
1
|
hello moooofly! haha! |
之后查看状态
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
D:\myGIT\modb>git statusOn branch masterYour branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits)Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: helloworld.txtno changes added to commit (use "git add" and/or "git commit -a")D:\myGIT\modb> |
果然……同时出现了让我将(前面的)commit 进行 push 和将(后面的)修改 staged for commit 的建议。
这里我选择执行 add 命令,结果出现了关于行结束的警告,这个暂时跳过不处理。
|
1
2
3
4
5
|
D:\myGIT\modb>git add .warning: LF will be replaced by CRLF in helloworld.txt.The file will have its original line endings in your working directory.D:\myGIT\modb> |
执行 status 命令,发现有新的修改需要 be committed ,或者也可以使用 git reset HEAD helloworld.txt 将已经处于 staged 状态的修改回退到 unstage 状态。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
D:\myGIT\modb>git statuswarning: LF will be replaced by CRLF in helloworld.txt.The file will have its original line endings in your working directory.On branch masterYour branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits)Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: helloworld.txtD:\myGIT\modb> |
这里执行 reset 操作进行回退。
|
1
2
3
4
5
6
7
|
D:\myGIT\modb>git reset HEAD helloworld.txtwarning: LF will be replaced by CRLF in helloworld.txt.The file will have its original line endings in your working directory.Unstaged changes after reset:M helloworld.txtD:\myGIT\modb> |
可以看到,文件 helloworld.txt 已经回退到 Unstaged 状态。
重新查看 status 信息,发现状态回到了让我将 commit 进行 push 和将修改 staged for commit 的状态。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
D:\myGIT\modb>git statusOn branch masterYour branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits)Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: helloworld.txtno changes added to commit (use "git add" and/or "git commit -a")D:\myGIT\modb>D:\myGIT\modb>git add .warning: LF will be replaced by CRLF in helloworld.txt.The file will have its original line endings in your working directory.D:\myGIT\modb>D:\myGIT\modb>git commit -m "add string 2 in helloworld.txt"[master warning: LF will be replaced by CRLF in helloworld.txt.The file will have its original line endings in your working directory.793216b] add string 2 in helloworld.txtwarning: LF will be replaced by CRLF in helloworld.txt.The file will have its original line endings in your working directory. 1 file changed, 2 insertions(+), 1 deletion(-)D:\myGIT\modb>D:\myGIT\modb>git statusOn branch masterYour branch is ahead of 'origin/master' by 2 commits. (use "git push" to publish your local commits)nothing to commit, working directory cleanD:\myGIT\modb> |
可以看出,此时本地版本已经处于领先于 origin/master 2 次 commit 的状态。
执行 push 命令将 2 次 commit 进行提交。
|
1
|
D:\myGIT\modb>git push origin master |
注:关于 “warning: LF will be replaced by CRLF” 的问题可以参考《GIT 使用时遇到的行结束符设置问题》。
Git 使用初体验的更多相关文章
- Git学习笔记(windows git之初体验)
阿里国内镜像地址: https://npm.taobao.org/mirrors/git-for-windows/ 最近在学习廖雪峰老师关于git的教程,链接可以在我的首页找到.首先使用国内镜像下载并 ...
- git初体验(三)git分支
分支的理念就是分身,就像孙悟空拔出猴毛变出很多跟自己一模一样的猴子,然后每个猴子做自己的事情互不干涉,等到所有猴子做完之后,猴子集合来合并劳动成果,然后悟空就把那些猴子猴孙门统统收回了. 你创建了一个 ...
- protobuf初体验
概念介绍 Protocol buffers 是google公司的与语言无关.与平台无关的.可扩张的为序列化话结构数据,就像xml一样,办事更加的小巧.快速.简单.Protocol buffers 目前 ...
- node.js 初体验
node.js 初体验 2011-10-31 22:56 by 聂微东, 174545 阅读, 118 评论, 收藏, 编辑 PS: ~ 此篇文章的进阶内容在为<Nodejs初阶之express ...
- grunt 构建工具(build tool)初体验
操作环境:win8 系统,建议使用 git bash (window下的命令行工具) 1,安装node.js 官网下载:https://nodejs.org/ 直接点击install ,会根据你的操 ...
- iOS7初体验(1)——第一个应用程序HelloWorld
iOS7 Beta已经发布了,迫不及待地下载了iOS 7及Xcode 5并体验了一下.先做一个简单的Hello World看看都有哪些变化吧. 1. 启动Xcode5-DP: 2. 从菜单选择File ...
- spring cloud 初体验
spring cloud分为注册端.客户端以及消费端 初体验的理解就是: 注册端就是将之前所有的应用在这边进行注册,然后给每个应用都生成自己的标识,这些应用就是来自于客户端,消费端则通过调用注册端(有 ...
- Hexo初体验
title: Hexo初体验 date: 2018-05-10 tags: Hexo categories: Hexo --- Hexo本地安装 Node.js安装 Hexo npm安装如下 npm ...
- django初体验 学习笔记
django环境搭建 1.安装Python 2.ipython sudo apt-get install ipython sudo pip instal ...
随机推荐
- MemoryStream 转 pdf
在项目开发中用到将MemoryStream 转pdf,在转化过程中需要建了一个.dom格式的模板,先保存为.doc文件,然后再转换为.pdf. 有一个插件感觉好不错,给大家推荐一下. dll下载链接 ...
- sync命令
sync命令用于强制被改变的内容立刻写入磁盘,更新超块信息. 在Linux/Unix系统中,在文件或数据处理过程中一般先放到内存缓冲区中,等到适当的时候再写入磁盘,以提高系统的运行效率.sync命令则 ...
- cookie和session的区别(搜狐笔试考到的一个题目)
一.cookie机制和session机制的区别***************************************************************************** ...
- split分割函数
java.lang.string.split split 方法 将一个字符串分割为子字符串,然后将结果作为字符串数组返回. stringObj.split([separator,[limit]]) 参 ...
- 终于下决心在cnblogs上安家了,^_^
以前在这个地方学到了很多东西,希望在这里安家以后,自己也可以有很多成长. mark一下,~~
- 数据库文件导入导出操作,以及赋予权限SQL语句
1.导出数据库xxxx和tlog(经过测试,没有问题)# /data/mysql/bin/mysqldump -u root -ppassword qq9x | gzip > /home/xxx ...
- nginx 实现Web应用程序的负载均衡
文章转载自 博客园, 原文地址 http://www.cnblogs.com/ivanyb/archive/2011/11/16/2250710.html 看到园子中的大牛代震军写的一篇玩玩负载均衡- ...
- <context:annotation-config> 和 <context:component-scan>的区别
转自:GOOD spring <context:annotation-config> 跟 <context:component-scan>诠释及区别 <context:a ...
- 1.java.io包中定义了多个流类型来实现输入和输出功能,
1.java.io包中定义了多个流类型来实现输入和输出功能,可以从不同的角度对其进行分 类,按功能分为:(C),如果为读取的内容进行处理后再输出,需要使用下列哪种流?(G) A.输入流和输出流 B ...
- AJAX校验商品价格(类似校验用户名)
服务器端程序 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <%@ WebHandler Language=" ...