git——从入门到精通

git使用教程

本教程仅记录了学习狂神说git时的学习笔记与一些自己使用时的技巧,侵删。
另外,归根结底最全面的资料还是官方文档,大家应该养成遇到难以解决的问题去官方文档上查询的习惯。(虽然我也没有)

准备知识

版本控制

分类

  1. 本地版本控制

适合个人

  1. 集中版本控制

版本数据放在服务器上。

缺点

(1)存在服务器损坏风险

(2)无分支

  1. 分布式版本控制

(1)同步到每个用户本地

(2)可离线提交

Linux基础命令

linux基本指令

在这里插入图片描述

切勿在Linux上尝试 rm -rf / 会格式化电脑

基本理论

在这里插入图片描述

工作区域

  1. 工作目录
  2. 暂存区
  3. 资源库
  4. git远程仓库
    在这里插入图片描述
  • 工作区:本地存放项目文件代码
  • 暂存区:临时存放改动,其实为一个列表信息文件
  • 本地仓库:存放文件变化数据
  • 远程仓库:github/gitee……

克隆项目至本地

  1. 在本地搭建一个仓库
    #初始化
    git init

    #克隆远程仓库
    git clone [url] 
    #url:https://github.com/LancasterLiu/spider
  1. 同步github的文件
	git pull

git相关配置

配置环境变量只是为了可以随时使用,但已存在于右键,无需配置,且已自动配置

  1. 查看
  • 查看配置:git config -l
  • 查看系统配置:git config --system --list
  • 查看本地配置:git config --global --list
  1. 修改配置(本质读写文件)
  • git config --global user.name “xxx”
  • git config --global user.email

修改提交时的用户名名称

  1. 命令行修改(如上)

git config --global user.name xxx

  1. 直接修改.gitconfig文件
    在这里插入图片描述

修改远程仓库地址

  1. 命令行修改
git remote set-url origin <url>

或者

git remote rm origin
git remote add origin [url]
  1. 直接修改config文件
    在这里插入图片描述

远端操作

添加远程仓库

git remote add origin url

移除远程仓库

git remote remove origin

文件操作

文件4种状态:

  1. Untracked(未跟踪):即在文件夹中,但不在git库中。可通过git add .使状态变为Staged
  2. Unmodify(文件已入库但未修改):可通过git rm使状态变为Untracked即移除;或修改文件使状态变为Modified
  3. Modified(已修改):可通过git add .使状态变为Staged;或可通过git checkout使状态变为Unmodify,即还原原来文件
  4. Staged(暂存):可通过git commit使修改同步到git库,状态变为Unmodify;或通过git reset HEAD filename取消暂存,状态变为Modify

查看文件状态

查看状态

#查看所有文件状态
git status

#查看指定文件状态
git status "filename"

更新文件

git pull origin 远端分支:本地分支

添加所有文件到暂存区

#添加所有文件到暂存区
git add .

提交暂存区文件

#提交暂存区文件到git仓库
git commit -m “文件描述”

上传到远端

git push origin 本地分支:远端分支

上传时忽略文件方法:

在主目录下创建.gitignore文件,可以控制不上传哪些文件

  • 使用手册
    • #为注释
    • 可使用linux通配符
    • !代表例外规则,不被忽略
    • /路径分隔符
  • 举例
    #注释
    *.txt		#忽略所有txt文件
    !lib.txt 	#lib.txt除外
    /temp		#不忽略temp
    main/		#忽略main下所有文件
    main/*.txt	#忽略main下所有txt文件

比较差异

可以比较尚未提交的文修改前后的差异

git diff filename

查看日志

git log

版本回退

git回退到上个版本

git reset --hard HEAD^ 

回退到n次提交之前

git reset --hard HEAD~n

配置SSH公钥

  1. 生成公钥文件
    #直接在bash里面输入
    ssh-keygen -t rsa
  1. 复制.pub文件信息至gitee
  2. 创建git远程仓库

分支

  • 查询
    #本地分支
    git branch
    #远程分支
    git branch -r
  • 创建
    #仍停留在原分支
    git branch 分支名
    #切换到新分支
    git checkout -b 分支名
  • 常用分支命名
分支命名说明
主分支master主分支,所有提供给用户使用的正式版本,都在这个主分支上发布
开发分支dev开发分支,永远是功能最新最全的分支
功能分支feature新功能分支,某个功能点正在开发阶段
发布版本release发布定期要上线的功能
修复分支bug修复线上代码的 bug
  • 合并
    #合并指定分支到当前分支
    git merge 分支名
  • 删除
    #本地
    git branch -d 分支名
    #远程
    git branch -dr 分支名
    git push origin --delete 分支名
  • 切换
    git checkout 分支名

常见错误及解决

1.fatal: unable to access ‘https://github.com/…’: OpenSSL SSL_read: Connection was reset, errno 10054

错误描述:

fatal: unable to access ‘https://github.com/…’: OpenSSL SSL_read: Connection was reset, errno 10054

产生原因

一般是因为服务器的SSL证书没有经过第三方机构的签署

解决方法

git config --global http.sslVerify false

2.fatal: The remote end hung up unexpectedly

错误描述:

remote: Enumerating objects: 19, done.
remote: Counting objects: 100% (19/19), done.
remote: Compressing objects: 100% (19/19), done.
error: RPC failed; curl 56 GnuTLS recv error (-54): Error in the pull function.
fatal: The remote end hung up unexpectedly

产生原因

git默认缓存大小不足

解决方法

git config --global http.postBuffer 20000000

3.文件冲突

错误描述:

! [rejected] main -> front (non-fast-forward)
error: failed to push some refs to
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and integrate the remote changes
hint: (e.g. ‘git pull …’) before pushing again.
hint: See the ‘Note about fast-forwards’ in ‘git push --help’ for details.

产生原因

文件冲突

解决方法

git stash
git pull
git stash pop stash@{0}

或者强制更新

git fetch --all
git reset --hard origin/master

注:reset有3种模式:
soft:回退版本提交历史,暂存区和工作区不变。
mixed:回退版本提交历史,暂存区文件与该版本一致,工作区不变。
hard:回退版本提交历史,暂存区和工作区文件与该版本一致。

4.Git bash Error: Could not fork child process: There are no available terminals (-1)

错误描述:

Git bash Error: Could not fork child process: There are no available terminals (-1)

产生原因

打开的git-bash太多

解决方法
打开任务管理器关闭所有的git.exe

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值