Home Git usage
Post
Cancel

Git usage

这篇文章记录了 git 的一些用法。大致包括:

  • 基础用法
  • git submodule
  • git ssh 配置
  • Finish later …

Github 使用

  1. 首先配置一个 ssh key
    1. ssh-keygen -t ed25519 -C "cjl2559@163.com"
    2. cat <dir>/<file_name>.pub 这里我使用默认路径 ~/.ssh/ 命名成为 id_ed25519,与第三步对应,把pub 放到 github 上/使用 gh cli 登陆可以省去这一步。
    3. 让自己的电脑可以认出这个密钥对 eval "$(ssh-agent -s)" & ssh-add <dir>/<file_name>
  2. Git 默认配置 http / https 协议的端口设置,比如 git clone https:// 就会走 socks5 下面的端口,如果不用 github http/https 协议的功能,这一步可以不配置的,都走 ssh 协议
    1. git config --global http.proxy socks5://127.0.0.1:7891 (把 socks5 转发端口配置在 7891 端口)
    2. git config --global https.proxy socks5://127.0.0.1:7891
    3. git config -l 或者 vi ~/.gitconfig 看看是否正确了
  3. 由于需要 pull/push ,pull/push 走的ssh协议 (github目前已经不支持 http/https 协议对仓库进行操作)
    1. 我们需要让 ssh 访问 github.com 的时候也去走代理端 vi ~/.ssh/config
    2. 使用 ProxyCommand 让 ssh 在匹配到域名 github.com 去走代理协议 socks5 ,下面的指令中 nc 是连接指令,-X 4/5代表 socks4/socks5协议,-x 后面写 ip:port,默认端口是1080,最后再(Optional: 加入 identityfile,如果第一步配置好的话可以不要)
      1
      2
      3
      4
      5
      6
      7
      
      Host github.com
          ProxyCommand nc -X 5 -x 127.0.0.1:7891 %h %p
          HostName %h
          Port 22
          User git
          IdentityFile ~/.ssh/id_ed25519 
          IdentitiesOnly yes
      

大致有 push / commit / add / sreset / rm / stage / merge / rebase …

git rebase

git rebase -i HEAD~2,回退之前的两个提交,进入一个编辑窗口,pick想要保留的提交就OK

  1. pick
  2. squarsh
  3. reword
  4. fixup
  5. 具体查看各个的功能

git remote

1
2
3
git remote -v  # 看下远程的
git remote set-url origin git@github.com:USERNAME/REPONAME.git  # https换成ssh
git remote add origin git@github.com:USERNAME/REPOSITORY.git  # 这样也行

git reset

回退一次提交:git reset HEAD^

回退好几次: git reset HEAD~<num>

git push

git push –set-upstream -f origin chat

–set-upstream 在 remote 上新建本地的分支

-f force push 适合 rebase 后的分支强制提交

Play with github

Github-CLI 工具 gh 的常见使用:

  • 先登陆一下 CLI:gh auth login
  • 远程创建一个仓库:gh repo create --source [repo-directory] --public/--private --remote REPOSITORY-NAME
  • gh 添加 SSH 连接 github,官方教程

    1
    2
    3
    4
    5
    6
    
    conda install gh --channel conda-forge
    gh auth login  # 这里用 https protocal
    ssh-keygen -t ed25519 -C "your_email@example.com"
    eval "$(ssh-agent -s)"
    ssh-add ~/.ssh/id_ed25519
    gh ssh-key add ~/.ssh/id_ed25519.pub --title "personal laptop"
    

Git submodule

Git子模块是一种将一个Git仓库作为子目录包含在另一个仓库中的方式。子模块允许您将外部仓库整合到您自己的项目中,使它们保持独立并独立进行版本控制。

以下是与Git子模块相关的一些关键概念和命令:

  1. 添加子模块,指令后面跟上要包含的仓库的URL在项目中的目标路径
1
git submodule add https://github.com/example/repo.git submodules/repo
  1. 使用
1
2
3
git submodule init  # 初始化子模块
git submodule update  # 更新子模块  考虑 --recursive 递归下载子模块
cd <submodule path> && git chekout main/master  # 把子模块的文件夹上填上内容,不然文件都在.git里面
  1. 移除 How to remove a git submodule? ref-url It is a fairly common need but has a slightly convoluted procedure. To remove a submodule you need to:
    1. Delete the relevant line from the .gitmodules file.
    2. Delete the relevant section from .git/config.
    3. Run git rm –cached path_to_submodule (no trailing slash).
    4. Commit and delete the now untracked submodule files. ‘

Problems

Detach 怎么处理

  1. 得到提交的hash值
  2. git switch -c branch_name commit_hash
  3. git checkout main
  4. git merge branch_name
  5. git push -u origin main
This post is licensed under CC BY 4.0 by the author.

Wandb 使用

Adaboost

Comments powered by Disqus.