Git常用命令

git同步远程仓库

//新建一个upstream的远程主分支
git remote add upstream https://github.com/singerdmx/flutter-quill
//将远程主分支更新到本地
git fetch upstream
//切换到master分支                           
git checkout master
//将远程主分支合并到master                          
git merge upstream/master 
//推送到远程                   
git push       

Git仓库迁移到新的仓库

git clone --bare <旧仓库地址>
cd <克隆的仓库>
git remote rename origin old-origin
git remote add origin <新仓库地址>
git push -u origin --all
git push -u origin --tags

Git撤销远程提交

git revert <commit-hash>  
git push origin <branch-name>
# 重置到指定的提交  
git reset --hard <commit-hash>  
# 强制推送更改到远程仓库  
git push origin <branch-name> --force

生成密钥对

ssh-keygen -t rsa -b 4096 -C "your-comment"

-t rsa: 指定密钥类型为 RSA(默认)。

-b 4096: 密钥长度 4096 位(默认 3072)。

-C: 添加注释(如邮箱或用途),默认是 用户名@主机名

生成文件:~/.ssh/id_rsa(私钥)和 ~/.ssh/id_rsa.pub(公钥)

生成 GitHub 专用密钥(ED25519 类型,推荐)

ssh-keygen -t ed25519

生成服务器专用密钥(RSA 4096 位)

ssh-keygen -t rsa -b 4096 -C "server@example.com" -f ~/.ssh/server_rsa

多密钥管理

在 ~/.ssh/config 中指定不同主机使用不同密钥:

Host gitlab.com
HostName gitlab.com
PreferredAuthentications publickey  
User git
IdentityFile ~/.ssh/id_rsa_gitlab

Host github.com
HostName github.com
PreferredAuthentications publickey  
User git
IdentityFile ~/.ssh/id_rsa_github

Git配置

git config --global user.name "GitLab用户名"
git config --global user.email "GitLab邮箱"
posted @ 2024-01-08 19:14  南极冰川雪  阅读(8)  评论(0)    收藏  举报