git

git一些问题累积

安装

windows

  1. 下载
    1. 连接1淘宝镜像
    2. 连接2GitforWindows国内下载站
  2. 安装
    1. 因为没装gui,所以右键删掉:
    2. 点击左下角开始菜单 - 运行(输入regedit)- 确定或者回车;
    3. 计算机_CLASSES_ROOT,删掉gui
    4. 关闭注册表

linux

  1. 用户 git config --list
  2. 配置公私秘钥
    1. cd ~/.ssh
    2. ls
    3. ssh-keygen
    4. cat id_rsa.pub,把输出的加到github ssh里

使用

上传

  1. 进入文件夹folder_to_upload,git bash(命令行也行,能登陆到github账号就行)
  2. git init
  3. git add .
  4. git commit -m "随便写什么"
  5. github上新建一个仓库,不要勾选Initialize this repository with:Add a README file(创建之后能看到它的教程的)
  6. 将本地仓库与 GitHub 仓库关联git remote add origin https://github.com/用户名/仓库名.git
  7. git branch -M main
  8. git push -u origin main

可以携程sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/bin/bash

# 检查是否提供 commit 信息
if [ -z "$1" ]; then
# 如果没有提供 commit 信息,使用默认值
commit_message="Auto commit: $(date '+%Y-%m-%d %H:%M:%S')"
echo "No commit message provided. Using default: \"$commit_message\""
else
# 使用用户提供的 commit 信息
commit_message="$1"
fi

# 自动添加、提交并推送
git add .
git commit -m "$commit_message"
git push

# 提示完成
echo "Changes have been added, committed with message \"$commit_message\", and pushed successfully!"

git pull

不保留本地 git reset --hard HEAD

git pull origin main

提交合并

面对This branch is 113 commits ahead of, 23 commits behind huggingface/lerobot:main.怎么处理

rebase

提交历史会清晰,但是工作量比较大 rebase 的过程是:Git 会把你本地分支的 113 次提交,一个个“摘出来”,然后“重新应用”在 huggingface/lerobot:main 的最新 23 个提交之后。

just merge

直接合并的话提交历史很混乱

git fetch upstream git merge upstream/main

版本回退

代码传错了,覆盖了之前的正确版本咋搞

  1. 查询id,找到对应提交的id

    1
    git log --pretty=oneline
  2. 回退

    1
    git reset --hard 正确版本的id

    正确:revert

    1
    git revert 错误版本id

    或者批量revert

    1
    git revert OLDER_COMMIT^…NEWER_COMMIT

上游

添加

删除

在错误的git仓库里面进行了git remote add upstream https://github.com/xx/xx.git的操作之后怎么撤销?

你可以运行以下命令查看当前的远程仓库列表:git remote -v

如果你想撤销这个操作(即删除刚刚添加的 upstream 远程仓库),可以使用以下命令: git remote remove upstream

branch

  1. 创建分支
  2. 删除分支
    1. 本地:git branch -d name-of-branch (没合并的话会报错,用-D可以强制删除)
    2. 远程:git push origin --delete name-of-branch

报错

权限问题

  1. remote: Support for password authentication was removed on August 13, 2021.
    1. 根据官方教程
    2. Settings,最下面的Developer settings,展开Personal access tokens,选中classic那个,Generate new token
    3. token名字随便,过期、用途选repo
    4. 将令牌复制到剪贴板。关闭页面后不能再出现,除非重新生成!
    5. 设置token,使用,填密码的时候粘贴token
    6. 将分支克隆到本地的仓库(xxx.github.io)中的.git文件夹复制到博客文件夹中
    7. 在博客目录下执行命令同步到远程的hexo分支
      1
      2
      3
      git add -A
      git commit -m "备份Hexo(提交的描述)"
      git push origin hexo
  2. remote: Invalid username or token. Password authentication is not supported for Git operations. fatal: 'https://github.com/milong26/lerobot_diy.git/' 鉴权失败

跟上面一样的解决方式,就是generate key,然后当成密码

网络问题

  1. Could not resolve host: github.com

附加功能

lfs

  1. 本地用户安装
    1
    2
    3
    4
    5
    6
    ~/application$ mkdir git-lfs cd git-lfs
    wget https://github.com/git-lfs/git-lfs/releases/download/v3.5.1/git-lfs-linux-amd64-v3.5.1.tar.gz
    tar -xvzf git-lfs-linux-amd64-v3.5.1.tar.gz
    echo 'export PATH="$HOME/application/git-lfs/git-lfs-3.7.0:$PATH"' >> ~/.bashrc
    source ~/.bashrc
    git lfs version

然后在需要lfs的git仓库里面

1
2
git lfs install
git lfs pull

gitignore

.gitignore 文件,强烈建议在新建项目的时候就搞,在里面可以写想忽略的文件或者文件夹,这样git上传的时候就会忽略这个文件

git clone的时候用的https和ssh有什么区别