Git学习之一 Git 库的初始化和Git中文件的状态.

Git功能优势

Git是一个版本控制系统。对代码进行日常更新修改等操作极为友好,使用Git不但可以让我们时刻将本地代码上传到服务器上(github),以保证代码不会丢失,还可以方便多人同时协作,对不同人开发的代码进行融合。
Git的绝大多数操作都是在本地完成的。

Git初始化操作

进入一个新建文件夹,比如我们新建一个gitTest文件夹,使用以下命令:

mkdir gitTest && cd gitTest 
操作示例:
➜  MyGit mkdir gitTest && cd gitTest
➜  gitTest 

这样我们就创建并进入了gitTest文件夹,我们想将gitTest文件夹内的内容纳入到版本库,要对文件夹进行初始化

git init
操作示例:
➜  gitTest git init
Initialized empty Git repository in /Users/username/Documents/MyGit/gitTest/.git/

这样,这个文件夹就成了一个git仓库,文件夹内会有git进行版本控制的必要文件,存放在 .git文件夹内。以“.”开头的文件夹在Linux系统中是隐藏的,我们可以通过命令查看它们:

ls -al;

操作示例:
➜  gitTest git:(master) ls -al
total 0
drwxr-xr-x   3 username  staff   96  1 21 09:32 .
drwxr-xr-x   3 username  staff   96  1 21 09:31 ..
drwxr-xr-x  10 username  staff  320  1 21 09:32 .git

可以看到,在gitTest文件夹下有一个.git文件夹,表明这是一个git仓库。
在我的操作示例中有一个"gitTest git:(master)"这个标记,因为我用的是zsh,对git操作很友好,如果进入一个git仓库,会显示当前操作分支(git:(master)表明当前是在master分支)。

git的文件配置

进入版本库后,我们要对版本库进行配置,比如配置用户名和用户email。
首先我们查看git当前配置用git config命令。

配置分为local和global和system的配置:

local配置:

查看local配置:

➜  gitTest git:(master) git config --local --list

core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
core.ignorecase=true
core.precomposeunicode=true
(END)

我们对local进行设置:

➜  gitTest git:(master) git config --local user.name zhangsan
➜  gitTest git:(master) git config --local user.email 'zhangsan@git.com'
➜  gitTest git:(master) git config --local --list

core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
core.ignorecase=true
core.precomposeunicode=true
user.name=zhangsan		 		#用户名改为张三
user.email=zhangsan@git.com		#用户邮箱进行修改
~

如果要删除用户名怎么做呢?

git config --local --unset user.name

➜  gitTest git:(master) git config --local --unset user.name
➜  gitTest git:(master) git config --local --list

core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
core.ignorecase=true
core.precomposeunicode=true
user.email=zhangsan@git.com
(END)
#用户名已经消失了

local:仅对当前文件夹下的版本库生效,比如再新建一个gitTest2文件的配置就不会生效

➜  gitTest git:(master) cd ../      			#回到上一级文件下
➜  MyGit ls
gitTest											#查看文件内容
➜  MyGit mkdir gitTest2 && cd gitTest2 			#创建并进入gitTest2
➜  gitTest2 git init							#在gitTest文件夹下初始化内容
Initialized empty Git repository in /Users/yibin.yang/Documents/MyGit/gitTest2/.git/	
➜  gitTest2 git:(master) git config --local --list    #查看当前git配置

core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
core.ignorecase=true
core.precomposeunicode=true
(END)

可以看到我们在gitTest1下配置了email但在gitTest2下并没有内容。所以local配置仅对当前文件夹生效。

global 配置

命令:

git config --global --list  					#查看配置
git config --global user.name 名称  				#设置用户名
git config --global user.email <mail>			#设置邮箱
git config --global --unset user.name			#删除用户名
git config --global --unset user.email			#删除邮箱

global 的配置对电脑当前用户生效,即该用户所建的所有版本库均可以使用global配置,为了方便,配置global就可以了。

system 配置

命令:

git config --system --list  					#查看配置
git config --system user.name 名称  				#设置用户名
git config --system user.email <mail>			#设置邮箱
git config --system --unset user.name
git config --system --unset user.email

global 的配置对电脑所有用户生效。

优先级

如果在 local global system下均配置了用户名和密码怎么办呢?

优先级是 local > global > system

所以三个都配置时,使用local的配置。

git版本库中文件的三种状态

在git版本控制系统中,存在三个分区:

1、工作区

我们平时写代码,改代码都是在这个区进行的。当我们新建一个文件时,比如hello.txt,这个时候我们就是在工作区创建了一个文件。

2、暂存区

暂存区是一个过渡区,从工作区向提交区的过渡,只有暂存区的内容才能提交到提交区
通常用命令

git add filename	#将文件filename纳入暂存区
git add .			#将当前文件夹所有文件递归地纳入暂存区(子文件夹下的文件也会被纳入)

3、版本区

当我们想把完成的代码存储起来,就可以将暂存区的内容提交到版本区,这时候文件就进入到正式的版本控制中了。纳入版本区以后,文件以后作的任意修改,总能恢复到现在的状况。
提交命令

git commit -m 'message'

示例演示

首先创建一个文件并查看文件内容:

➜  gitTest git:(master) touch test.txt		#创建文件
➜  gitTest git:(master)ls 				#查看当前目录 已经创建了test.txt
test.txt
➜  gitTest git:(master)cat test.txt 		#查看test.txt文件内容为空
git status

那我们接下来介绍一个查看当前git状态的命令 git status

➜  gitTest git:(master)git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	test.txt

nothing added to commit but untracked files present (use "git add" to track)

在敲出git status后,我们看到出现提示信息
第一句:在主分支上(分支是git的一个特点,我们一般可以在开发分支开发,开发debug完成后提交到test分支进行测试,测试成功后提交到主分支进行生产发布),在git初始化时,git自动创建并进入主分支。
第二句:尚未有任何提交(表明提交区为空)
第三句:尚未追踪 test.txt文件(git会自动检查与提交区不同的修改,当前提交区是空的,所以当我们创建文件时,是在工作区创建的,会比提交区多出来一个文件,而这个文件未纳入版本控制库)
提示我们用 git add纳入暂存区
第四句表明没有任何内容被添加到暂存区等待提交,但是出现了一个未追踪的文件。

git add

现在我们进行 git add 操作

➜  gitTest git:(master)git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
	new file:   test.txt

我们看第三句:
改变等待提交,新文件:test.txt 表明test已经纳入暂存区。
还提示我们可以用git -rm --cached <文件名>取消文件暂存。这个我们等会再讲。

git commit

刚刚我们将test.txt 纳入到暂存区,现在我们想从暂存区提交到版本区

输入 git commit

git commit

会进入一个文本编辑器有以下内容


# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch master
#
# Initial commit
#
# Changes to be committed:
#	new file:   test.txt
#

这是让我们输入提交信息,以便以后查看
我们可以直接用就不必调出来文本编辑器,记住git进行提交时一定要输入提交信息的,不然无法提交。

 ➜  gitTest git:(master)git commit -m 'init commit'
[master (root-commit) 6276b5f] init commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.txt

➜  gitTest git:(master) git status 
On branch master
nothing to commit, working tree clean

提交后有提示信息,在敲出git status后,提示我们当前在主分支上,没有文件等待提交,工作区干净的,这表明工作区的内容和版本区内容一致。

暂存区文件的删除

如果我们要从暂存区删除文件怎么做呢,有两种做法:

git rm --cached <文件名>
git reset head <文件名>

演示以下

➜  gitTest git:(master) echo hello >> test.txt
➜  gitTest git:(master)git status 
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   test.txt

no changes added to commit (use "git add" and/or "git commit -a")

#修改test文件

➜  gitTest git:(master)git add .
➜  gitTest git:(master)git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   test.txt

#纳入暂存区

➜  gitTest git:(master)git reset head test.txt
Unstaged changes after reset:
M	test.txt
➜  gitTest git:(master)git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   test.txt

no changes added to commit (use "git add" and/or "git commit -a")

#可以看到暂存区已经没有了,工作区有修改
#下面用git rm --cached 
➜  gitTest git:(master)git add .
➜  gitTest git:(master)git rm --cached test.txt
rm 'test.txt'
➜  gitTest git:(master)git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	deleted:    test.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	test.txt

可以看到这个时候git暂存区删除了test.txt文件,如果提交(git commit 命令)那么提交区也会删除这个文件,只有工作区会有这个文件。
而 git reset head 命令仅仅是取消暂存区的存储,使用这个命令以后暂存区就为空了。

比较git rm --cached 和 git reset head

1、git rm --cached 删除暂存区内容,相当于做了删除修改后提交,如果进行提交,提交区也会删除文件,而且工作区内容不变
2、git reset head 将暂存区转移到工作区,是git add的反操作。如果进行提交,提交区不做任何修改。
3、git reset head 后可以用git checkout 文件名,丢弃掉工作区所做的修改,使之保持与提交区内容相同。

总结

1、git仓库初始化 git int
2、git仓库配置 git config --logcal|global|system …
3、git三个分区的关系和文件在三个分区转移方法。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值