Git 多用户配置
- 清除全局配置(可选)
如果你想从头开始配置多用户,先清除全局设置:git config --global --unset user.name git config --global --unset user.email
- 设置全局默认用户(可选)
为所有仓库设置一个默认用户:git config --global user.name "Default Name" git config --global user.email "default@example.com"
- 为特定仓库设置独立用户
进入项目目录,执行以下命令(不带–global参数):git config user.name "Project Name" git config user.email "project@example.com"
- 生成ssh免密登录公钥和私钥
如果git配置的账户名不一样 则需要生成不同的rsa公钥ssh-keygen -t rsa -C "default@example.com"
- config配置
配置文件在~/.ssh/config
#Host * # AddKeysToAgent yes # IdentityFile ~/.ssh/id_rsa_git_wdaipc_gmail # 1 github 第一个账号 Host github-x HostName github.com #AddKeysToAgent yes PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_ 替换自己的 User git # 2 github 第二个账号 Host github-xx HostName github.com #AddKeysToAgent yes PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_ 替换自己的 User git # 3 github 地三个账号 Host github-xxx HostName github.com #AddKeysToAgent yes PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_ 替换自己的 User git
- git clone:
github-x
需要换成自己设置的git clone git@github-x:用户名/仓库名.git
- 打开git自动添加密匙
打开~/.bashrc
# 加载新用户密钥(这里保留,确保新密钥加载) #eval "$(ssh-agent -s)" # git_x ssh-add ~/.ssh/id_rsa_ 替换自己的 # gitea_xx ssh-add ~/.ssh/id_rsa_ 替换自己的
git常用命令
- 基础操作
# 初始化仓库 git init # 克隆远程仓库 git clone <远程仓库URL> # 添加文件到暂存区 git add <文件名> # 添加单个文件 git add . # 添加所有文件 # 提交到本地仓库 git commit -m "提交说明" # 查看状态 git status # 查看提交历史 git log git log --oneline # 简洁模式
- 分支管理
# 查看分支 git branch # 本地分支 git branch -a # 所有分支(包括远程) # 创建分支 git branch <分支名> # 切换分支 git checkout <分支名> # 创建并切换分支 git checkout -b <分支名> # 合并分支 git merge <源分支> # 将源分支合并到当前分支 # 删除分支 git branch -d <分支名> # 已合并的分支 git branch -D <分支名> # 强制删除未合并的分支
- 远程仓库操作
# 添加远程仓库 git remote add origin <远程仓库URL> # 推送本地分支到远程 git push -u origin <分支名> # 第一次推送 git push # 后续推送 # 拉取远程更新 git pull origin <分支名> # 查看远程仓库信息 git remote -v
- 撤销与回滚
# 撤销暂存区的文件 git reset <文件名> # 丢弃工作区的修改 git checkout -- <文件名> # 回退到指定提交 git reset --hard <提交哈希值> # 创建新提交来撤销某次提交 git revert <提交哈希值>
- 标签管理
# 创建标签 git tag <标签名> # 轻量标签 git tag -a <标签名> -m "标签说明" # 附注标签 # 推送标签到远程 git push origin <标签名> git push origin --tags # 推送所有标签 # 查看标签 git tag
- 比较差异
# 查看工作区与暂存区的差异 git diff # 查看暂存区与最新提交的差异 git diff --staged # 查看两次提交之间的差异 git diff <提交1> <提交2>
- 其他实用命令
# 查看命令帮助 git help <命令名> # 显示帮助文档 git <命令名> --help # 同上 # 忽略文件 # 创建 .gitignore 文件,写入要忽略的文件/目录 touch .gitignore
正文完