01_Git初始化

Git版本控制

安装

参考其他教程

配置变量

在开始Git之路之前,需要配置Git的配置变量-> user.name和user.email,这是一次性工作。

这些设置会记录在全局文件(Linux下~/.gitconfig) or 系统文件(/etc/gitconfig) 中永久记录。

告诉Git当前用户姓名和邮件地址,用于标识推送身份,在推送时候会用到。

git config --global user.name "your_name"
git config --global user.email "your_email@doman.com"
//比如我的,不要照抄,更改你的信息
//git config --global user.name "mrliu"
//git congif --global user.email "652455342@qq.com"

作用域(之后说)

  • global
  • local
  • system
git config --local //当前某个仓库
git config --gobal //本用户全局所有仓库
git config --system //对系统所有登陆用户
git config --list --local 
git config --list --global
git config --list --system

设置别名

通过sudo获取管理员权限,希望注册的命令别名能够别所有用户使用。此情况是在linux环境下,如果是win则不需要sudo。

sudo git config --system alias.st status
sudo git config --system alias.ci "commit -s"
sudo git config --system alias.co checkout
sudo git config --system alias.br branch

也可以使用下边命令,只在本用户的全局配置中添加Git别名。

sudo git config --global alias.st status
sudo git config --global alias.ci "commit -s"
sudo git config --global alias.co checkout
sudo git config --global alias.br branch

开启颜色显示

git config --global color.ui true

建Git仓库

cd project_name	//现有的项目文件夹
git init		//初始化
git init new_project_name	//初始化新的Git仓库

工作目录会产生.git的文件夹,但是在工作区隐藏了此文件夹。

[root@iZ0jlekc5tccfxx5b4j7hnZ git]# mkdir firstProjectAboutGit
[root@iZ0jlekc5tccfxx5b4j7hnZ git]# cd firstProjectAboutGit/
[root@iZ0jlekc5tccfxx5b4j7hnZ firstProjectAboutGit]# git init
Initialized empty Git repository in /root/git/firstProjectAboutGit/.git/
[root@iZ0jlekc5tccfxx5b4j7hnZ firstProjectAboutGit]# ll -a
total 12
drwxr-xr-x 3 root root 4096 Feb  4 21:20 .
drwxr-xr-x 3 root root 4096 Feb  4 21:20 ..
drwxr-xr-x 7 root root 4096 Feb  4 21:20 .git

第一次提交到本地暂存区(不理解后边有详细解析)

在工作区加一些文件,执行一下命令,创建一个welcome.txt 文件

echo "Hello World!" > welcome.txt
[root@iZ0jlekc5tccfxx5b4j7hnZ firstProjectAboutGit]# echo "Hello World" > welcome.txt
[root@iZ0jlekc5tccfxx5b4j7hnZ firstProjectAboutGit]# ll
total 4
-rw-r--r-- 1 root root 12 Feb  4 21:23 welcome.txt

将这个文件新加到版本库

git add welcome.txt
git commit -m "init file and write \"hello world to one txt\" ";
//等同于 git ci "init file and write \"hello world to one txt\" ";
[root@iZ0jlekc5tccfxx5b4j7hnZ firstProjectAboutGit]# git commit -m "init file and write \"hello world to one txt\" ";
[master (root-commit) 2f0d879] init file and write "hello world to one txt"
 1 file changed, 1 insertion(+)
 create mode 100644 welcome.txt

通过上面命名可以看出:

  • ci 等同于commit,设置的别名。
  • “-m” 参数设置提交说明:"init file and write “hello world to one txt” ",在git中-m 提交说明是强制性的,不像其他版本控制系统可以接受空白提交说明。当提交不提供提交说明时,Git会自动打开一个编辑器,要求您输入提交说明,保存退出,才能提交。需要使用vim和emacs的编辑技巧,否则保存退出也会成为问题。
  • 命令输出第一行,提交在名为 maste 分支上,第一个提交,提交ID:“2f0d879”。
  • 命令输出第二行,此次提交修改了一个文件,包含一行插入。
  • 命令输出第三行,创建了一个文件 welcome.txt

Other

01 为什么工作区目录下有一个.git目录

对应CVS而言,工作区的根目录以及每一个子目录下都有一个CVS目录,CVS目录中包含几个配置文件,建立了对版本控制库的追踪。比如CVS目录下的Entries文件记录了从版本库检出到工作区的文件名称,版本和时间戳等。

这样设计可以将工作区移动到任何目录,依然能正常工作。

缺点也很多,例如,工作区文件修改,因为原始文件没有文件对比,所有向服务器提交修改时只能对整个文件进行传输,而不是仅仅传输改动文件,降低效率。

此外还有信息泄露,例如Web服务器的目录下包含CVS文件,通过扫描CVS/Entries文件可以得到目录下单文件信息,从而获得他们想要的信息。

Git版本库放在工作区根目录,使得所有版本控制操作都在本地完成,不可以脱离网络执行。而且不存在安全泄露问题(只要保护好.git目录)。甚至Git还提供一条指令 git grep 来更好的搜索工作区文件内容。

带来一个疑惑,.git在工作区根目录,哪在子目录下执行的时候,如何定位版本库呢?

实际上,当Git在某个子目录下执行操作的时候,会在工作区目录中依次向上递归查找.git目录,找到.git就是工作区对应的版本库。

在(退回到上级目录)非工作区执行git命令会因为找不到.git目录而报错。

[root@iZ0jlekc5tccfxx5b4j7hnZ firstProjectAboutGit]# cd ..
[root@iZ0jlekc5tccfxx5b4j7hnZ git]# git status
fatal: Not a git repository (or any of the parent directories): .git

可以使用strace 命令追踪执行git status 命令访问磁盘,会依次看到向上递归的过程。

[root@iZ0jlekc5tccfxx5b4j7hnZ git]# [root@iZ0jlekc5tccfxx5b4j7hnZ git]# strace -e "trace=file" git status
execve("/usr/bin/git", ["git", "status"], 0x7ffd54afa528 /* 26 vars */) = 0
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
open("/lib64/libpcre.so.1", O_RDONLY|O_CLOEXEC) = 3
open("/lib64/libz.so.1", O_RDONLY|O_CLOEXEC) = 3
open("/lib64/libpthread.so.0", O_RDONLY|O_CLOEXEC) = 3
open("/lib64/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
access("/etc/sysconfig/strcasecmp-nonascii", F_OK) = -1 ENOENT (No such file or directory)
access("/etc/sysconfig/strcasecmp-nonascii", F_OK) = -1 ENOENT (No such file or directory)
open("/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3
getcwd("/root/git", 4096)               = 10
stat(".", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
stat(".git", 0x7ffca6023700)            = -1 ENOENT (No such file or directory)
access(".git/objects", X_OK)            = -1 ENOENT (No such file or directory)
access("./objects", X_OK)               = -1 ENOENT (No such file or directory)
stat("..", {st_mode=S_IFDIR|0550, st_size=4096, ...}) = 0
chdir("..")                             = 0
stat(".git", 0x7ffca6023700)            = -1 ENOENT (No such file or directory)
access(".git/objects", X_OK)            = -1 ENOENT (No such file or directory)
access("./objects", X_OK)               = -1 ENOENT (No such file or directory)
stat("..", {st_mode=S_IFDIR|0555, st_size=4096, ...}) = 0
chdir("..")                             = 0
stat(".git", 0x7ffca6023700)            = -1 ENOENT (No such file or directory)
access(".git/objects", X_OK)            = -1 ENOENT (No such file or directory)
access("./objects", X_OK)               = -1 ENOENT (No such file or directory)
fatal: Not a git repository (or any of the parent directories): .git
+++ exited with 128 +++

execve("/usr/bin/git", ["git", "status"], 0x7ffd7da851e8 /* 26 vars */) = 0
brk(NULL)                               = 0x1120000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f30d9e6c000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=43396, ...}) = 0
mmap(NULL, 43396, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f30d9e61000
close(3)                                = 0
open("/lib64/libpcre.so.1", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\360\25\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=402384, ...}) = 0
mmap(NULL, 2494984, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f30d99ea000
mprotect(0x7f30d9a4a000, 2097152, PROT_NONE) = 0
mmap(0x7f30d9c4a000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x60000) = 0x7f30d9c4a000
close(3)                                = 0
open("/lib64/libz.so.1", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0P!\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=90160, ...}) = 0
mmap(NULL, 2183272, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f30d97d4000
mprotect(0x7f30d97e9000, 2093056, PROT_NONE) = 0
mmap(0x7f30d99e8000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x14000) = 0x7f30d99e8000
close(3)                                = 0
open("/lib64/libpthread.so.0", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\200m\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=142144, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f30d9e60000
mmap(NULL, 2208904, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f30d95b8000
mprotect(0x7f30d95cf000, 2093056, PROT_NONE) = 0
mmap(0x7f30d97ce000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x16000) = 0x7f30d97ce000
mmap(0x7f30d97d0000, 13448, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f30d97d0000
close(3)                                = 0
open("/lib64/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0`&\2\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=2156592, ...}) = 0
mmap(NULL, 3985920, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f30d91ea000
mprotect(0x7f30d93ae000, 2093056, PROT_NONE) = 0
mmap(0x7f30d95ad000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1c3000) = 0x7f30d95ad000
mmap(0x7f30d95b3000, 16896, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f30d95b3000
close(3)                                = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f30d9e5f000
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f30d9e5d000
arch_prctl(ARCH_SET_FS, 0x7f30d9e5d740) = 0
access("/etc/sysconfig/strcasecmp-nonascii", F_OK) = -1 ENOENT (No such file or directory)
access("/etc/sysconfig/strcasecmp-nonascii", F_OK) = -1 ENOENT (No such file or directory)
mprotect(0x7f30d95ad000, 16384, PROT_READ) = 0
mprotect(0x7f30d97ce000, 4096, PROT_READ) = 0
mprotect(0x7f30d99e8000, 4096, PROT_READ) = 0
mprotect(0x7f30d9c4a000, 4096, PROT_READ) = 0
mprotect(0x766000, 4096, PROT_READ)     = 0
mprotect(0x7f30d9e6d000, 4096, PROT_READ) = 0
munmap(0x7f30d9e61000, 43396)           = 0
set_tid_address(0x7f30d9e5da10)         = 29784
set_robust_list(0x7f30d9e5da20, 24)     = 0
rt_sigaction(SIGRTMIN, {sa_handler=0x7f30d95be860, sa_mask=[], sa_flags=SA_RESTORER|SA_SIGINFO, sa_restorer=0x7f30d95c7630}, NULL, 8) = 0
rt_sigaction(SIGRT_1, {sa_handler=0x7f30d95be8f0, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART|SA_SIGINFO, sa_restorer=0x7f30d95c7630}, NULL, 8) = 0
rt_sigprocmask(SIG_UNBLOCK, [RTMIN RT_1], NULL, 8) = 0
getrlimit(RLIMIT_STACK, {rlim_cur=8192*1024, rlim_max=RLIM64_INFINITY}) = 0
brk(NULL)                               = 0x1120000
brk(0x1141000)                          = 0x1141000
brk(NULL)                               = 0x1141000
open("/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=106176928, ...}) = 0
mmap(NULL, 106176928, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f30d2ca7000
close(3)                                = 0
getcwd("/root/git", 4096)               = 10
stat(".", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
stat(".git", 0x7ffe504811f0)            = -1 ENOENT (No such file or directory)
access(".git/objects", X_OK)            = -1 ENOENT (No such file or directory)
access("./objects", X_OK)               = -1 ENOENT (No such file or directory)
stat("..", {st_mode=S_IFDIR|0550, st_size=4096, ...}) = 0
chdir("..")                             = 0
stat(".git", 0x7ffe504811f0)            = -1 ENOENT (No such file or directory)
access(".git/objects", X_OK)            = -1 ENOENT (No such file or directory)
access("./objects", X_OK)               = -1 ENOENT (No such file or directory)
stat("..", {st_mode=S_IFDIR|0555, st_size=4096, ...}) = 0
chdir("..")                             = 0
stat(".git", 0x7ffe504811f0)            = -1 ENOENT (No such file or directory)
access(".git/objects", X_OK)            = -1 ENOENT (No such file or directory)
access("./objects", X_OK)               = -1 ENOENT (No such file or directory)
write(2, "fatal: Not a git repository (or "..., 69fatal: Not a git repository (or any of the parent directories): .git
) = 69
exit_group(128)                         = ?
+++ exited with 128 +++
//在工作区子目录下,查找.git目录
git rev-parse --git-dir
//工作区根目录
git rev-parse --show-toplevel
//相对于工作区根目录的相对目录
git rev-parse --show-prefix
[root@iZ0jlekc5tccfxx5b4j7hnZ firstProjectAboutGit]# mkdir -p a/b/c
[root@iZ0jlekc5tccfxx5b4j7hnZ firstProjectAboutGit]# cd a/b/c/
[root@iZ0jlekc5tccfxx5b4j7hnZ c]# git rev-parse --git-dir
/root/git/firstProjectAboutGit/.git
[root@iZ0jlekc5tccfxx5b4j7hnZ c]# git rev-parse --show-toplevel
/root/git/firstProjectAboutGit
[root@iZ0jlekc5tccfxx5b4j7hnZ firstProjectAboutGit]# mkdir -p a/b/c
[root@iZ0jlekc5tccfxx5b4j7hnZ firstProjectAboutGit]# cd a/b/c/
[root@iZ0jlekc5tccfxx5b4j7hnZ c]# git rev-parse --git-dir
/root/git/firstProjectAboutGit/.git
[root@iZ0jlekc5tccfxx5b4j7hnZ c]# git rev-parse --show-toplevel
/root/git/firstProjectAboutGit
02 git config命令的各参数有何区别

首先配置文件有三个级别,分别是:版本控制库级别配置文件,全局配置文件(用户主目录)个系统级配置文件(/etc目录下)。

优先级分别是 版本控制库 > 全局配置文件 > 系统配置文件。

参数分别是:

–global
–system
–local
03 随意设置提交者姓名,是否不太安全

Git可以随意设置用户名和邮箱地址,在个人使用的时候,没必要在添加一次身份认证,而在团队合作的时候,就需要进行用户身份认证并检查授权。

05 命令别名是干什么的

简化命令行支持。但个人学习的时候,推荐使用全称,在以后熟悉后可别名简化。

未完待续…

  • 17
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值