git的基本使用

2022/8/1 23:24:12

本文主要是介绍git的基本使用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Git介绍

git拒绝接管空目录

它是一个开源的分布式版本控制系统,可以有效、高速地处理从很小到非常大的项目版本管理,这里涉及到了一个概念:版本控制。
那么什么是版本控制?为什么需要版本控制呢?
当你在修改一个文件时,突然发现修改的并不符合自己的要求,这时你想退回最开始的状态就只能通过手动还原,可能你还不记得原来的文件是什么样子的。此时你可能会想到,将源文件做一个备份,修改一部分就做一次备份,就和VMware的快照一样。如果修改错误,就将源文件的类容cp出来。可一旦文件特别多,特别大的时候是不是就特别难管理。所以出现了版本控制系统。让程序帮你管理源文件。

git的功能:

  • 协同修改
  • 数据备份
  • 版本管理
  • 权限控制
  • 历史记录
  • 分支管理

git的工作原理

在这里插入图片描述
Workspace:工作区 -> 修改代码的地方
Index / Stage:暂存区 -> 一段缓存,存放修改的内容
Repository:仓库区(或本地仓库)-> 可以理解为备份
Remote:远程仓库 -> 可以理解为将本地仓库上传到云端

整个流程大概是 pull(拉取代码)>修改以后add添加到缓存区>再commit提交到本地仓库>最后将本地仓库push到远程仓库
这里有一个讲的比较好的例子

安装

linux上面安装git非常简单,直接用yum安装就好了

yum install git-all -y
git --version  # 查看git版本

配置信息

# 因为git是分布式版本控制系统,所以需要通过用户名来作区分
# --global是所有版本库都可以使用这个配置
# 设置用户名和邮箱
git config --global user.name "自定义的用户名"
git config --global user.email "自定义邮箱"
# 删除用户名和邮箱
git config --global --unset user.name "想要取消的用户名"
git config --global --unset user.email "想要取消的邮箱"
# 查看用户名和邮箱
git config user.name
git config user.email

创建版本库

# 自定义一个文件夹
[root@fyh ~]# mkdir -p /fyh/gits
[root@fyh ~]# cd /fyh/gits
[root@fyh ~]# pwd
/fyh/gits
# 通过git init将/fyh/gits目录变成通过git管理的仓库
[root@fyh gits]# git init
Initialized empty Git repository in /fyh/gits/.git/
# 此时我们ls -a查看一下
[root@fyh gits]# ls -a
.  ..  .git # git是一个隐藏文件里面存放着git命令相关的配置文件
[root@fyh gits]# ls .git/
branches  config  description  HEAD  hooks  info  objects  refs
# 最熟悉的就是config文件,肯定是配置文件,我们可以打开看一下
[root@fyh gits]# vim .git/config
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
# 发现啥也没有
# 创建一个用户,这次就不需要--global,这个用户我只想在这个版本库中使用
[root@fyh gits]# git config user.name "fyh"
[root@fyh gits]# git config user.email "1334056960@qq.com"
# 再打开配置文件看看
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[user]
        name = fyh
        email = 1334056960@qq.com
# 发现多了一个user正好是我们创建的用户

提交文件

# 创建一个测试文件
[root@fyh gits]# vim test.txt
this is test
# 将test.txt文件添加到暂存区
[root@fyh gits]# git add test.txt
# 将暂存区的文件提交到本地仓库
[root@fyh gits]# git commit -m "add test.txt"  # add test.txt 是一个注释,用于提示本次提交的内容.
[master (root-commit) e76ceeb] add test.txt
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt

版本回退

# 我们上面只提交了一次,所以只有一个版本,我们需要多提交几次
[root@fyh gits]# vim test.txt
this is test
this is 2
[root@fyh gits]# git add test.txt
[root@fyh gits]# git commit -m "add this is 2"
[root@fyh gits]# vim test.txt
this is test
this is 2
this is 3
[root@fyh gits]# git add test.txt
[root@fyh gits]# git commit -m "add this is 3"

# 到此本地仓库应该有三个不同的版本,工作区的版本是最新的
# 可以通过git log 查看我们提交的信息
[root@fyh gits]# git log
commit 2b6fc6946aa18aa16c24067b96f91568ed9e09d1  # commit是版本号,我们版本回退的时候需要用到它
Author: fyh <1334056960@qq.com>  # Author是用户信息
Date:   Mon Oct 18 14:33:36 2021 +0800  # 时间

    add this is 3  # 提交注释

commit 5a982a4d22df71c0b3f0d9794231203a2089c564
Author: fyh <1334056960@qq.com>
Date:   Mon Oct 18 14:32:14 2021 +0800

    add this is 2

commit e76ceeb17e353568442e2b329213051d5b1e6cf8
Author: fyh <1334056960@qq.com>
Date:   Mon Oct 18 14:25:58 2021 +0800

    add test.txt

# cat 看一下test.txt文件内容
[root@fyh gits]# cat test.txt 
this is test
this is 2
this is 3
# 回退到最新的版本,也就是只有一行this is test
# git reset --hard [版本号]
[root@fyh gits]# git reset --hard e76ceeb17e353568442e2b329213051d5b1e6cf8
HEAD is now at e76ceeb add test.txt
[root@fyh gits]# cat test.txt 
this is test
# 再看一下git log
[root@fyh gits]# git log
commit e76ceeb17e353568442e2b329213051d5b1e6cf8
Author: fyh <1334056960@qq.com>
Date:   Mon Oct 18 14:25:58 2021 +0800

    add test.txt
# 但现在又想回到最新的版本
# 可以通过git reflog查看本地仓库所有的版本信息
[root@fyh gits]# git reflog
e76ceeb HEAD@{0}: reset: moving to e76ceeb17e353568442e2b329213051d5b1e6cf8
2b6fc69 HEAD@{1}: commit: add this is 3
5a982a4 HEAD@{2}: commit: add this is 2
e76ceeb HEAD@{3}: commit (initial): add test.txt
# 回到最新的版本
[root@fyh gits]# git reset --hard 2b6fc69
[root@fyh gits]# cat test.txt 
this is test
this is 2
this is 3

工作区与暂存区

上面也将了,我们提交文件需要通过暂存区去提交给仓库的。

# 通过git status可以查看当前版本库的状态
[root@fyh gits]# git status
# On branch master
nothing to commit, working directory clean
# 可以看到提示,我们并没有做任何的修改,当我们添加第四行以后再看一下
[root@fyh gits]# vim test.txt
this is test
this is 2
this is 3
this is 4

[root@fyh gits]# 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:   test.txt
#
no changes added to commit (use "git add" and/or "git commit -a") # 提示我们需要添加到暂存区
# 将文件添加到暂存区再看一下
[root@fyh gits]# git add test.txt
[root@fyh gits]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	modified:   test.txt
#
# 提交到仓库
[root@fyh gits]# git commit -m "add this is 4"
[root@fyh gits]# git status
# On branch master
nothing to commit, working directory clean

管理修改

正常的流程是先add添加到暂存区,在commit添加到仓库,我们试试直接提交到仓库看看有什么变化吗?

[root@fyh gits]# vim test.txt
# 提示我们需要添加 或者 加上-a参数 
[root@fyh gits]# git commit -m "add this is 5"
# 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:   test.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
# 试试添加-a参数
[root@fyh gits]# git commit -am "add this is 5"
[master ddc881f] add this is 5
 1 file changed, 1 insertion(+)
 

看来还是必须要将文件添加暂存区才能提交给仓库,所以可以肯定git add只是将文件放在暂存区,git commit只负责将暂存区的文件更新到仓库。

撤销修改

[root@fyh gits]# vim test.txt 
[root@fyh gits]# cat test.txt 
this is test
this is 2
this is 3
this is 4
this is 5
this is 6
[root@fyh gits]# 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:   test.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
# 可以看(use "git checkout -- <file>..." to discard changes in working directory),提示你可以丢弃工作区的修改
[root@fyh gits]# git checkout -- test.txt 
# 可以看见我们添加的第六行确实消失了
[root@fyh gits]# cat test.txt 
this is test
this is 2
this is 3
this is 4
this is 5

命令git checkout -- readme.txt意思就是,把readme.txt文件在工作区的修改全部撤销,这里有两种情况:

一种是readme.txt自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;

一种是readme.txt已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。

总之,就是让这个文件回到最近一次git commit或git add时的状态。

删除文件

说到删除文件,第一反应肯定rm

[root@fyh gits]# rm -f test.txt 
[root@fyh gits]# ls

用git status查看一下,工作区和版本库就不一致了,git status命令会立刻告诉你哪些文件被删除了

[root@fyh gits]# 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:    test.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

现在还有两个选择:1.使用git checkout恢复文件。2.使用gti rm删除仓库中的文件

[root@fyh gits]# git checkout -- test.txt
# test.txt文件确实回来了
[root@fyh gits]# ls
test.txt

# 使用git rm删除呢
[root@fyh gits]# git rm test.txt 
rm 'test.txt'
[root@fyh gits]# git commit -m "delete test.txt"
[master 6d9e229] delete test.txt
 1 file changed, 5 deletions(-)
 delete mode 100644 test.txt
[root@fyh gits]# git status
# On branch master
nothing to commit, working directory clean


这篇关于git的基本使用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程