【Git】基础-1

文章目录

    • 1.安装
    • 2.git入门
    • 3.git config

1.安装

# yum install git

检查git的版本

# git --version
git version 1.8.3.1
You have new mail in /var/spool/mail/root

2.git入门

(1)创建一个工程目录

# mkdir lession01

(2)将工程目录初始化

# cd lession01/
# git init

(3)初始化完成后,git在当前工程创建了.git文件夹,存放git相关的内容

# ls -al
total 0
drwxr-xr-x 3 root root  18 Aug  4 08:54 .
drwxr-xr-x 4 root root  40 Aug  4 08:48 ..
drwxr-xr-x 7 root root 119 Aug  4 08:54 .git
# cd .git
# ll
total 12
drwxr-xr-x 2 root root   6 Aug  4 08:54 branches
-rw-r--r-- 1 root root  92 Aug  4 08:54 config
-rw-r--r-- 1 root root  73 Aug  4 08:54 description
-rw-r--r-- 1 root root  23 Aug  4 08:54 HEAD
drwxr-xr-x 2 root root 242 Aug  4 08:54 hooks
drwxr-xr-x 2 root root  21 Aug  4 08:54 info
drwxr-xr-x 4 root root  30 Aug  4 08:54 objects
drwxr-xr-x 4 root root  31 Aug  4 08:54 refs

(4)在工程目录下创建一个新的文件

# echo "hello git" >> a.txt

(5)查看git status

# git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add ..." to include in what will be committed)
#
#	a.txt
nothing added to commit but untracked files present (use "git add" to track)

(6)将新增的文件加入到git的index域

# git add a.txt

(7)查看git status

# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached ..." to unstage)
#
#	new file:   a.txt

(8)提交

# git commit -m "first commit"

(9)查看提交记录

# git log
commit ac0c6182c3f10601c6e59ee056678d33ce04b820
Author: jim19Green <[email protected]>
Date:   Tue Aug 4 09:04:46 2020 +0800

    first commit
# git log --oneline
ac0c618 first commit

(10)修改a.txt的内容,git能够发现文件的变动

# echo "111111" >> a.txt
# git status
# On branch master
# Changes not staged for commit:
#   (use "git add ..." to update what will be committed)
#   (use "git checkout -- ..." to discard changes in working directory)
#
#	modified:   a.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

(11)使用git checkout撤销对a.txt的修改

# git checkout -- a.txt
# cat a.txt 
hello git

总结:

序号 命令 说明
1 git --version 查看git的版本信息
2 git init 初始化当前目录,在当前目录创建.git文件夹
3 git add 将文件添加到git的index中
4 git commit -m “comment words” 提交,-m指定提交的注释
5 git chechout – 撤销对file的修改,注意,撤销的操作被丢弃了,无法找回
6 git status 查看当前目录下git 的状态,git会对当前工程的版本信息进行描述
7 git log 查看git的日志,日志内记录了用户名,email、git版本号等内容
8 git log --oneline 查看git日志,单行模式

3.git config

(1)查看git config的帮助文档

# git config --help
GIT-CONFIG(1)    Git Manual            GIT-CONFIG(1)
NAME
       git-config - Get and set repository or global options
SYNOPSIS
       git config [<file-option>] [type] [-z|--null] name [value [value_regex]]
       git config [<file-option>] [type] --add name value
       git config [<file-option>] [type] --replace-all name value [value_regex]
       git config [<file-option>] [type] [-z|--null] --get name [value_regex]
       git config [<file-option>] [type] [-z|--null] --get-all name [value_regex]
       git config [<file-option>] [type] [-z|--null] --get-regexp name_regex [value_regex]
       git config [<file-option>] --unset name [value_regex]
       git config [<file-option>] --unset-all name [value_regex]
       git config [<file-option>] --rename-section old_name new_name
       git config [<file-option>] --remove-section name
       git config [<file-option>] [-z|--null] -l | --list
       git config [<file-option>] --get-color name [default]
       git config [<file-option>] --get-colorbool name [stdout-is-tty]
       git config [<file-option>] -e | --edit

git config是git配置,git的配置分为三个等级,分别对应3个config文件

  • local:./.git/config,local级别的config存放在当前工程目录下
  • global:~/.gitconfig,global级别的config存放在用户目录下
  • system:/etc/gitconfig。system级别的config存放在/etc目录下

(2)查看local界别的config

# git config --local --list
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true

(3)查看global级别的config

# git config --global --list
user.name=jim19Green
user.email=[email protected]
color.ui=auto

(4)查看system级别的config

# git config --system --list
fatal: unable to read config file '/etc/gitconfig': No such file or directory

(5)在工程目录下初始化,添加文件,添加文件到index,提交文件,查看log

# mkdir lession02
# cd lession02
# git init 
Initialized empty Git repository in /root/Desktop/git/lession02/.git/
# echo "lession02 learn git" >> a.txt
# git add a.txt 
# git commit -m "first commit"
[master (root-commit) 6982243] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 a.txt
# git log 
commit 6982243766ed3c6e11a104d203df566a9fe33820
Author: jim19Green <[email protected]>
Date:   Tue Aug 4 09:26:34 2020 +0800
    first commit

(6)在local级别设置user.name和user.email

# git config --local user.name jim
# git config --local user.email [email protected]
# git config --local --list
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
user.name=jim
user.email=[email protected]
# git config --list
user.name=jim19Green
user.email=[email protected]
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
user.name=jim
user.email=[email protected]

(7)修改a.txt文件,git add,git commit,查看git log

# echo "111111" >> a.txt
# git status
# On branch master
# Changes not staged for commit:
#   (use "git add ..." to update what will be committed)
#   (use "git checkout -- ..." to discard changes in working directory)
#
#	modified:   a.txt
no changes added to commit (use "git add" and/or "git commit -a")
# git add a.txt
# git commit -m "second commit"
[master 79d8d1e] second commit
 1 file changed, 1 insertion(+)
 # git log
commit 79d8d1ea90d4545cedde122e2e55b64760d1fef7
Author: jim <[email protected]>
Date:   Tue Aug 4 09:32:22 2020 +0800

    second commit

commit 6982243766ed3c6e11a104d203df566a9fe33820
Author: jim19Green <[email protected]>
Date:   Tue Aug 4 09:26:34 2020 +0800

    first commit

(8)删除local级别的user.name和user.email配置

# git config --local --unset user.name
# cat .git/config 
[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
[user]
	email = [email protected]

(9)删除local级别的user相关的配置

# git config --local --remove-section user
# cat .git/config 
[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true

(10)启用有帮助的彩色命令行输出

# git config --global color.ui auto

总结:

  1. git config的配置分为3个级别,local、global、system,分别对应3个配置文件
  2. 属性名称完全一致时,local级别的属性会覆盖global级别的属性,global级别的属性会覆盖system级别的属性
序号 命令 说明
1 git config --help 查看git config的帮助文档
2 git config --local --list 查看local级别的配置
3 git config --global --list 查看global级别的配置
4 git config --system --list 查看system级别的配置
5 git config --list 查看所有配置
6 git config --local user.name 在local级别下配置user.name
7 git config --local --unset user.name 删除local级别下的user.name配置

你可能感兴趣的:(Git)