【原创】个人 Git 使用初体验


      好久以前在 http://git.oschina.net/ 上建立了一个私有项目 modb ,目的主要是用来学习如何使用 GIT 来开源本身写的东东,中间因为种种缘由停顿了很长时间,可是今天,我下定决心必定要将这个事情完成,因而乎,探索之旅又开始了……

(本文以 windows 平台上的操做进行说明)

最初建立 modb 项目时,默认会产生以下 3 个文件:
  • .gitignore
  • LICENSE
  • README.md
其中 .gitignore 文件的做用能够参考: .gitignore 文件使用说明

接下来只要从官网下载了最新的 Git 客户端安装使用就能够了,我安装的是最新的 Git-1.9.2-preview20140411.exe

首先,将 modb.git 获取到本地。
D:\myGIT>git clone https://git.oschina.net/moooofly/modb.git
Cloning into 'modb'...
Username for 'https://git.oschina.net': moooofly
Password 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。
D:\myGIT>cd modb
D:\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 .gitignore
2014-04-28  14:05             1,094 LICENSE
2014-04-28  14:05                 7 README.md
2014-04-28  14:14                 0 helloworld.txt
               4 个文件          1,257 字节
               2 个目录  6,756,630,528 可用字节

D:\myGIT\modb>
经过 add 命令添加新建的文件,经过 status 命令查看此时的状态信息,经过 commit 命令在本地提交变动状态。
D:\myGIT\modb>git add .

D:\myGIT\modb>
D:\myGIT\modb>git status
On branch master
Your branch is up-to-date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   helloworld.txt


D:\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 based
on 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.com

After 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.txt

D:\myGIT\modb>
      看上面的提示,以系统默认的用户名和密码来进行代码的管理彷佛不妥。按照 oschina/git-osc 和《Git初体验的说法,应该使用在 GIT@OSC 上注册的用户名和邮箱。
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.txt

D:\myGIT\modb>
在输入命令 git commit --amend --reset-author 时,会以 VIM 编辑器的形式打开以下内容的文件。
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 命令向服务器提交。
D:\myGIT\modb>
D:\myGIT\modb>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

D:\myGIT\modb>git push origin master
Username for 'https://git.oschina.net': moooofly
Password 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 -> master

D:\myGIT\modb>
此时刷新项目网址,能够看到新的文件已经成功提交了(项目为私有,目前只有我本身能看到)。

接着测试修改文件内容的状况,在 helloworld.txt 文件中增长
hello world! haha!
以后查看状态
D:\myGIT\modb>git status
On branch master
Your 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.txt

no 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 。
D:\myGIT\modb>
D:\myGIT\modb>git log
commit 136a5da602fbba228c51cb7f680f1784bea1e6af
Author: moooofly <centos.sf@gmail.com>
Date:   Mon Apr 28 15:11:53 2014 +0800

    add helloworld.txt

commit 8fb8c6358323d3213b244355fa0e9df0e28a3b0d
Author: 摩云飞 <centos.sf@gmail.com>
Date:   Thu Jan 2 18:23:10 2014 +0800

    Initial commit

D:\myGIT\modb>
此时查看 log 信息,能够看到仅有最初建立和刚刚添加 helloworld.txt 文件时的日志内容。

再次执行 add 和 commit 命令,并查看相关状态信息。
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 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

D:\myGIT\modb>
      上述打印能够看出,个人本地代码版本已经超前 'origin/master' 分支 1 个 commit 了。此时已经没有其余须要 commit 的修改,只须要执行 push 操做将本地修改推到 GIT 服务器端。
D:\myGIT\modb>git log
commit 1c01bff84483507b428eecd4fff7bbe89467dcce
Author: moooofly <centos.sf@gmail.com>
Date:   Mon Apr 28 16:38:16 2014 +0800

    add string in helloworld.txt

commit 136a5da602fbba228c51cb7f680f1784bea1e6af
Author: moooofly <centos.sf@gmail.com>
Date:   Mon Apr 28 15:11:53 2014 +0800

    add helloworld.txt

commit 8fb8c6358323d3213b244355fa0e9df0e28a3b0d
Author: 摩云飞 <centos.sf@gmail.com>
Date:   Thu Jan 2 18:23:10 2014 +0800

    Initial commit

D:\myGIT\modb>
从上述打印能够知道,只要执行过 commit 就会在 log 中体现出来。

此时不执行 push 动做,而是再次修改文件的内容,增长
hello moooofly! haha!
以后查看状态
D:\myGIT\modb>git status
On branch master
Your 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.txt

no changes added to commit (use "git add" and/or "git commit -a")

D:\myGIT\modb>
果真……同时出现了让我将(前面的)commit 进行 push 和将(后面的)修改 staged for commit 的建议。

这里我选择执行 add 命令,结果出现了关于行结束的警告,这个暂时跳过不处理。
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 状态。
D:\myGIT\modb>git status
warning: LF will be replaced by CRLF in helloworld.txt.
The file will have its original line endings in your working directory.
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:
  (use "git reset HEAD <file>..." to unstage)

        modified:   helloworld.txt


D:\myGIT\modb>
这里执行 reset 操做进行回退。
D:\myGIT\modb>git reset HEAD helloworld.txt
warning: 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.txt

D:\myGIT\modb>
能够看到,文件 helloworld.txt 已经回退到 Unstaged 状态。

从新查看 status 信息,发现状态回到了让我将 commit 进行 push 和将修改 staged for commit 的状态。
D:\myGIT\modb>git status
On branch master
Your 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.txt

no 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.txt
warning: 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 status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

nothing to commit, working directory clean

D:\myGIT\modb>
能够看出,此时本地版本已经处于领先于 origin/master 2 次 commit 的状态。

执行 push 命令将 2 次 commit 进行提交。
D:\myGIT\modb>git push origin master
注:关于 “warning: LF will be replaced by CRLF” 的问题能够参考《GIT 使用时遇到的行结束符设置问题》。
相关文章
相关标签/搜索