使用指定私钥克隆
# 方案1
GIT_SSH_COMMAND='ssh -i ~/.ssh/specified_private_key' git clone git@github.com:username/repository.git
# 方案2
git clone remote_url.git --config core.sshCommand="ssh -i ~/.ssh/specified_private_key"
浅克隆
如果你想克隆的代码库提交记录过多,或者你不关心该仓库的历史提交记录,可以使用浅克隆模式。
# 只拉取最近一次提交
git clone --depth 1 <repository_url>
# 后续想拉取以前的记录
git pull --unshallow
从git库中移除.env文件
echo '**/.env' > .gitignore
git rm --cached **/.env
git add .
git commit -m "Ignore all .env files across all directories"
git push
本地忽略.env文件,但是git库不移除
# 添加某个目录的.env文件
git update-index --assume-unchanged path/to/.env
# 循环添加所有的.env文件
find . -name ".env" -exec git update-index --assume-unchanged {} \;
# 检查是否生效
git ls-files -v | grep '^[a-z]'
#恢复
git update-index --no-assume-unchanged path/to/.env
合并feature分支到main分支,保持main分支为一条直线
# 拉取main分支最新内容
git checkout main
git pull origin main
# 切换到目标feature分支,将main分支rebase到目标分支
git checkout feature/demo
git rebase main
# 如果有冲突,解决冲突后,继续rebase
git rebase --continue
# 切换至main分支,合并feature分支
git checkout main
git merge feature/demo
git push origin main
合并远端tag到本地分支
假设远端tag为0.6.15
git fetch upstream
git checkout -b feature/0.6.15 0.6.15
## rebase这种方式似乎合并的冲突文件比较多,下次合并新的远端tag可能还要解决一次冲突,如果使用merge则冲突较少
# 合并main分支到新的分支
git rebase main
# 如果有冲突,解决冲突后,继续rebase
git rebase --continue
通过https协议代理github的ssh克隆
如果你的代理网络不允许ssh协议,会导致你使用ssh地址clone github仓库时报错。github官方的https监听也代理了ssh协议,可以参考文档:https://docs.github.com/en/authentication/troubleshooting-ssh/using-ssh-over-the-https-port。
# 1. 测试这个方案是不是可以通(确保你的pub key配置在了账号上)
ssh -T -p 443 git@ssh.github.com
# 2. 配置ssh协议走github的443端口(类似于走他们的ng tcp代理)
cat << EOF >> ~/.ssh/config
Host github.com
Hostname ssh.github.com
Port 443
User git
EOF
# 3. 测试
ssh -T git@github.com
# 如果你的机器依赖代理,还需要加这段
export https_proxy=https://localhost:10808