git/GitHub进阶学习

本文最后更新于:16 天前

每一次的回首,都是对过往的温柔告别,也是对未来勇敢前行的期许。

git/GitHub 进阶学习

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

提交历史

2024 年 8 月 28 日

idea拉取项目报错:master has no tracked branch或master 没有跟踪的分支-CSDN博客

[Git版本控制常见问题记录_git代码版本控制的问题-CSDN博客](https://blog.csdn.net/HTxinghai/article/details/135867596?ops_request_misc=%7B%22request%5Fid%22%3A%220E0816F9-79A7-4088-86B5-1B1159ACA63B%22%2C%22scm%22%3A%2220140713.130102334.pc%5Fall.%22%7D&request_id=0E0816F9-79A7-4088-86B5-1B1159ACA63B&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-2-135867596-null-null.142^v100^pc_search_result_base4&utm_term=git 协同常见报错&spm=1018.2226.3001.4187)

[Merge还是Rebase?这次终于懂了-CSDN博客](https://blog.csdn.net/qq_47831505/article/details/135433009?ops_request_misc=%7B%22request%5Fid%22%3A%2231FEFAD6-5FD2-4246-BFE0-9C017ABAFA8C%22%2C%22scm%22%3A%2220140713.130102334..%22%7D&request_id=31FEFAD6-5FD2-4246-BFE0-9C017ABAFA8C&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-135433009-null-null.142^v100^pc_search_result_base4&utm_term=merge rebase&spm=1018.2226.3001.4187)

fatal: refusing to merge unrelated histories-CSDN博客

原因: 远程和本地两个仓库的历史记录不相关 ,Git 默认不允许这样的操作 (例如:你的新仓库是全新的,没有任何提交历史), 因为它可能会导致文件冲突或其问题。

解决方法:分两步完成同时在idea中终端当中进行操作

1.这个命令会将远程仓库 origin 的 master 分支的内容拉取到当前分支。

1
git pull origin master --allow-unrelated-histories

2.使用本地master分支来设置跟踪远程的master分支

1
git branch --set-upstream-to=origin/master master

git update 时出现 Before update your uncommitted changes were saved to shelf. 在更新之前,未提交的变更已保存到 shelf。-CSDN博客

git使用IDEA工具更新代码,导致未提交代码被覆盖解决办法_local changes were not restored before update your-CSDN博客

image-20240828194819551

冲突

2024 年 8 月 29 日

当你在执行 git pull origin master --allow-unrelated-histories 命令时遇到了一个错误,这主要是因为 Git 检测到你的本地分支上有一些文件(如 .gitignore.mvn/wrapper/maven-wrapper.propertiesmvnwmvnw.cmd)已经被修改,而这些修改与远程分支(origin/master)上的相应文件存在冲突,导致合并操作无法直接进行。

为了解决这个问题,你有几个选项:

  1. 提交你的本地更改
    如果你确实想要保留这些本地更改,并且认为它们应该被包含在项目中,你可以先提交这些更改到你的本地分支,然后再尝试合并。

    1
    2
    3
    git add .gitignore .mvn/wrapper/maven-wrapper.properties mvnw mvnw.cmd  
    git commit -m "Commit local changes before pull"
    git pull origin master --allow-unrelated-histories

    注意,如果远程分支上的这些文件也有更新,你可能会遇到合并冲突,需要手动解决。

  2. 撤销本地更改
    如果你不想保留这些本地更改,可以撤销它们,然后再进行合并。

    1
    2
    git checkout -- .gitignore .mvn/wrapper/maven-wrapper.properties mvnw mvnw.cmd  
    git pull origin master --allow-unrelated-histories

    这个命令会将这些文件恢复到最近一次提交的状态。

  3. 使用 stash
    如果你暂时想保存这些更改,但又想先合并远程分支的更改,可以使用 git stash

    1
    2
    3
    git stash  
    git pull origin master --allow-unrelated-histories
    git stash pop

    git stash 会将你的工作区更改保存到一个临时的栈中,然后你可以安全地执行合并操作。合并完成后,使用 git stash pop 来恢复你的更改。如果合并后你的更改与新的代码存在冲突,你可能需要手动解决这些冲突。

  4. 检查 .gitignore 是否需要更新
    如果你正在修改的 .gitignore 文件是为了添加或删除某些不需要被 Git 跟踪的文件,那么你应该考虑是否需要在合并远程更改之前更新这个文件。如果远程分支上的 .gitignore 文件已经包含了你的更改,你可能就不需要再修改它了。

选择哪种方法取决于你的具体需求和当前的工作流程。如果你是在准备面试或提交简历的过程中遇到这个问题,确保你的工作区是干净的,以避免在面试过程中遇到意外的 Git 问题。

.gitignore 冲突

2024 年 9 月 2 日

gitignore文件使用方法(gitignore教程)(git status –ignored)(git check-ignore -v <file>)-CSDN博客

.gitignore文件不起作用,解决方法_.gitignore 不生效-CSDN博客

maven项目的gitignore文件自动生成在.idea文件中 并且没有自动配置_idea不会自动生成gitignore-CSDN博客

image-20240902173116787

17:23:59.488: [hawthorn-monitor-backend] git -c core.quotepath=false -c log.showSignature=false merge origin/cy error: Your local changes to the following files would be overwritten by merge: .gitignore Please commit your changes or stash them before you merge. Aborting Merge with strategy ort failed.

这个错误信息表明你在尝试合并 origin/cy 分支到你的当前分支时遇到了问题,因为 .gitignore 文件在你的本地有修改,而这些修改会与合并操作冲突。Git 不允许合并操作覆盖你的本地更改,所以合并被中止了。

为了解决这个问题,你有几个选项:

  1. 提交你的更改
    如果你认为 .gitignore 文件中的更改是重要的,并且你希望保留这些更改,你可以先将它们提交到你的当前分支。

    1
    2
    git add .gitignore  
    git commit -m "Update .gitignore"

    然后再尝试合并:

    1
    2
    3
    bash复制代码

    git merge origin/cy
  2. 储藏你的更改(Stashing):
    如果你现在不想提交这些更改,但是想先合并分支,你可以使用 git stash 命令来储藏这些更改。这会将你的工作目录和暂存区的更改保存起来,然后你可以安全地合并分支。合并完成后,你可以使用 git stash pop 来恢复你的更改。

    1
    2
    3
    git stash  
    git merge origin/cy
    git stash pop
  3. 检查 .gitignore 文件
    在合并之前,检查 .gitignore 文件中的更改是否确实需要。有时候,一些临时的或不必要的更改可能会被错误地添加进去。如果是这样,你可以简单地撤销这些更改,然后再进行合并。

    1
    2
    git checkout -- .gitignore  
    git merge origin/cy

选择哪种方法取决于你的具体情况和偏好。如果你不确定,可以先提交你的更改,这样你就可以在合并后更容易地跟踪和管理你的代码变更。

本地 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

Git出现 FETCH_HEAD fatal: refusing to merge unrelated histories解决方法-CSDN博客

1
git pull origin master --allow-unrelated-histories

参与 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进阶学习
https://test.atomgit.net/blog/2023/06/04/git-GitHub进阶学习/
作者
Memory
发布于
2023年6月4日
更新于
2024年9月2日
许可协议