当前位置: 首页 > news >正文

【Git】-- Git基本操作

文章目录

  • 2. Git基本操作
    • 2.1 创建本地仓库
    • 2.2 配置本地仓库
      • 方式1:单独设置
      • 方式2:全局设置
    • 2.3 Git工作原理
    • 2.4 文件操作
      • 2.4.1 添加文件
      • 2.4.2 修改文件
        • 查看当前仓库的状态
        • 查看当前工作区和暂存区的具体差异
      • 2.4.3 删除文件
        • 方式一
        • 方式二
    • 2.5 .git文件
      • 2.5.1 查看git目录
      • 2.5.2 git对象
      • 2.5.3 查看指针指向的分支
      • 2.5.4 查看分支
      • 2.5.5 查看commit id
      • 2.5.6 查看tree对象
    • 2.6 查看提交记录
    • 2.7 版本回退
    • 2.8 撤销修改
      • 情况一:撤销还没有add的工作区中的内容
      • 情况二:撤销已经add,但还未commit的内容
      • 情况三:撤销已经commit的内容(前提:commit之后,没有执行push操作)

更多Git相关知识: Git专栏

2. Git基本操作

2.1 创建本地仓库

  1. 创建一个目录。仓库需要在文件目录下进行创建。
root@VM-0-3-ubuntu:~# mkdir gitcode/root@VM-0-3-ubuntu:~# cd gitcode
  1. 初始化一个空的Git仓库
root@VM-0-3-ubuntu:~/gitcode# git inithint: Using'master'as the nameforthe initial branch. This default branch name hint: is subject to change. To configure the initial branch name to useinall hint: of your new repositories,whichwill suppress this warning, call: hint: hint:gitconfig--globalinit.defaultBranch<name>hint: hint: Names commonly chosen instead of'master'are'main','trunk'and hint:'development'.The just-created branch can be renamed via this command: hint: hint:gitbranch-m<name>Initialized empty Git repositoryin/root/gitcode/.git/ root@VM-0-3-ubuntu:~/gitcode# la.git

此时会在当前目录下出现一个隐藏文件.git

查看.git 目录

root@VM-0-3-ubuntu:~/gitcode# tree .git/.git/ ├── branches ├── config ├── description ├── HEAD ├── hooks │ ├── applypatch-msg.sample │ ├── commit-msg.sample │ ├── fsmonitor-watchman.sample │ ├── post-update.sample │ ├── pre-applypatch.sample │ ├── pre-commit.sample │ ├── pre-merge-commit.sample │ ├── prepare-commit-msg.sample │ ├── pre-push.sample │ ├── pre-rebase.sample │ ├── pre-receive.sample │ ├── push-to-checkout.sample │ ├── sendemail-validate.sample │ └── update.sample ├── info │ └── exclude ├── objects │ ├── info │ └── pack └── refs ├── heads └── tags

该git目录是追踪管理git仓库的,如果对该目录进行修改的话,会直接将该git仓库破坏掉。

2.2 配置本地仓库

方式1:单独设置

  1. 配置用户名和邮箱
root@VM-0-3-ubuntu:~/gitcode# git config user.name "cuckoo"root@VM-0-3-ubuntu:~/gitcode# git config user.email "buxinyu163@163.com"
  1. 查看配置
root@VM-0-3-ubuntu:~/gitcode# git config -lcore.repositoryformatversion=0core.filemode=truecore.bare=falsecore.logallrefupdates=trueuser.name=cuckoouser.email=buxinyu163@163.com
  1. 删除配置
root@VM-0-3-ubuntu:~/gitcode# git config --unset user.nameroot@VM-0-3-ubuntu:~/gitcode# git config --unset user.emailroot@VM-0-3-ubuntu:~/gitcode# git config -lcore.repositoryformatversion=0core.filemode=truecore.bare=falsecore.logallrefupdates=true

方式2:全局设置

上面的设置方式是只在当前的本地仓库上生效。

但是,在一台服务器上可以创建多个本地仓库,加上--global选项,表示配置在当前机器上所有的本地仓库上。

root@VM-0-3-ubuntu:~/gitcode# git config --global user.name "cuckoo"root@VM-0-3-ubuntu:~/gitcode# git config --global user.email "buxinyu163@163.com"root@VM-0-3-ubuntu:~/gitcode# git config -luser.name=cuckoouser.email=buxinyu163@163.comcore.repositoryformatversion=0core.filemode=truecore.bare=falsecore.logallrefupdates=true

同样删除配置时,也是只需要加上--global选项即可。

2.3 Git工作原理

工作区:电脑上文件的目录。

暂存区/索引 和 版本库 中存放的不是具体的内容,而是一个个git对象的索引。

每add一次,在工作区中修改的内容会写入对象库中一个新的git对象中(Git会为每个被修改/新添加的文件创建一个blob对象,如果文件内容没有变化,就会重用已有的blob对象),同时暂存区中目录树的文件索引会被更新。git追踪管理的是修改,并不是文件。

在创建Git版本库时,Git会自动创建一个唯一的master分支,HEAD指针指向master分支。

当执行commit命令时,HEAD指向的分支会做出相应的更新。此时,暂存区中的目录树才被真正写到版本库中。

2.4 文件操作

2.4.1 添加文件

  1. 创建一个文件
root@VM-0-3-ubuntu:~/gitcode# touch ReadMeroot@VM-0-3-ubuntu:~/gitcode# la.git ReadMe
  1. 在文件中写入内容
root@VM-0-3-ubuntu:~/gitcode# vim ReadMeroot@VM-0-3-ubuntu:~/gitcode# cat ReadMehellogit
  1. 提交到暂存区中

git add .表示:将工作区中所有修改的文件提交到暂存区中。

git add ReadMe表示:指定ReadMe文件添加到暂存区中。

如果想要一次指定多个文件进行add,用空格隔开文件名即可。

root@VM-0-3-ubuntu:~/gitcode# touch file1 file2 file3root@VM-0-3-ubuntu:~/gitcode# lafile1 file2 file3 .git ReadMe root@VM-0-3-ubuntu:~/gitcode# git add .root@VM-0-3-ubuntu:~/gitcode# git commit -m "新增3个文件"[master 46fcecf]新增3个文件3files changed,0insertions(+),0deletions(-)create mode100644file1 create mode100644file2 create mode100644file3
  1. 提交到版本库中
root@VM-0-3-ubuntu:~/gitcode# git commit -m "新增ReadMe文件"[master(root-commit)77fde10]新增ReadMe文件1filechanged,1insertion(+)create mode100644ReadMe

需要注意的是,执行commit命令时,只会将暂存区中的内容提交到版本库中,工作区中未被add的内容不会被提交到版本库中。

2.4.2 修改文件

  1. 修改
root@VM-0-3-ubuntu:~/gitcode# vim ReadMeroot@VM-0-3-ubuntu:~/gitcode# cat ReadMehellogithello world
查看当前仓库的状态
root@VM-0-3-ubuntu:~/gitcode# git statusOn branch master Changes not stagedforcommit:(use"git add <file>..."to update what will be committed)(use"git restore <file>..."to discard changesinworking directory)modified: ReadMe no changes added to commit(use"git add"and/or"git commit -a")

只记录哪些文件被修改了,并不记录修改了哪些内容

查看当前工作区和暂存区的具体差异
root@VM-0-3-ubuntu:~/gitcode# git diffdiff--gita/ReadMe b/ReadMe index 8d0e412..05fe86c100644--- a/ReadMe +++ b/ReadMe @@-1+1,2 @@ hellogit+hello world
  1. 提交到版本库中
root@VM-0-3-ubuntu:~/gitcode# git add .root@VM-0-3-ubuntu:~/gitcode# git commit -m "修改ReadMe文件"[master 97d8589]修改ReadMe文件1filechanged,1insertion(+)

2.4.3 删除文件

方式一
root@VM-0-3-ubuntu:~/gitcode# lafile1 file2 file3 .git ReadMe root@VM-0-3-ubuntu:~/gitcode# rm file1root@VM-0-3-ubuntu:~/gitcode# lafile2 file3 .git ReadMe root@VM-0-3-ubuntu:~/gitcode# git add .root@VM-0-3-ubuntu:~/gitcode# git commit -m "delete file1"[master 07344ec]delete file11filechanged,0insertions(+),0deletions(-)delete mode100644file1
方式二
root@VM-0-3-ubuntu:~/gitcode# lafile2 file3 .git ReadMe root@VM-0-3-ubuntu:~/gitcode# git rm file2rm'file2'root@VM-0-3-ubuntu:~/gitcode# lafile3 .git ReadMe root@VM-0-3-ubuntu:~/gitcode# git statusOn branch master Changes to be committed:(use"git restore --staged <file>..."to unstage)deleted: file2

git rm命令会直接删除工作区和暂存区中的file2。只需要再执行依次commit操作即可。

root@VM-0-3-ubuntu:~/gitcode# git commit -m "delete file2"[master 5a01a0a]delete file21filechanged,0insertions(+),0deletions(-)delete mode100644file2 root@VM-0-3-ubuntu:~/gitcode# git statusOn branch master nothing to commit, working tree clean

2.5 .git文件

2.5.1 查看git目录

root@VM-0-3-ubuntu:~/gitcode# tree .git.git ├── branches ├── COMMIT_EDITMSG ├── config ├── description ├── HEAD ├── hooks │?? ├── applypatch-msg.sample │?? ├── commit-msg.sample │?? ├── fsmonitor-watchman.sample │?? ├── post-update.sample │?? ├── pre-applypatch.sample │?? ├── pre-commit.sample │?? ├── pre-merge-commit.sample │?? ├── prepare-commit-msg.sample │?? ├── pre-push.sample │?? ├── pre-rebase.sample │?? ├── pre-receive.sample │?? ├── push-to-checkout.sample │?? ├── sendemail-validate.sample │?? └── update.sample ├── index ├── info │?? └── exclude ├── logs │?? ├── HEAD │?? └── refs │?? └── heads │?? └── master ├── objects │?? ├── 0e │?? │?? └── 6b1780b73cd9220ec5073dc64b42f7ad4bd945 │?? ├──15│?? │?? └── a37e9ef171cca4a5d985fccd1fcf9414b2c7cf │?? ├──46│?? │?? └── fcecf10dd0b16e4b0a219b60c0f65aead13977 │?? ├──77│?? │?? └── fde109374766c4a3bcf52f069c16f05d9f0532 │?? ├── 8d │?? │?? └── 0e41234f24b6da002d962a26c2495ea16a425f │?? ├── e6 │?? │?? └── 9de29bb2d1d6434b8b29ae775ad8c2e48c5391 │?? ├── info │?? └── pack └── refs ├── heads │?? └── master └── tags19directories,29files

2.5.2 git对象

git对象分为四种:
1. commit 对象
2. blob对象
3. tree对象
4. tag对象

2.5.3 查看指针指向的分支

root@VM-0-3-ubuntu:~/gitcode# cat .git/HEADref: refs/heads/master

2.5.4 查看分支

master指向的就是最新的一次提交的commit id .

root@VM-0-3-ubuntu:~/gitcode# cat .git/refs/heads/master46fcecf10dd0b16e4b0a219b60c0f65aead13977

2.5.5 查看commit id

gitcat-file-pcommitId

2.5.6 查看tree对象

root@VM-0-3-ubuntu:~/gitcode# git cat-file -p 15a37e9ef171cca4a5d985fccd1fcf9414b2c7cf100644blob 8d0e41234f24b6da002d962a26c2495ea16a425f ReadMe100644blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 file1100644blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 file2100644blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 file3 root@VM-0-3-ubuntu:~/gitcode# git cat-file -p 8d0e41234f24b6da002d962a26c2495ea16a425fhellogit


commitID指向的是一个tree对象的引用,tree对象代表的是目录结构,指向的是blob对象(文件内容)和其他的tree对象(子目录)。

2.6 查看提交记录

root@VM-0-3-ubuntu:~/gitcode# git logcommit 46fcecf10dd0b16e4b0a219b60c0f65aead13977(HEAD ->master)Author: cuckoo<buxinyu163@163.com>Date: Sat Jan1015:02:562026+0800 新增3个文件 commit 77fde109374766c4a3bcf52f069c16f05d9f0532 Author: cuckoo<buxinyu163@163.com>Date: Sat Jan1014:45:382026+0800 新增ReadMe文件

更加美观的打印方式:

root@VM-0-3-ubuntu:~/gitcode# git log --pretty=oneline46fcecf10dd0b16e4b0a219b60c0f65aead13977(HEAD ->master)新增3个文件 77fde109374766c4a3bcf52f069c16f05d9f0532 新增ReadMe文件

2.7 版本回退

版本回退其实git只需要修改对应分支指向的commit ID即可。

版本回退reset命令的三个选项
1.--soft:回退版本库中的内容,工作区和暂存区中的内容不进行回退。
2.--mixed:回退版本库和暂存区中的内容,工作区中的内容不进行回退。
3.--hard:回退工作区、暂存区、版本库中的内容。
默认是--mixed选项

这是我们现在的版本

root@VM-0-3-ubuntu:~/gitcode# git log --pretty=oneline97d8589fc0a517644a879826e165ee247020a9ce(HEAD ->master)修改ReadMe文件 46fcecf10dd0b16e4b0a219b60c0f65aead13977 新增3个文件 77fde109374766c4a3bcf52f069c16f05d9f0532 新增ReadMe文件 root@VM-0-3-ubuntu:~/gitcode# lafile1 file2 file3 .git ReadMe root@VM-0-3-ubuntu:~/gitcode# cat ReadMehellogithello world

回退到上一个版本:

root@VM-0-3-ubuntu:~/gitcode# git reset --hard 46fcecf10dd0b16e4b0a219b60c0f65aead13977HEAD is now at 46fcecf 新增3个文件 root@VM-0-3-ubuntu:~/gitcode# lafile1 file2 file3 .git ReadMe root@VM-0-3-ubuntu:~/gitcode# cat ReadMehellogit

只要知道对应版本的commit id就可以实现回退。

2.8 撤销修改

情况一:撤销还没有add的工作区中的内容

修改前:

root@VM-0-3-ubuntu:~/gitcode# cat ReadMehellogithello world

修改后:

root@VM-0-3-ubuntu:~/gitcode# vim ReadMeroot@VM-0-3-ubuntu:~/gitcode# cat ReadMehellogithello world hello cuckoo

撤销修改后:

root@VM-0-3-ubuntu:~/gitcode# git checkout -- ReadMeroot@VM-0-3-ubuntu:~/gitcode# cat ReadMehellogithello world

情况二:撤销已经add,但还未commit的内容

修改前:

root@VM-0-3-ubuntu:~/gitcode# cat ReadMehellogithello world

修改后:

root@VM-0-3-ubuntu:~/gitcode# vim ReadMeroot@VM-0-3-ubuntu:~/gitcode# cat ReadMehellogithello world hello cuckoo

add后:

root@VM-0-3-ubuntu:~/gitcode# git add ReadMeroot@VM-0-3-ubuntu:~/gitcode# git statusOn branch master Changes to be committed:(use"git restore --staged <file>..."to unstage)modified: ReadMe

撤销修改后:

  1. 使用--hard可以使工作区、暂存区都回退到版本库的当前版本。
root@VM-0-3-ubuntu:~/gitcode# git reset --hard HEADHEAD is now at 97d8589 修改ReadMe文件 root@VM-0-3-ubuntu:~/gitcode# cat ReadMehellogithello world
  1. 使用--mixed可以使暂存区中的内容回退到版本库的当前版本,就变成了情况1。
root@VM-0-3-ubuntu:~/gitcode# cat ReadMehellogithello world hello cuckoo root@VM-0-3-ubuntu:~/gitcode# git add ReadMeroot@VM-0-3-ubuntu:~/gitcode# git statusOn branch master Changes to be committed:(use"git restore --staged <file>..."to unstage)modified: ReadMe root@VM-0-3-ubuntu:~/gitcode# git reset --mixed HEAD ReadMewarning:--mixedwith paths is deprecated;use'git reset -- <paths>'instead. Unstaged changes after reset: M ReadMe root@VM-0-3-ubuntu:~/gitcode# git statusOn branch master Changes not stagedforcommit:(use"git add <file>..."to update what will be committed)(use"git restore <file>..."to discard changesinworking directory)modified: ReadMe no changes added to commit(use"git add"and/or"git commit -a")root@VM-0-3-ubuntu:~/gitcode# git checkout -- ReadMeroot@VM-0-3-ubuntu:~/gitcode# git statusOn branch master nothing to commit, working tree clean

情况三:撤销已经commit的内容(前提:commit之后,没有执行push操作)

修改前:

root@VM-0-3-ubuntu:~/gitcode# cat ReadMehellogithello world

修改后:

root@VM-0-3-ubuntu:~/gitcode# vim ReadMeroot@VM-0-3-ubuntu:~/gitcode# cat ReadMehellogithello world hello cuckoo

add & commit 后:

root@VM-0-3-ubuntu:~/gitcode# git add ReadMeroot@VM-0-3-ubuntu:~/gitcode# git commit -m "修改ReadMe文件"[master7963664]修改ReadMe文件1filechanged,1insertion(+)

撤销后:

root@VM-0-3-ubuntu:~/gitcode# git reset --hard HEAD^HEAD is now at 97d8589 修改ReadMe文件 root@VM-0-3-ubuntu:~/gitcode# cat ReadMehellogithello world

几个^就表示回退到前几个版本

http://www.cnnetsun.cn/news/2734342.html

相关文章:

  • AI智能体开发流程
  • AI英语口语助手APP的开发
  • 制造业现场用的SPC能力分析小工具:一键算CPK/PPK,自动生成带规格线的直方图
  • 告别DLL错误:VisualCppRedist AIO全合一运行库终极解决方案
  • 用DeblurGAN-v2拯救你的模糊照片:从手机快照到专业摄影,保姆级实战教程
  • 18 小凌派 rk2206 鸿蒙 liteos 如何通过修改配置文件,编译不通的案例
  • OpenAI万亿IPO前夜豪赌AI基建,谷歌、英伟达等巨头跟风,普通人要为此买单?
  • 5分钟掌握Pulover‘s Macro Creator:Windows自动化神器的终极指南
  • 基于ESP8266与TLC59116的16路LED Web控制方案详解
  • 异步音乐生成API架构深度解析与实战集成指南
  • 免费开源AMD Ryzen调试工具SMUDebugTool:掌握硬件性能的终极指南
  • 终极指南:3分钟免费上手EmotiVoice多音色情感语音合成引擎 [特殊字符]
  • 为什么你的AI秒杀总超时?3类典型数据闭环断裂场景,及TensorRT加速+RedisJSON原子操作修复手册
  • 在Ubuntu 22.04上保姆级安装AutoDock Vina、MGLtools和Open Babel(含环境变量配置避坑指南)
  • 价值变现的终端:AI应用层
  • Ai2Psd终极指南:如何实现Illustrator到Photoshop的无损矢量图层转换
  • 两种方法锁定 PDF,拒绝内容被随意篡改
  • 轻量TVA模型CIM固化精度保障方案
  • IEA-15-240-RWT:15MW海上风力涡轮机开源模型的完整指南
  • Windows热键冲突深度解析:hotkey-detective架构设计与企业级部署指南
  • 基于Arduino与LM35的温度监测系统:从模拟信号采集到LCD显示全解析
  • TechWiz LCD 2D 应用:IPS显示模拟
  • CloudBeaver终极指南:浏览器端多数据库统一管理平台深度解析与实战部署
  • ComfyUI IPAdapter Plus完全指南:轻松实现AI图像精准控制
  • 如何快速掌握ChanlunX:通达信缠论插件的完整使用指南
  • 基于红外传感器与继电器实现低成本非接触式门铃改造方案
  • 本地运行的紫微斗数推演工具:完整支持文墨天机三合四化与十二宫飞化逻辑
  • 终极快速指南:如何3步掌握glogg日志分析开源工具
  • 别再到处找破解版了!Kali Linux 2024最新版一键安装AWVS 14教程(附官方试用版申请与激活)
  • Windows预览版退出与系统稳定化:OfflineInsiderEnroll注册表修改方案深度解析