Git 使用快速入门

一、创建初始版本库:
执行 git init,将当前目录转化为Git版本库。

$ git init
Initialized empty Git repository in D:/Git/.git/

二、将文件添加到版本库中:
使用 git add filefile 添加到版本库中

$ echo 'hello world' > hello.txt
$ git add hello.txt

运行 git status 命令,显示中间状态的 hello.txt 。

$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   hello.txt

一条完全限定的 git commit 命令必须提供日志消息和作者。

$ git commit -m "Initial contents of hello"
[master (root-commit) 8d290ef] Initial contents of hello
 1 file changed, 1 insertion(+)
 create mode 100644 hello.txt

可以在命令中提供一条日志消息,但更典型的做法是在交互式编辑器会话期间创建消息。为了在 git commit 期间让 Git 打开你喜欢的编辑器,要设置你的 GIT_EDITOR环境变量。

$ export GIT_EDITOR=vim

把新文件提交到版本库中后, git status 命令显示没有突出的、暂存的变更需要文件。

三、配置提交作者:
可以用 git config 命令在配置文件中保存你的身份.

$ git config user.name "Ang"
$ git config user.emal "1193530304@qq.com"

也可以使用 GIT_AUEHOR_NAME 和 GIT_AUTHOR_EMAIL 环境变量来告诉Git你的姓名和email地址。这些变量一旦设置就会覆盖所有的配置设置。

四、再次提及:
来提交一次对 hello.txt 文件的修改。

$ cat hello.txt
hello world

hi

$ git commit hello.txt

在这里不需要执行 git add hello.txt,因为这个文件已经存在版本库里了。

五、查看提交:
git log 命令会产生版本库里一系列单独提交的历史。

$ git log
commit 714bc280a9a237a986021357700b2af3226fe663
Author: Ang <1193530304@qq.com>
Date:   Wed Aug 2 15:42:56 2017 +0800

        my first revision

commit f857d79d4d83ed4906895f3f3597d86c765aa22c
Author: Ang <1193530304@qq.com>
Date:   Wed Aug 2 15:41:06 2017 +0800

    Initial contents of hello

为了查看特定提交的更加详细的信息,可以使用 git show 命令带一个提交码。

$ git show 714bc280a9a237a986021357700b2af3226fe663
commit 714bc280a9a237a986021357700b2af3226fe663
Author: Ang <1193530304@qq.com>
Date:   Wed Aug 2 15:42:56 2017 +0800

        my first revision

diff --git a/hello.txt b/hello.txt
index 3b18e51..e30a896 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1 +1,3 @@
 hello world
+
+hi
\ No newline at end of file

如果在执行 git show 命令的时候没有显示指定提交码,它将只显示最近一次提交的详细信息。
另一种查看方式是使用 show-branch,提供当前开发分支简介的单行摘要。

$ git show-branch --more=10
[master]        my first revision
[master^] Initial contents of hello

参数“- -more=10”表示额外的10个版本,此处只有两个版本。

六、查看提交差异:
使用两个提交的全ID名并运行 git diff。

$ git diff f857d79d4d83ed4906895f3f3597d86c765aa22c \
>  714bc280a9a237a986021357700b2af322
diff --git a/hello.txt b/hello.txt
index 3b18e51..e30a896 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1 +1,3 @@
 hello world
+
+hi
\ No newline at end of file

七、版本库内文件的删除和重命名:
使用 git rm 命令从版本库中删除一个文件。接着使用 git commit 在版本库里实现这个变更。

$ ls
hello.txt

$ git rm hello.txt
rm 'hello.txt'

$ git commit -m "remove"
[master 8734a78] remove
 1 file changed, 3 deletions(-)
 delete mode 100644 hello.txt

可以通过 git rm 和 git add 组合命令来间接为一个文件重命名

$ mv foo.txt bar.txt
$ git rm foo.txt
rm 'foo.txt'
$ git add bar.txt

也可以直接使用 git mv 命令。

$ git mv foo.txt bar.txt

在任意一种情况下,暂存的变更必须随后进行提交。

$ git commit -m "moved foo to bar"
[master a1e7a27] moved foo to bar
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename foo.txt => bar.txt (100%)

八、创建版本库副本:
之前在 D:/Git/git 目录中建立了一个初始版本库,就可以通过 git clone 命令创建一个完整的副本。
在 Git 目录里建立一个副本,并命名为 my_website。

$ cd ..

$ git clone git my_website
Cloning into 'my_website'...
done.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值