git/GitHub进阶学习

本文最后更新于:1 年前

git/GitHub进阶学习

  • 第一次学习git还是过年前后做瑞吉外卖时学的,也总结过Git基础的笔记
  • 后来做用户中心、伙伴匹配等项目时也学会了将本地代码提交至Gitee/GitHub的远程仓库,但对git的使用也仅限于此
  • 昨天跟着鱼皮兄学到了在GitHub上如何优雅地在线查看别人项目的代码甚至在线运行,激起了我要持续学习GitHub的兴趣
  • 今天康文昌的git/GitHub教学,简直是醍醐灌顶,有感而发,在此记录:

本地git的基本操作

  • 以下操作均可在VS Code中操作(已集成git基础命令)
  • 初始化用户和邮箱
1
2
git config --global user.name "用户名"
git config --global user.email "用户邮箱"
  • 本地初始化一个仓库
1
git init
  • 将文件修改添加至暂存区
1
git add .c
  • 提交暂存区内的修改记录
1
git commit -m "fix(member):memory"
  • 查看提交记录
1
git log
  • 硬回退至某一次提交版本
1
git reset --hard 提交记录id
  • 创建分支
1
git branch 分支名
  • 选择分支
1
git checkout 分支名

git链接远程仓库

  • 注册并登录GitHub就不多说了,新建一个仓库test
  • 链接至远程仓库(注:origin仅为仓库名,可自定义)
1
git remote add origin 仓库地址
  • 设置主分支(默认分支)
1
git branch -M main
  • 执行更新,并提交至远程仓库
1
2
3
git add .
git commit -m "fix(member):memory"
git push origin
  • 注:这里可能会出现以下几个问题:
1
2
3
4
5
6
7
8
9
10
11
问题一:
————————————————
warning: HTTPS connections may not be secure. See https://aka.ms/gcm/tlsverify for more information.
To https://github.com/deng-2022/test.git
! [rejected] main -> main (fetch first)
error: failed to push some refs to 'https://github.com/deng-2022/test.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first 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.
  • 总结原因以及解决办法如下:
1
2
3
4
5
6
报错的原因是因为,每个仓库都有一个分支,也可以理解为大仓库里的小仓库,我们只是跟线上远程仓库有了关联,但没有跟线上远程仓库的某个分支关联,所以我们没法提交

在终端输入 git pull --rebase origin master 即可跟刚创建的线上远程仓库的默认分支master关联
这时再执行一下 git push -u origin master 即可将我们的项目文件上传到关联的线上远程文件中
————————————————
原文链接:https://blog.csdn.net/weixin_52641692/article/details/130230537
  • 关联线上远程仓库默认分支
1
git pull --rebase origin main
  • 提交项目文件至相关联的远程仓库分支中
1
git push -u origin main
1
2
3
问题二:
————————————————
这个我目前没遇到过,但问题描述是SSL证书过期之类的,总之就是本地的SSL证书有问题
  • 执行以下命令即可:
1
git config --global http.sslverify false

参与GitHub开源项目

  • 这个我在此简述一下吧,操作的多了就会熟练的:
  • 在开源项目中点击点击Fork,把项目拉取到自己的仓库中,执行修改并提交至自己的远程仓库分支中,再远程链接到开源项目仓库地址,回到开源项目中点击Pull requests -> new pull request -> compare across forks,选择自己对应的仓库分支,create pull request即可提交修改,恭喜你,已经完成了第一次向开源项目贡献代码
1
2
3
4
5
6
git clone https://github.com/deng-2022/re01.git .
git remote add upstream https://github.com/midorg-com/re01.git
git checkout -b memory
git add .
git commit -m "add(momber): memory"
git push -u origin memory

git/GitHub进阶学习
http://example.com/2023/06/04/git-GitHub进阶学习/
作者
Memory
发布于
2023年6月4日
更新于
2023年6月5日
许可协议