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

Git实用指令记录

config

  • 用例:对git最先要做的一个操作就是配置用户名和邮箱,否则无法commit
  • 查看所有可以config的条目,非常之多
    $ git config --list
    core.symlinks=false
    core.autocrlf=true
    core.fscache=true
    color.interactive=true
    color.ui=auto
    help.format=html
    diff.astextplain.textconv=astextplain
    rebase.autosquash=true
    filter.lfs.clean=git-lfs clean -- %f
    filter.lfs.smudge=git-lfs smudge -- %f
    filter.lfs.process=git-lfs filter-process
    filter.lfs.required=true
    credential.helper=!"D:/Software/PortableGit/mingw64/libexec/git-core/git-credential-store.exe"
    user.email=auzfhuang@mail.scut.edu.cn
    user.name=DEDSEC_Roger
    credential.helperselector.selected=store
    core.repositoryformatversion=0
    core.filemode=false
    core.bare=false
    core.logallrefupdates=true
    core.symlinks=false
    core.ignorecase=true
    remote.origin.url=https://<token>@github.com/DEDSEC-Roger/Speaker_Recognition.git
    remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
    branch.main.remote=origin
    branch.main.merge=refs/heads/main
    
  • 但我们需要配置的并不多,只需要配置user.email和user.name即可,配置完后可以查看一下
    $ git config --global user.name DEDSEC_Roger
    $ git config --global user.email auzfhuang@mail.scut.edu.cn
    $ git config --global --list
    user.email=auzfhuang@mail.scut.edu.cn
    user.name=DEDSEC_Roger
    # credential.helperselector是我之前设置的
    credential.helperselector.selected=store
    

clone

  • 用例:从GitHub,clone某个仓库的某个分支(branch)到当前文件夹
  • 找到main分支,复制HTTPS的网址
    在这里插入图片描述
  • 在本地新建一个文件夹,然后运行
    $ git clone https://github.com/DEDSEC-Roger/Speaker_Recognition.git
    Cloning into 'Speaker_Recognition'...
    remote: Enumerating objects: 271, done.
    remote: Counting objects: 100% (11/11), done.
    remote: Compressing objects: 100% (10/10), done.
    remote: Total 271 (delta 5), reused 3 (delta 1), pack-reused 260
    Receiving objects: 100% (271/271), 32.10 MiB | 1.09 MiB/s, done.Resolving deltas: 100% (50/50), done.
    
  • 成功下载该仓库该分支的代码到本地了,是放在一个文件夹里面的,这个文件夹里除了.git文件夹,其他都称为工作区(working directory)
  • 注意:clone包含.git文件夹,如果我们已经做了很多修改,那么.git文件夹会非常大,因为保存了以前的commit,可以采用–depth=1来限制只提取最近一次commit,并采用–branch来指定分支
    $ git clone --depth=1 --branch=main https://github.com/DEDSEC-Roger/Speaker_Recognition.git
    正克隆到 'Speaker_Recognition'...
    remote: Enumerating objects: 77, done.
    remote: Counting objects: 100% (77/77), done.
    remote: Compressing objects: 100% (75/75), done.
    remote: Total 77 (delta 0), reused 65 (delta 0), pack-reused 0
    展开对象中: 100% (77/77), 完成.
    

status

  • 用例:监控本地分支(Your branch)、缓存区(暂存区)(stage、index)和工作区有无发生修改
  • 本地分支和缓存区都是隐藏的,在.git文件夹里,不影响工作区
  • 修改文件夹里的Test.py,然后运行
    $ git status
    On branch main
    Your branch is up to date with 'origin/main'.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.pyno changes added to commit (use "git add" and/or "git commit -a")
    
  • 会告诉你哪些文件被修改了,哪些修改还没有被添加到缓存区

add

  • 用例:将工作区(下文省略)修改的文件,添加到缓存区,添加的是完整路径,因此添加单个文件会创建该文件的整个目录,因此add文件即可,不用担心目录不完整
  • add只会添加工作区发生修改的文件,未修改的文件,即使add,也不会被放到缓存区
  • add添加的语法有很多,如下
    # 当前目录下所有文件
    git add .# 当前目录下单个文件
    git add filename# 当前目录下多个文件
    git add filename1 filename2 filename3# 当前目录下所有.py文件
    # 一个*表示匹配任意数量字符
    # 一个?表示匹配任一(包括无)字符
    # .符号也会被匹配
    git add *.py# 当前目录下所有.pyc, .pyo, .pyd文件
    # 一个[]表示匹配括号内的任一字符,也可以在括号内加连接符,如[0-9]匹配0至9的数
    git add *.py[cod]# 当前目录下除.py文件外的所有文件
    # 一个!在前表示反转规则
    git add !*.py# 整个文件夹,必须是非空文件夹
    git add folder# folder文件夹下,以及子文件夹下的所有文件
    git add folder/*# folder文件夹下,以及子文件夹下的所有.py文件
    # 两个*表示匹配任意子文件夹
    git add folder/**/*.py
    
  • 比如说,在Profile文件夹下,粘贴了一个以Delete结尾的文件夹,这个文件夹里有.txt文件,现在回到Speaker_Recognition文件夹,要把.txt文件,添加到缓存区
    $ git add Profile/*Delete/*.txt
    $ git status
    On branch main
    Your branch is up to date with 'origin/main'.Changes to be committed:(use "git restore --staged <file>..." to unstage)new file:   Profile/ECAPA_TDNN_GLOB_c512-ASTP-emb192-ArcMargin-LM_Delete/dummy.txtChanges 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.py
    

commit

  • 用例:把缓存区的文件都添加到本地分支
  • commit必须用-m写信息,否则无法commit,如果不写信息就按了回车,会进入vim强行让你写,可以按下esc,然后输入:q,最后按下回车退出
  • commit后status查看状态,会提示说本地分支多了一个commit
    $ git commit -m "add files"
    [main 487222b] add files1 file changed, 0 insertions(+), 0 deletions(-)
    create mode 100644 Profile/ECAPA_TDNN_GLOB_c512-ASTP-emb192-ArcMargin-LM_Delete/dummy.txt
    $ git status
    On branch main
    Your branch is ahead of 'origin/main' by 1 commit.(use "git push" to publish your local commits)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.pyno changes added to commit (use "git add" and/or "git commit -a") 
    

restore

  • 用例:进行工作区和缓存区的恢复操作
  • 将工作区已修改的文件恢复到修改之前
    $ git status
    On branch main
    Your branch is up to date with 'origin/main'.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.pyno changes added to commit (use "git add" and/or "git commit -a")
    $ git restore .\Test.py
    $ git status
    On branch main
    Your branch is up to date with 'origin/main'.nothing to commit, working tree clean
    
  • 将缓存区的文件恢复到工作区,工作区文件不变,相当于撤销add操作
    $ git add .\Test.py
    $ git status
    On branch main
    Your branch is up to date with 'origin/main'.Changes to be committed:(use "git restore --staged <file>..." to unstage)modified:   Test.py
    $ git restore --staged .\Test.py
    $ git status
    On branch main
    Your branch is up to date with 'origin/main'.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.pyno changes added to commit (use "git add" and/or "git commit -a")
    

rm

  • 用例:可用于撤销add,本质上是将指定的文件变成Untracked状态,所谓Untracked状态的文件,就是在clone到本地时,分支中没有的文件。建议用git restore --staged filename代替
  • 操作后会显示delete了该文件,然后我们再把该文件改好,再次add该文件,就能把正确的更新放到缓存区
    git add .\Test.py
    $ git status
    On branch main
    Your branch is up to date with 'origin/main'.Changes to be committed:(use "git restore --staged <file>..." to unstage)modified:   Test.py
    $ git rm --cached .\Test.py
    rm 'Test.py'
    $ git status
    On branch main
    Your branch is up to date with 'origin/main'.Changes to be committed:(use "git restore --staged <file>..." to unstage)deleted:    Test.pyUntracked files:(use "git add <file>..." to include in what will be committed)Test.py
    $ git add .\Test.py
    $ git status
    On branch main
    Your branch is up to date with 'origin/main'.Changes to be committed:(use "git restore --staged <file>..." to unstage)modified:   Test.py
    

reset

  • 用例:如果后续又commit了一些文件,但是感觉操作错了,可以用reset
    # --soft表示退回本地分支的commit到缓存区
    # HEAD后面跟几个~就是最近的几次commit
    # HEAD后面也可直接跟~数字,比如HEAD~~等价于HEAD~2
    $ git reset --soft HEAD~# --mixed表示退回本地分支的commit到缓存区,再把缓存区的添加全部去除
    $ git reset --mixed HEAD~# 慎用!--hard表示退回本地分支的commit到缓存区,再把缓存区的添加去掉
    # 再把工作区的修改也恢复,工作区的恢复是全部恢复
    $ git reset --hard HEAD~
    
  • 如果你没有commit任何东西,不要使用reset,因为这样会回退本地分支,这会导致本地分支与远程分支发生差异Your branch and 'origin/main' have diverged, and have 1 and 2 different commits each, respectively. (use "git pull" to merge the remote branch into yours)
  • 发生差异后,会导致无法push本地分支到远程分支,需要先pull远程分支

remote

  • 用例:查看进行pull和push操作的远程分支的信息,由于GitHub要求token才能push分支,所以还需要进行仓库的地址设定
    # 指定仓库名origin,查看该仓库的信息
    # 直接git remote,查看连接了哪些仓库
    $ git remote show origin
    * remote originFetch URL: https://github.com/DEDSEC-Roger/Speaker_Recognition.gitPush  URL: https://github.com/DEDSEC-Roger/Speaker_Recognition.gitHEAD branch: mainRemote branch:main trackedLocal branch configured for 'git pull':main merges with remote mainLocal ref configured for 'git push':main pushes to main (local out of date)
    
  • 先去GitHub申请token,官方教程在此,拿到token后,一定要复制保存到本地
  • 然后设置远程仓库的地址,必须先设置这个带有token的地址,才能在GitHub顺利地push
    $ git remote set-url origin https://<token>@github.com/DEDSEC-Roger/Speaker_Recognition.git
    $ git remote show origin
    * remote originFetch URL: https://<token>@github.com/DEDSEC-Roger/Speaker_Recognition.gitPush  URL: https://<token>@github.com/DEDSEC-Roger/Speaker_Recognition.gitHEAD branch: mainRemote branch:main trackedLocal branch configured for 'git pull':main merges with remote mainLocal ref configured for 'git push':main pushes to main (local out of date)
    

pull

  • 用例:拉取远程分支的更新,使本地分支与远程分支同步up to date
    # pull的对象是某个仓库的某个分支,加上仓库名origin和分支名main更严谨一些
    $ git pull origin main
    remote: Enumerating objects: 7, done.
    remote: Counting objects: 100% (7/7), done.
    remote: Compressing objects: 100% (4/4), done.
    remote: Total 5 (delta 2), reused 0 (delta 0), pack-reused 0
    Unpacking objects: 100% (5/5), 1.27 KiB | 81.00 KiB/s, done.
    From https://github.com/DEDSEC-Roger/Speaker_Recognitionb41da3b..3149ba8  main       -> origin/main
    Updating b41da3b..3149ba8
    Fast-forward...CAPA_TDNN_GLOB_c512-ASTP-emb192-ArcMargin-LM.onnx | Bin 24861931 -> 0 bytesResource/origin.jpg                                  | Bin 1852464 -> 0 bytes2 files changed, 0 insertions(+), 0 deletions(-)delete mode 100644 Model/ECAPA_TDNN_GLOB_c512-ASTP-emb192-ArcMargin-LM.onnxdelete mode 100644 Resource/origin.jpg
    # 此时再查看状态,本地分支的修改还在,而且新增的文件处于Untracked状态
    $ git status
    On branch main
    Your branch is up to date with 'origin/main'.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.pyUntracked files:(use "git add <file>..." to include in what will be committed)Profile/ECAPA_TDNN_GLOB_c512-ASTP-emb192-ArcMargin-LM_Delete/no changes added to commit (use "git add" and/or "git commit -a")
    

push

  • 用例:将本地分支push到远程分支,更新本地分支的修改到远程分支
  • 要push前,可以git remote show对应仓库,如果仓库连不上,git remote show会失败,等成功了,再push,防止中途出问题
    $ git remote show origin
    * remote originFetch URL: https://<token>@github.com/DEDSEC-Roger/Speaker_Recognition.gitPush  URL: https://<token>@github.com/DEDSEC-Roger/Speaker_Recognition.gitHEAD branch: mainRemote branch:main trackedLocal branch configured for 'git pull':main merges with remote mainLocal ref configured for 'git push':main pushes to main (fast-forwardable)
    # push的对象是某个仓库的某个分支,加上仓库名origin和分支名main更严谨一些
    $ git push origin main
    Enumerating objects: 7, done.
    Counting objects: 100% (7/7), done.
    Delta compression using up to 8 threads
    Compressing objects: 100% (4/4), done.
    Writing objects: 100% (4/4), 380 bytes | 380.00 KiB/s, done.
    Total 4 (delta 3), reused 0 (delta 0), pack-reused 0
    remote: Resolving deltas: 100% (3/3), completed with 3 local objects.
    To https://github.com/DEDSEC-Roger/Speaker_Recognition.gitaf33ce1..251eb2b  main -> main
    

.gitignore

  • 用例:指定哪些文件被忽略,只对Untracked状态的文件有用
  • .gitignore不是指令,而是一个文件,在创建GitHub仓库之初,就可以勾选创建.gitignore文件。之后再创建也是可以的,但是创建.gitignore文件前就存在的文件,都处于Tracked状态
  • 不论是在本地分支还是远程分支,不论.gitignore有无指定忽略,Tracked状态的文件发生修改后,都会被git检索出来,参与同步
  • .gitignore文件的语法,和add类似,不再赘述,给出一个小例子
    # Customization
    Audio/**/*.wav
    Model/**/*.onnx
    Profile/**/*.npy# Byte-compiled / optimized / DLL files
    __pycache__/
    *.py[cod]
    *$py.class# C extensions
    *.so
    

rm另一用法

使用前请备份要被删除的文件

  • 用例:将全部文件都变成Untracked状态(.gitignore文件创建前的文件也变成Untracked状态了),然后将全部文件add回去,再commit、push,这样能将.gitignore指定忽略的文件从远程分支全部删除,不被忽略的文件保留
  • 这样做就像是在创建GitHub仓库之初,就创建了.gitignore文件,git clone也不会下载Untracked状态的文件
    $ git rm -r --cached .
    rm '.gitignore'
    rm '.vscode/launch.json'
    rm 'Audio.py'
    rm 'Audio/hzf_certain.wav'
    rm 'Audio/hzf_certain_2.wav'
    ...
    $ git status
    On branch main
    Your branch is up to date with 'origin/main'.Changes to be committed:(use "git restore --staged <file>..." to unstage)deleted:    .gitignoredeleted:    .vscode/launch.jsondeleted:    Audio.pydeleted:    Audio/hzf_certain.wavdeleted:    Audio/hzf_certain_2.wav...
    $ git add .
    $ git status
    On branch main
    Your branch is up to date with 'origin/main'.Changes to be committed:(use "git restore --staged <file>..." to unstage)modified:   Audio.pydeleted:    Audio/hzf_certain.wavdeleted:    Audio/hzf_certain_2.wavdeleted:    Audio/hzf_certain_3.wavdeleted:    Audio/hzf_certain_4.wavdeleted:    Audio/hzf_certain_5.wav...
    $ git commit -m "completely update .gitignore"
    [main fa38978] completely update .gitignore62 files changed, 1673 insertions(+), 1673 deletions(-)delete mode 100644 Audio/hzf_certain.wavdelete mode 100644 Audio/hzf_certain_2.wavdelete mode 100644 Audio/hzf_certain_3.wavdelete mode 100644 Audio/hzf_certain_4.wavdelete mode 100644 Audio/hzf_certain_5.wav...
    $ git push origin main
    
http://www.lryc.cn/news/7000.html

相关文章:

  • 复杂美公链技术重要特色:平行公链架构
  • Java——进制转换的一些内容
  • 使用 Nodejs、Express、Postgres、Docker 在 JavaScript 中构建 CRUD Rest API
  • 电子招标采购系统源码之什么是电子招投标系统?
  • 匹配文件名称模块glob和fnmatch
  • day12_oop
  • 在 Flutter 中使用 webview_flutter 4.0 | js 交互
  • 嵌入式ARM工业边缘计算机BL302的CAN总线接口如何设置?
  • Win11系统如何安装Ubuntu20.04(WSL版本)并安装docker
  • Elasticsearch和Solr的区别
  • 如何在北京买房
  • 使用Proxifier+burp抓包总结
  • 安装华为aab包的处理方式
  • Word处理控件Aspose.Words功能演示:使用 C++ 将 RTF 文档转换为 PDF
  • 【Java|多线程与高并发】进程与线程的区别与联系
  • K8s手工创建kubeconfig
  • 【SQL开发实战技巧】系列(十七):时间类型操作(下):确定两个日期之间的工作天数、计算—年中周内各日期出现次数、确定当前记录和下一条记录之间相差的天数
  • 代码随想录算法训练营第二十八天 | 491.递增子序列,46.全排列,47.全排列 II
  • 使用 Three.js 后处理的粗略铅笔画效果
  • 推荐一些不常见的搜索引擎
  • RabbitMQ工作模式
  • 机器学习在预测脊髓型颈椎病中的应用:一项28名参与者的事后初步研究
  • 【智能计算数学】微积分
  • win10+RTX4070ti+libtorch部署
  • 【Python百日进阶-Web开发-Vue3】Day518 - Vue+ts后台项目5:用户列表
  • Linux内核转储---kdump原理梳理
  • 【C++】从0到1入门C++编程学习笔记 - 实战篇:演讲比赛流程管理系统
  • 04 OpenCV位平面分解
  • Onvif协议如何判断摄像机支持 —— 筑梦之路
  • 情人节new一个对象给你