这篇文章记录了 git 的一些用法。大致包括:
- 基础用法
- git submodule
- git ssh 配置
- Finish later …
Github 使用
- 首先配置一个 ssh key
ssh-keygen -t ed25519 -C "cjl2559@163.com"
cat <dir>/<file_name>.pub
这里我使用默认路径~/.ssh/
命名成为id_ed25519
,与第三步对应,把pub 放到 github 上/使用gh cli
登陆可以省去这一步。- 让自己的电脑可以认出这个密钥对
eval "$(ssh-agent -s)"
&ssh-add <dir>/<file_name>
- Git 默认配置 http / https 协议的端口设置,比如 git clone https:// 就会走 socks5 下面的端口,如果不用 github http/https 协议的功能,这一步可以不配置的,都走 ssh 协议
git config --global http.proxy socks5://127.0.0.1:7891
(把 socks5 转发端口配置在 7891 端口)git config --global https.proxy socks5://127.0.0.1:7891
git config -l
或者vi ~/.gitconfig
看看是否正确了
- 由于需要 pull/push ,pull/push 走的ssh协议 (github目前已经不支持 http/https 协议对仓库进行操作)
- 我们需要让 ssh 访问 github.com 的时候也去走代理端
vi ~/.ssh/config
- 使用
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
- 我们需要让 ssh 访问 github.com 的时候也去走代理端
大致有 push / commit / add / sreset / rm / stage / merge / rebase …
git rebase
git rebase -i HEAD~2
,回退之前的两个提交,进入一个编辑窗口,pick想要保留的提交就OK
- pick
- squarsh
- reword
- fixup
- …
- 具体查看各个的功能
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子模块相关的一些关键概念和命令:
- 添加子模块,指令后面跟上要包含的仓库的URL和在项目中的目标路径
1
git submodule add https://github.com/example/repo.git submodules/repo
- 使用
1
2
3
git submodule init # 初始化子模块
git submodule update # 更新子模块 考虑 --recursive 递归下载子模块
cd <submodule path> && git chekout main/master # 把子模块的文件夹上填上内容,不然文件都在.git里面
- 移除 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:
- Delete the relevant line from the .gitmodules file.
- Delete the relevant section from .git/config.
- Run git rm –cached path_to_submodule (no trailing slash).
- Commit and delete the now untracked submodule files. ‘
Problems
Detach 怎么处理
得到提交的hash值
- git switch -c branch_name commit_hash
- git checkout main
- git merge branch_name
- git push -u origin main
Comments powered by Disqus.