西数超哥博客
运维经验教程分享

git基础:通过git如何进行初始化配置、创建仓库、工作区修改保存提交、查看状态、查看提交历史、查看提交差异、克隆仓库

21运维原创,转载请注明出处。
之前记录了文章windows和linux下如何安装git,今天记录下如何进行git的一些基础操作。包括如下基础内容:
通过git如何进行初始化配置、创建仓库、工作区修改保存提交、查看状态、查看提交历史、查看提交差异、克隆仓库。这里以之前linux安装的git为例进行操作,涉及到如下命令:
git config
git init
git add
git commit
git diff
git status
git log
git show
git clone
1,初始化配置
服务器配置好以后,需要进行常规配置。比如:

[root@21yunwei 21yunwei]# git config  --global user.name  "21yunwei"
[root@21yunwei 21yunwei]# git config  --global user.email "admin@21yunwei.com"

延伸部分:
(1)如果要进行颜色显示,比如文件的新建、修改等等要加以颜色显示,还需要进行如下配置:

git config --global color.ui true

(2)设置别名。有些命令可能想要偷懒,可以设置一个别名,比如对初始化进行别名设置,我们将init用it代替:

[root@21yunwei 21yunwei]# git config --global alias.it init

那以后进行初始化的时候就可以直接使用git it 代替git init了。
(3)命令补全
如果需要,可以到https://github.com/markgandolfo/git-bash-completion 进行设置。可以操作的时候进行git命令补全功能,类似linux下的tab命令补全。
(4)配置全局以及局部

git config --global 修改的当前用户,修改的用户级文件~/.gitconfig
git config --system 修改当前系统,包含所有用户,修改的系统文件/etc/gitconfig
git config 只针对当前仓库,修改文件是当前仓库下的.git/config

其中.git优先级最低。比如我们设置了git config 设置了其他用户或邮箱,提交的时候会显示这里的信息,用户信息也可以到.git/config查看。

2,创建仓库

[root@21yunwei home]# mkdir -p repos/21yunwei #创建仓库总目录以及项目
[root@21yunwei repos]# cd 21yunwei/
[root@21yunwei 21yunwei]# git init   #初始化版本库
已初始化空的 Git 仓库于 /home/repos/21yunwei/.git/
[root@21yunwei 21yunwei]# ll  -a
总用量 16
drwxr-xr-x 3 root root 4096 11月 17 23:00 .
drwxr-xr-x 4 root root 4096 11月 17 22:56 ..
drwxr-xr-x 8 root root 4096 11月 17 23:00 .git
[root@21yunwei 21yunwei]# ll  .git/
总用量 44
drwxr-xr-x 2 root root 4096 11月 17 22:56 branches
-rw-r--r-- 1 root root   17 11月 17 23:00 COMMIT_EDITMSG
-rw-r--r-- 1 root root   92 11月 17 22:56 config
-rw-r--r-- 1 root root   73 11月 17 22:56 description
-rw-r--r-- 1 root root   23 11月 17 22:56 HEAD
drwxr-xr-x 2 root root 4096 11月 17 22:56 hooks
-rw-r--r-- 1 root root  137 11月 17 22:58 index
drwxr-xr-x 2 root root 4096 11月 17 22:56 info
drwxr-xr-x 3 root root 4096 11月 17 22:59 logs
drwxr-xr-x 7 root root 4096 11月 17 22:59 objects
drwxr-xr-x 4 root root 4096 11月 17 22:56 refs

好了,仓库初始化完毕

3,工作区修改、暂存区保存和提交到版本库
文件进行工作区操作,修改以后add到暂存区,最后commit提交到版本库。

 

 

[root@21yunwei 21yunwei]#echo  "hello git!" >index.php

[root@21yunwei 21yunwei]# git status
位于分支 master

初始提交

未跟踪的文件:
(使用 "git add <文件>..." 以包含要提交的内容)

index.php

提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
[root@21yunwei 21yunwei]# git add index.php 
[root@21yunwei 21yunwei]# git status
位于分支 master

初始提交

要提交的变更:
(使用 "git rm --cached <文件>..." 以取消暂存)

新文件: index.php
[root@21yunwei 21yunwei]# git commit -m "add index.php"
[master(根提交) 07f2f56] add index.php
 1 file changed, 1 insertion(+)
 create mode 100644 index.php
[root@21yunwei 21yunwei]# git status
位于分支 master
无文件要提交,干净的工作区
[root@21yunwei 21yunwei]# git log
commit 07f2f56778d36355bc69d210c5f850cd99d3c2c7
Author: oldboy <705754153@qq.com>
Date:   Thu Nov 17 22:59:28 2016 +0800

    add index.php

4,查看状态
命令git status,可以查看当前文件是否提交到暂存区等。

[root@21yunwei 21yunwei]# git status
位于分支 master

初始提交

要提交的变更:
(使用 "git rm --cached <文件>..." 以取消暂存)

新文件: index.php

5,查看提交历史
命令git log,查看提交历史情况:

[root@21yunwei 21yunwei]# git log
commit 07f2f56778d36355bc69d210c5f850cd99d3c2c7
Author: oldboy <705754153@qq.com>
Date:   Thu Nov 17 22:59:28 2016 +0800

    add index.php

6,查看提交差异
通过命令git diff 查看:

[root@21yunwei 21yunwei]# git diff 
diff --git a/index.php b/index.php
index d92b44e..16357af 100644
--- a/index.php
+++ b/index.php
@@ -1 +1,2 @@
 hello  git!
+update index.php

7,克隆仓库
这个非常使用,尤其是下载githut上的一些资源的时候。比如我们命令补全脚本就可以通过如下方式下载:

[root@21yunwei 21yunwei]# git clone https://github.com/markgandolfo/git-bash-completion.git
正克隆到 'git-bash-completion'...
remote: Counting objects: 23, done.
remote: Total 23 (delta 0), reused 0 (delta 0), pack-reused 23
展开对象中: 100% (23/23), 完成.

暂时记录这些基础操作。

转载请注明:西数超哥博客www.ysidc.top» git基础:通过git如何进行初始化配置、创建仓库、工作区修改保存提交、查看状态、查看提交历史、查看提交差异、克隆仓库

https://www.ysidc.top 西数超哥博客,数据库,西数超哥,虚拟主机,域名注册,域名,云服务器,云主机,云建站,ysidc.top

赞(0)
声明:本站发布的内容(图片、视频和文字)以原创、转载和分享网络内容为主,若涉及侵权请及时告知,将会在第一时间删除。本站原创内容未经允许不得转载:西数超哥博客 » git基础:通过git如何进行初始化配置、创建仓库、工作区修改保存提交、查看状态、查看提交历史、查看提交差异、克隆仓库