【Git】版本控制器详解之git的概念和基本使用

版本控制器git

初始Git

为了能够更⽅便我们管理不同版本的⽂件,便有了版本控制器 。所谓的版本控制器,就是⼀个可以记录⼯程的每⼀次改动和版本迭代的⼀个管理系统,同时也⽅便多⼈协同作业。

目前最主流的版本控制器就是Git。Git可以控制电脑上所有格式的文件,例如doc、excel、dwg、dgn、rvt等等。对于开发⼈员来说,Git最重要的就是可以帮助我们管理软件开发项⽬中的源代码文件

git的安装

Centos7.9下安装git

bash 复制代码
查看是否安装git
git --version 
//没有则使用yum安装
sudo yum -y install git

git的基本使用

初始化本地仓库

bash 复制代码
git init

执行完毕后会生成一个隐藏的.git文件,内含有配置信息

配置本地仓库

当安装Git后⾸先要做的事情是设置你的用户名、e-mail地址

bash 复制代码
#设置配置
[wmh@pDaD gitcode]$ git config user.name "wmh"
[wmh@pDaD gitcode]$ git config user.email "66666@qq.com"
#查看配置
[wmh@pDaD gitcode]$ git config -l
user.email=66666@qq.com
user.name=wmh
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true

#删除配置
[wmh@pDaD gitcode]$ git config --unset user.name
[wmh@pDaD gitcode]$ git config --unset user.email
[wmh@pDaD gitcode]$ git config -l
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true

配置全局生效

全局的配置必须使用全局的删除

bash 复制代码
[wmh@pDaD gitcode]$ git config --global user.name "wmh"
[wmh@pDaD gitcode]$ git config --global user.email "66666@qq.com"

[wmh@pDaD gitcode]$ git config --global --unset user.name
[wmh@pDaD gitcode]$ git config --global --unset user.email

三区协作


.git :版本库,虽然在工作区,但是不属于工作区
code :工作区;用来写代码的文件或者是目录

刚创建的myfile是不会被git管理起来的,需要先add到暂存区,然后commit到版本库的master分支下,暂存区和master下存储的都是git对象

添加---add

bash 复制代码
git add .  #把当前目录所有文件添加到暂存区
git commit -m "注释" #把暂存区内容交付给版本库
git log #显示日志
bash 复制代码
[wmh@pDaD code]$ git add .
[wmh@pDaD code]$ git commit -m "first file"
[master (root-commit) bd3f11d] first file
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 myfile
[wmh@pDaD code]$ touch myfile2
[wmh@pDaD code]$ git add myfile2
[wmh@pDaD code]$ git commit -m "add new file"
[master 0857536] add new file
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 myfile2
[wmh@pDaD code]$ git log
commit 0857536013e6b5c890d2fa0ba454041e30bfab15
Date:   Wed Aug 9 17:16:36 2023 +0800

    add new file

commit bd3f11df093e0e559697f0a2989a32843c30783b
Date:   Wed Aug 9 17:15:41 2023 +0800

    first file
[wmh@pDaD code]$ git log --pretty=online
fatal: invalid --pretty format: online
[wmh@pDaD code]$ git log --pretty=oneline
0857536013e6b5c890d2fa0ba454041e30bfab15 add new file
bd3f11df093e0e559697f0a2989a32843c30783b first file

观察.git文件结构变化

在.git文件里,可以看到出现了index(暂存区),前面说道HEAD指向master,通过cat .git/HEAD查到了master;接着查看master内容结果是一串哈希摘要,并且与object第一个字符串相同,其实就是git对象,通过深度查阅哈希摘要发现有tree、parent等字段,parent对应的是上一个commit对象,再细细追究,就是修改的文件内容

修改文件--status|diff

git status 命令⽤于查看在你上次提交之后是否有对⽂件进⾏再次修改
git diff [file] 命令⽤来显示暂存区和⼯作区文件的差异

也可以使⽤ git diff HEAD -- [file] 命令来查看版本库和⼯作区⽂件的区别

当使用git add之后,再次查看,发现少了一行,完成完整的操作流程,最后就会显示没用什么可以commit的了

版本回退--reset

执行 git reset 命令⽤于回退版本,可以指定退回某⼀次提交的版本。

要解释⼀下"回退"本质是要将版本库中的内容进行回退,⼯作区或暂存区是否回退由命令参数决定:

git reset 命令语法格式为: git reset [--soft | --mixed | --hard] [HEAD]

• --mixed 为默认选项,使⽤时可以不⽤带该参数。该参数将暂存区的内容退回为指定提交版本内

容,⼯作区⽂件保持不变。

• --soft 参数对于⼯作区和暂存区的内容都不变,只是将版本库回退到某个指定版本。

• --hard 参数将暂存区与⼯作区都退回到指定版本。切记⼯作区有未提交的代码时不要用这个命

令,因为⼯作区会回滚,你没有提交的代码就再也找不回了,所以使用该参数前⼀定要慎重。

• HEAD 可直接写成commit id,表示指定退回的版本

演示--hard

git log查看提交日志,找到以前的commit id

使用git reset进行回退到之前的版本,如果记得回退之前的commit id,可以再次回退

使用git reflog用于查看本地提交记录,简短的commit id也可以进行版本回退

撤销修改

情况⼀:对于工作区的代码,还没有add

bash 复制代码
git checkout -- myfile
bash 复制代码
[wmh@pDaD code]$ cat myfile
first line
second line
third line
[wmh@pDaD code]$ git checkout -- myfile
[wmh@pDaD code]$ cat myfile
first line
second line

情况⼆:已经 add ,但没有 commit

方案一:使用git reset -- mixed+git checkout

方案二:使用git reset -- hard

bash 复制代码
[wmh@pDaD code]$ cat myfile
first line
second line
shhhshsh

[wmh@pDaD code]$ git add myfile
[wmh@pDaD code]$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	modified:   myfile
#
[wmh@pDaD code]$ git reset --mixed HEAD
Unstaged changes after reset:
M	myfile
[wmh@pDaD code]$ 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:   myfile
#
no changes added to commit (use "git add" and/or "git commit -a")

[wmh@pDaD code]$ git checkout -- myfile
[wmh@pDaD code]$ git status
# On branch master
nothing to commit, working directory clean

情况三:已经 add ,并且也 commit 了,但还未push

直接写HEAD是当前版本

HEAD^-----上一版本

HEAD^^----上上一版本

bash 复制代码
#回退到上一个版本
git reset --hard HEAD^

删除文件

已经 add ,并且也 commit 了的文件,要删⽂件,除了要删工作区的⽂件,还要清除版本库的⽂件。

第一步:使用 git rm file

第二步:git commit

bash 复制代码
[wmh@pDaD code]$ git rm myfile3
rm 'myfile3'
[wmh@pDaD code]$ ll
total 4
-rw-rw-r-- 1 wmh wmh 23 Aug 10 17:04 myfile
-rw-rw-r-- 1 wmh wmh  0 Aug 10 16:18 myfile2
[wmh@pDaD code]$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	deleted:    myfile3
#
[wmh@pDaD code]$ git commit -m "delete file3"
[master dda18c2] delete file3
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 myfile3
[wmh@pDaD code]$ git status
# On branch master
nothing to commit, working directory clean
相关推荐
给阿姨倒1杯卡布奇诺3 小时前
Git在多人开发中的常见用例
git
小超嵌入式笔记4 小时前
[教程]Gitee保姆级图文使用教程
git·gitee
突然暴富的我8 小时前
Git 的基本概念和使用方式。
前端·git·json·github
福星高照~11 小时前
Git、Github、tortoiseGit下载安装调试全套教程
git·github
C、空白格17 小时前
IDEA 2018提交Git之后撤销commit
java·git·intellij-idea
老衲有点帅18 小时前
Git 常用命令及其作用
git·github
肆·意20 小时前
Git安装以及环境配置(详细)
git
小远披荆斩棘20 小时前
Github与本地仓库建立链接、Git命令(或使用Github桌面应用)
git·github
Dongguabai21 小时前
使用 Git Hooks 防止敏感信息泄露
git
sun00770021 小时前
git 添加本地分支, clean
git