#系统环境准备:
[root@linux-node2 ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@linux-node2 ~]# uname -r
3.10.0-327.el7.x86_64
[root@linux-node2 ~]# getenforce
Disabled
[root@linux-node2 ~]# yum -y install bash-completion.noarch #安装命令补全包
#git安装部署
[root@linux-node2 ~]# yum -y install git
[root@linux-node2 ~]# git --version
git version 1.8.3.1
[root@linux-node2 ~]# git config --global user.name "Dev-name" #开发者名称
[root@linux-node2 ~]# git config --global user.email "Dev-name@qq.com"
[root@linux-node2 ~]# git config --global color.ui true
[root@linux-node2 ~]# cat .gitconfig
[user]
name = liqi
email = liqi@qq.com
[color]
ui = true
#git初始化
[root@linux-node2 ~]# mkdir git_data
[root@linux-node2 ~]# cd git_data/
[root@linux-node2 git_data]# git init
Initialized empty Git repository in /root/git_data/.git/
[root@linux-node2 git_data]# cd .git/
#隐藏文件介绍
[root@linux-node2 .git]# ll
total 16
drwxr-xr-x 2 root root 6 Nov 7 22:30 branches #分支目录
-rw-r--r-- 1 root root 92 Nov 7 22:30 config #定义项目特有的配置选项
-rw-r--r-- 1 root root 73 Nov 7 22:30 description #仅供git web程序使用
-rw-r--r-- 1 root root 23 Nov 7 22:30 HEAD #指示当前的分支
drwxr-xr-x 2 root root 4096 Nov 7 22:30 hooks #包含git钩子文件
drwxr-xr-x 2 root root 20 Nov 7 22:30 info #包含一个全局排除文件(exclude文件)
drwxr-xr-x 4 root root 28 Nov 7 22:30 objects #存放所有数据内容,有info和pack两个子文件夹
drwxr-xr-x 4 root root 29 Nov 7 22:30 refs #存放指向数据(分支)的提交对象的指针
#还有一个需要手动创建的文件index;它是保存暂存区信息;
#查看状态(需要在工作目录中执行)
[root@linux-node2 git_data]# git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)
#数据推送与拉取的流程图
#git的四种状态
#untracked(未跟踪);unmodified(未被修改);modified(已修改);staged(跟踪)
#git基础命令
[root@linux-node2 git_data]# touch a b c
[root@linux-node2 git_data]# git status # On branch master # # Initial commit # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # a # b # c nothing added to commit but untracked files present (use "git add" to track)
#提交文件
[root@linux-node2 git_data]# git add a [root@linux-node2 git_data]# git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: a # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # b # c
#此时a文件被放在暂存区,也就是前文提到的index中;
[root@linux-node2 git_data]# cd .git/ [root@linux-node2 .git]# ll total 20 drwxr-xr-x 2 root root 6 Nov 7 22:30 branches -rw-r--r-- 1 root root 92 Nov 7 22:30 config -rw-r--r-- 1 root root 73 Nov 7 22:30 description -rw-r--r-- 1 root root 23 Nov 7 22:30 HEAD drwxr-xr-x 2 root root 4096 Nov 7 22:30 hooks -rw-r--r-- 1 root root 96 Nov 7 23:30 index drwxr-xr-x 2 root root 20 Nov 7 22:30 info drwxr-xr-x 5 root root 37 Nov 7 23:30 objects drwxr-xr-x 4 root root 29 Nov 7 22:30 refs [root@linux-node2 .git]# cat index DIRC]´ Ȫ]´ !W¤枢²ҖCK)®wZق䋓aK縕H꒐$@x `
#将文件 a移出暂存区
[root@linux-node2 .git]# git rm --cached a rm 'a' [root@linux-node2 .git]# cd ../ [root@linux-node2 git_data]# git status # On branch master # # Initial commit # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # a # b # c nothing added to commit but untracked files present (use "git add" to track)
#所有未跟踪文件都加到缓存区
[root@linux-node2 git_data]# git add * [root@linux-node2 git_data]# git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: a # new file: b # new file: c #
#提交到本地仓库
[root@linux-node2 git_data]# git commit -m "add newfile a" a [master (root-commit) 8a168ed] add newfile a 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 a
#此时再查看缓存区,发现a文件已经从缓存区移走
[root@linux-node2 git_data]# git commit -m "add newfile a" a [master (root-commit) 8a168ed] add newfile a 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 a [root@linux-node2 git_data]# git status # On branch master # Untracked files: # (use "git add <file>..." to include in what will be committed) # # b # c nothing added to commit but untracked files present (use "git add" to track)
#a文件已经被放到objects文件夹中
#现在修改a文件再查看
[root@linux-node2 git_data]# echo 1 >> a [root@linux-node2 git_data]# git status # On branch 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: a # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # b # c
#此时a文件状态已经变成modified(修改);需要重新提交到本地仓库才能变成跟踪状态
[root@linux-node2 git_data]# git commit -m "modified a" a [master 242ac03] modified a 1 file changed, 1 insertion(+) [root@linux-node2 git_data]# git status # On branch master # Untracked files: # (use "git add <file>..." to include in what will be committed) # # b # c nothing added to commit but untracked files present (use "git add" to track)
#修改文件名
#使用系统命令修改文件名
[root@linux-node2 git_data]# mv a a.txt [root@linux-node2 git_data]# git status # On branch master # Changes not staged for commit: # (use "git add/rm <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # deleted: a # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # a.txt # b # c no changes added to commit (use "git add" and/or "git commit -a")
#根据提示使用“git checkout -- <file>”恢复原文件,但对重命名后的文件没有影响,按照未跟踪的新文件处理;
[root@linux-node2 git_data]# git checkout -- a [root@linux-node2 git_data]# git status # On branch master # Untracked files: # (use "git add <file>..." to include in what will be committed) # # a.txt # b # c nothing added to commit but untracked files present (use "git add" to track)
[root@linux-node2 git_data]# ll
total 8
-rw-r--r-- 1 root root 2 Nov 8 02:17 a
-rw-r--r-- 1 root root 2 Nov 8 02:06 a.txt
-rw-r--r-- 1 root root 0 Nov 7 23:14 b
-rw-r--r-- 1 root root 0 Nov 7 23:14 c
#使用git命令修改文件名
[root@linux-node2 git_data]# git mv a a.txt [root@linux-node2 git_data]# git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # renamed: a -> a.txt # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # b # c
#将修改后的文件提交到本地仓库
[root@linux-node2 git_data]# git commit -m "mv a a.txt" a.txt
[master c70afc0] mv a a.txt
1 file changed, 1 insertion(+)
create mode 100644 a.txt [root@linux-node2 git_data]# git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed) # # b # c
#使用git status无法查看文件内容有哪些修改
[root@linux-node2 git_data]# cat a.txt 1 [root@linux-node2 git_data]# echo test >> a.txt [root@linux-node2 git_data]# git status # On branch 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: a.txt # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # b # c no changes added to commit (use "git add" and/or "git commit -a")
#可以通过git diff查看(git diff 只是比对工作目录与暂存区的不同)
[root@linux-node2 git_data]# git diff a.txt diff --git a/a.txt b/a.txt index d00491f..c0f2f8d 100644 --- a/a.txt +++ b/a.txt @@ -1 +1,2 @@ 1 +test
[root@linux-node2 git_data]# git commit -m "modified add test" a.txt
[master 24a4dcd] modified add test
1 file changed, 1 insertion(+)
#查看暂存区与本地仓库中文件内容的差异
[root@linux-node2 git_data]# echo oldboy >> a.txt [root@linux-node2 git_data]# git status # On branch 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: a.txt # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # b # c no changes added to commit (use "git add" and/or "git commit -a") [root@linux-node2 git_data]# git add a.txt #将文件提交到暂存区 [root@linux-node2 git_data]# git diff #再次对比,此时工作目录与暂存区内容一致
#需要加“--cached”对比暂存区与本地仓库的差异
[root@linux-node2 git_data]# git diff --cached a.txt diff --git a/a.txt b/a.txt index c0f2f8d..b21da30 100644 --- a/a.txt +++ b/a.txt @@ -1,2 +1,3 @@ 1 test +oldboy
[root@linux-node2 git_data]# git commit -m "addfile oldboy" a.txt
[master 819f492] addfile oldboy
1 file changed, 1 insertion(+)
#查看日志
[root@linux-node2 git_data]# git log commit 819f492c6c631951e8cdf3b18770620dc620acbc Author: Dev-name <Dev-name@qq.com> Date: Fri Nov 8 02:54:01 2019 +0800 addfile oldboy commit 24a4dcd90e025c55d67721ba85fadc99ffee3e14 Author: Dev-name <Dev-name@qq.com> Date: Fri Nov 8 02:36:15 2019 +0800 modified add test commit c70afc05e3b516ba3c6e50aa2cc3cd9ac43d8b3d Author: Dev-name <Dev-name@qq.com> Date: Fri Nov 8 02:25:24 2019 +0800 mv a a.txt
#简化日志
[root@linux-node2 git_data]# git log --oneline
819f492 addfile oldboy
24a4dcd modified add test
c70afc0 mv a a.txt
10b10f8 mv a a.txt
242ac03 modified a
8a168ed add newfile a
#查看指针
[root@linux-node2 git_data]# git log --oneline --decorate
819f492 (HEAD, master) addfile oldboy
24a4dcd modified add test
c70afc0 mv a a.txt
10b10f8 mv a a.txt
242ac03 modified a
8a168ed add newfile a
#查看每次具体的操作
[root@linux-node2 git_data]# git log -p commit 819f492c6c631951e8cdf3b18770620dc620acbc Author: Dev-name <Dev-name@qq.com> Date: Fri Nov 8 02:54:01 2019 +0800 addfile oldboy diff --git a/a.txt b/a.txt index c0f2f8d..b21da30 100644 --- a/a.txt +++ b/a.txt @@ -1,2 +1,3 @@ 1 test +oldboy commit 24a4dcd90e025c55d67721ba85fadc99ffee3e14 Author: Dev-name <Dev-name@qq.com> Date: Fri Nov 8 02:36:15 2019 +0800 modified add test diff --git a/a.txt b/a.txt index d00491f..c0f2f8d 100644 --- a/a.txt +++ b/a.txt @@ -1 +1,2 @@ 1 +test
#查看最近n次操作记录
[root@linux-node2 git_data]# git log -2
* 参考oldboy视频整理