Git系列二之数据管理

1.Git基本管理

git常用的基本操作

1.1提交数据

我们可以简单的把工作目录理解成是一个被Git服务程序管理的目录,Git会时刻的追踪目录内文件的改动,另外在安装好了Git服务程序后,默认就会创建好了一个叫做master的分支,我们直接可以提交数据到了

1.创建本地工作目录oldboy,并初始化为gi


 
 
  1. [root@linux-node1 ~]# mkdir oldboy ##这个目录就是一个仓库
  2. [ root@linux- node1 ~]# cd oldboy/
    [root@linux-node1 oldboy]# git init
    Initialized empty Git repository in /root/oldboy/.git/
    [root@linux-node1 oldboy]# ls -la
    总用量 12
    drwxr-xr-x 3 root root 4096 1月 20 11:49 .
    dr-xr-x---. 10 root root 4096 1月 20 11:48 ..
    drwxr-xr-x 7 root root 4096 1月 20 11:49 .git


2.创建readme.txt文件

 
 
  1. [root@linux-node1 oldboy]# vim readme.txt
    hehe

3.查看git状态

 
 
  1. [root@linux-node1 oldboy]# git status
    # On branch master
    #
    # Initial commit
    #
    # Untracked files:
    # (use "git add <file>..." to include in what will be committed)
    #
    # readme.txt
    nothing added to commit but untracked files present (use "git add" to track)

4.提示使用git add添加文件至暂存区

 
 
  1. [root@linux-node1 oldboy]# git add readme.txt
    [root@linux-node1 oldboy]# git status
    # On branch master
    #
    # Initial commit
    #
    # Changes to be committed:
    # (use "git rm --cached <file>..." to unstage)
    #
    # new file: readme.txt

5.使用git cmmit提交暂存区文件至git版本仓库 -m:提交描述信息(提交至远程需添加远程仓库)

 
 
  1. [root@ linux-node1 oldboy]# git commit -m "the first commit"
    [master (root-commit) 92e0f4f] the first commit
    1 files changed, 1 insertions(+), 0 deletions(-)
    create mode 100644 readme.txt
    [root@linux-node1 oldboy]# git status
    # On branch master
    nothing to commit (working director y clean)
    [root@linux-node1 oldboy]# vim deploy.sh
    #!/bin/bash
    echo hehe
    [root@linux-node1 oldboy]# cat deploy.sh
    #!/bin/bash
    echo hehe
    [root@linux-node1 oldboy]# git status
    # On branch master
    # Untracked files:
    # (use "git add <file>..." to include in what will be committed)
    #
    # deploy.sh
    nothing added to commit but untracked files present (use "git add" to track)
    [root@linux-node1 oldboy]# git add deploy.sh
    [root@linux-node1 oldboy]# git commit -m "2th commit"
    [master e5689ee] 2th commit
    1 files changed, 2 insertions(+), 0 deletions(-)
    create mode 100644 deploy.sh
    [root@linux-node1 oldboy]# ll
    总用量 8
    -rw-r--r-- 1 root root 22 1月 20 13:51 deploy.sh
    -rw-r--r-- 1 root root 7 1月 20 13:47 readme.txt
    [root@linux-node1 oldboy]# git log
    commit e5689eeca892c838f95b6c75591a3c55a4adfe88
    Author: sundandan <sundandan@gmail.com>
    Date: Fri Jan 20 13:52:13 2017 +0800
    2th commit
    commit 92e0f4f60bded4b7b19eb663bf57e0bbc121c199
    Author: sundandan <sundandan@gmail.com>
    Date: Fri Jan 20 13:50:05 2017 +0800
    the first commit
    [root@linux-node1 oldboy]# vim readme.txt
    1 hehe
    2 hehe
    [root@linux-node1 oldboy]# cat readme.txt
    1 hehe
    2 hehe
    [root@linux-node1 oldboy]# git status
    # On branch master
    # Changed but not updated:
    # (use "git add <file>..." to update what will be committed)
    # (use "git checkout -- <file>..." to discard changes in working directory)
    #
    # modified: readme.txt
    #
    no changes added to commit (use "git add" and/or "git commit -a")
    [root@linux-node1 oldboy]# git diff readme.txt
    diff --git a/readme.txt b/readme.txt
    index 408e625..833bc94 100644
    --- a/readme.txt
    +++ b/readme.txt
    @@ -1 +1,2 @@
    1 hehe
    +2 hehe
    [root@linux-node1 oldboy]# git add readme.txt
    [root@linux-node1 oldboy]# git commit -m "add 2hehe"
    [master 7bec572] add 2hehe
    1 files changed, 1 insertions(+), 0 deletions(-)
    [root@linux-node1 oldboy]# git log
    commit 7bec57206401f756f43cdc5ddcceaaff2cdbe3bb
    Author: sundandan <sundandan@gmail.com>
    Date: Fri Jan 20 13:54:21 2017 +0800
    add 2hehe
    commit e5689eeca892c838f95b6c75591a3c55a4adfe88
    Author: sundandan <sundandan@gmail.com>
    Date: Fri Jan 20 13:52:13 2017 +0800
    2th commit
    commit 92e0f4f60bded4b7b19eb663bf57e0bbc121c199
    Author: sundandan <sundandan@gmail.com>
    Date: Fri Jan 20 13:50:05 2017 +0800
    the first commit

1.2移除数据

有些时候会将已经添加到暂存区的文件移除,但仍然希望文件在工作目录中不丢失,换句话说,就是把文件从追踪清单中删除。

1.建立新文件

 
 
  1. [root@git-node1 demo]# touch index.php

2.添加新文件至暂存区

 
 
  1. [root@git-node1 demo]# git add index.php
  2. [root@git-node1 demo]# git status
  3. # 位于分支 master
  4. # 要提交的变更:
  5. # (使用 "git reset HEAD <file>..." 撤出暂存区)
  6. #
  7. # 新文件: index.php
  8. #

3.将新文件从暂存区撤出(并不会删除工作目录内的数据)

 
 
  1. [root@git-node1 demo]# git reset HEAD index.php
  2. [root@git-node1 demo]# git status //index.php文件状态为未跟踪
  3. # 位于分支 master
  4. # 未跟踪的文件:
  5. # (使用 "git add <file>..." 以包含要提交的内容)
  6. #
  7. # index.php
  8. 提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
  9. 如果想将文件数据从git暂存区和工作目录一起删除,可以做如下操作。
  10. [root@git-node1 demo]# git add index.php
  11. [root@git-node1 demo]# git rm index.php //文件存已经放入暂存区域,防止误删
  12. error: 'index.php' 有变更已暂存至索引中
  13. (使用 --cached 保存文件,或用 -f 强制删除)
  14. [root@git-node1 demo]# git rm -f index.php //使用追加强制删除-f参数。或者—cache保存
  15. rm 'index.php'
  16. [root@git-node1 demo]# git status //查看当前状态
  17. # 位于分支 master
  18. 无文件要提交,干净的工作区
  19. [root@git-node1 demo]# ls //index.php文件已经被删除
  20. index.html

1.3移动数据

有些时候会将已经添加到暂存区的文件重新修改名称,可使用如下操作完成。
1.git重新命名文件名称,使用git mv操作。

 
 
  1. [root@git-node1 demo]# git mv index.html index.php

2.使用git status查看更改状态

 
 
  1. [root@git-node1 demo]# git status
  2. # 位于分支 master
  3. # 要提交的变更:
  4. # (使用 "git reset HEAD <file>..." 撤出暂存区)
  5. #
  6. # 重命名: index.html -> index.php
  7. #

3.使用git commit 提交git仓库,并描述提交信息

 
 
  1. [root@git-node1 demo]# git commit -m "chnged name index.html->index.php"
  2. [master 9573413] chnged name index.html->index.php
  3. 1 file changed, 0 insertions(+), 0 deletions(-)
  4. rename index.html => index.php (100%)

1.4历史数据

在提交了若干更新之后,想回顾下提交历史,可以使用 git log 命令查看提交历史记录。
1.查看提交历史记录

 
 
  1. [root@git-node1 demo]# git log
  2. commit 95734131860ef7c9078b8a208ff6437d0952380b
  3. Author: xuliangwei <xuliangwei@foxmail.com>
  4. Date: Sun Nov 6 02:15:35 2016 +0800
  5. chnged name index.html->index.php
  6. commit 85bd2680bd4b70aeded9dbd230c07ab086712ff9
  7. Author: xuliangwei <xuliangwei@foxmail.com>
  8. Date: Sun Nov 6 01:51:22 2016 +0800
  9. the first commit index.html

2.查看历史2次提交记录

 
 
  1. [root@git-node1 demo]# git log -10
  2. commit 95734131860ef7c9078b8a208ff6437d0952380b
  3. Author: xuliangwei <xuliangwei@foxmail.com>
  4. Date: Sun Nov 6 02:15:35 2016 +0800
  5. chnged name index.html->index.php
  6. commit 85bd2680bd4b70aeded9dbd230c07ab086712ff9
  7. Author: xuliangwei <xuliangwei@foxmail.com>
  8. Date: Sun Nov 6 01:51:22 2016 +0800
  9. the first commit index.html

3.显示提交的内容差异,例如仅查看最近一次差异

 
 
  1. [root@git-node1 demo]# git log -p -2
  2. commit 95734131860ef7c9078b8a208ff6437d0952380b
  3. Author: xuliangwei <xuliangwei@foxmail.com>
  4. Date: Sun Nov 6 02:15:35 2016 +0800
  5. chnged name index.html->index.php
  6. diff --git a/index.html b/index.html
  7. deleted file mode 100644
  8. index e69de29..0000000
  9. diff --git a/index.php b/index.php
  10. new file mode 100644
  11. index 0000000..e69de29
  12. commit 85bd2680bd4b70aeded9dbd230c07ab086712ff9
  13. Author: xuliangwei <xuliangwei@foxmail.com>
  14. Date: Sun Nov 6 01:51:22 2016 +0800
  15. the first commit index.html
  16. diff --git a/index.html b/index.html
  17. new file mode 100644
  18. index 0000000..e69de29
  1. 简要显示数据增改行数
 
 
  1. [root@git-node1 demo]# git log --stat
  2. commit 95734131860ef7c9078b8a208ff6437d0952380b
  3. Author: xuliangwei <xuliangwei@foxmail.com>
  4. Date: Sun Nov 6 02:15:35 2016 +0800
  5. chnged name index.html->index.php
  6. index.html | 0
  7. 2 files changed, 0 insertions(+), 0 deletions(-)
  8. commit 85bd2680bd4b70aeded9dbd230c07ab086712ff9
  9. Author: xuliangwei <xuliangwei@foxmail.com>
  10. Date: Sun Nov 6 01:51:22 2016 +0800
  11. the first commit index.html
  12. index.html | 0
  13. 1 file changed, 0 insertions(+), 0 deletions(-)

5.根据不同的格式展示提交的历史信息

 
 
  1. [root@git-node1 demo]# git log --pretty=oneline
  2. 95734131860ef7c9078b8a208ff6437d0952380b chnged name index.html->index.php
  3. 85bd2680bd4b70aeded9dbd230c07ab086712ff9 the first commit index.htm
  4. 可以使用format参数来指定具体的输出格式,这样非常便于后期编程的提取分析哦,常用的格式有:
  5. %s 提交说明。
  6. %cd 提交日期。
  7. %an 作者的名字。
  8. %cn 提交者的姓名。
  9. %ce 提交者的电子邮件。
  10. %H 提交对象的完整SHA-1哈希字串。
  11. %h 提交对象的简短SHA-1哈希字串。
  12. %T 树对象的完整SHA-1哈希字串。
  13. %t 树对象的简短SHA-1哈希字串。
  14. %P 父对象的完整SHA-1哈希字串。
  15. %p 父对象的简短SHA-1哈希字串。
  16. %ad 作者的修订时间。
  17. 定制详细的日志输出
  18. [root@git-node1 demo]# git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset %cn"' --abbrev-commit --date=relative
  19. * 5f3f588 - (HEAD, origin/master, r, tt, master) merge branch linux readme.txt (68 分钟之前) xulia
  20. |\
  21. | * 4580c3b - (linux) touch readme.txt (80 分钟之前) xuliangwei"
  22. * | 4c7a145 - touch readme.txt (72 分钟之前) xuliangwei"
  23. |/
  24. * 9573413 - (tag: v1.0.0) chnged name index.html->index.php (5 小时之前) xuliangwei"
  25. * 85bd268 - the first commit index.html (5 小时之前) xuliangwei"
  26. 配置.git/config文件,增加如下2行,以后即可使用 git hlog代替如上复杂命令
  27. [root@git-node1 demo]# tail -2 .git/config
  28. [alias]
  29. hlog = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset %cn' --abbrev-commit --date=relative
  30. //以后使用git hlog即可实现复杂的命令

1.5还原数据

将文件boss good写成了boss dou,但是已经提交至远程仓库,现在想要撤回重新提交。
(本小结涉及远程仓库,可先学习后面章节,在回头学习本章节)
1.查看index.html文件

 
 
  1. [root@git-node1 demo]# git checkout -b dev //内容存在dev分支
  2. [root@git-node1 demo]# cat index.html
  3. hello boss
  4. [root@git-node1 demo]# echo "boos doubi" >> index.html

2.追加内容至index.html,并提交至远程仓库

 
 
  1. [root@git-node1 demo]# git add index.html
  2. [root@git-node1 demo]# git commit -m "boss dou"
  3. [master 1941990] boss dou
  4. 1 file changed, 1 insertion(+)
  5. [root@git-node1 demo]# git push origin dev
  6. Counting objects: 5, done.
  7. Compressing objects: 100% (2/2), done.
  8. Writing objects: 100% (3/3), 298 bytes | 0 bytes/s, done.
  9. Total 3 (delta 0), reused 0 (delta 0)
  10. To git@git-node1:root/git_demo.git
  11. 507bf99..1941990 dev -> dev

3.此时觉得写的不妥,想还原上一次提交的文件快照,找出历史信息

 
 
  1. [root@git-node1 demo]# git hlog -2
  2. * 1941990 - (HEAD, origin/dev, dev) boss dou (2 分钟之前) xuliangwei
  3. * 507bf99 - hello (30 分钟之前) xuliangwei

4.找到历史还原点的值后,就可以还原(值不写全,系统会自动匹配)

 
 
  1. [root@git-node1 demo]# git reset --hard 507bf99
  2. HEAD 现在位于 507bf99 hello
  3. [root@git-node1 xuliangwei]# git push origin dev -f //强制推送至dev分支
  4. [root@git-node1 demo]# cat index.html
  5. hello boss

1.6基本操作小结

相信大家对Git的基本操作有一定的了解,下面进行一下简单的梳理总结:

 
 
  1. 命令 git config --global user.name "name" #配置git使用用户
  2. # git config --global user.email "mail" #配置git使用邮箱
  3. # git config --global color.ui true #配置颜色
  4. # git config --list #查看当前配置
  5. # git init #初始为git工作目录
  6. # git status #查看git状态
  7. # git reflog #查看未来历史更新点
  8. # git reset --hard 4bf5b29 #找到历史还原点的SHA-1值,就可以还原(值不写全,系统会自动匹配)
  9. # git checkout -- file #恢复暂存区至上一版本
  10. # git add [file1] [file2] ... #添加指定文件至暂存区
  11. # git add [dir] #添加指定目录至暂存区,包括子目录(递归添加)
  12. # git add . #添加当前目录所有文件至暂存区
  13. # git rm [file1] [file2] ... #删除工作区文件,并将这次删除放入暂存区
  14. # git rm –cached [file] #停止追踪指定文件,但该文件会保留在工作区
  15. # git mv [file-old] [file-new] #重命名文件,修改后放入暂存区





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值