git常用操作

使用指定私钥克隆

# 方案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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值