Git 使用快速入门

来源:互联网 发布:做淘宝客怎么拉微信群 编辑:程序博客网 时间:2024/06/05 05:50

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

$ git initInitialized empty Git repository in D:/Git/.git/

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

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

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

$ git statusOn branch masterInitial commitChanges 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.txthello worldhi$ git commit hello.txt

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

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

$ git logcommit 714bc280a9a237a986021357700b2af3226fe663Author: Ang <1193530304@qq.com>Date:   Wed Aug 2 15:42:56 2017 +0800        my first revisioncommit f857d79d4d83ed4906895f3f3597d86c765aa22cAuthor: Ang <1193530304@qq.com>Date:   Wed Aug 2 15:41:06 2017 +0800    Initial contents of hello

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

$ git show 714bc280a9a237a986021357700b2af3226fe663commit 714bc280a9a237a986021357700b2af3226fe663Author: Ang <1193530304@qq.com>Date:   Wed Aug 2 15:42:56 2017 +0800        my first revisiondiff --git a/hello.txt b/hello.txtindex 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 \>  714bc280a9a237a986021357700b2af322diff --git a/hello.txt b/hello.txtindex 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 在版本库里实现这个变更。

$ lshello.txt$ git rm hello.txtrm '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.txtrm '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_websiteCloning into 'my_website'...done.
原创粉丝点击