Git 常用命令 - 服务器用

我又回来啦,之前写了个人用的 git 命令,现在大多用服务器,和其他人共同使用,需要用到更多命令

如果要新建一个仓库,记得先 git init,不要直接 git add remote origin,不然会把别人的代码提交到自己的仓库

另外记得配置用户,并且尽量不要使用 --global,不然别人会提交你的代码

目录

配置全局用户信息

为单个仓库配置用户信息

检查当前配置

远程仓库交互

分支协作管理

冲突解决与历史修改

团队协作流程示例

标签与版本管理

临时存储变更


配置全局用户信息

使用以下命令设置全局用户名和邮箱,这些信息会应用于所有 Git 仓库:

bash 复制代码
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
git config --global --unset user.email # 后悔药
git config --global --unset user.name

为单个仓库配置用户信息

如果需要为特定仓库设置不同的用户信息,进入该仓库目录后运行:

bash 复制代码
git config user.name "Your Name"
git config user.email "your.email@example.com"

检查当前配置

验证已配置的用户信息:

bash 复制代码
git config --list

或单独查看用户名和邮箱:

bash 复制代码
git config user.name
git config user.email

远程仓库交互

bash 复制代码
git clone <repository_url>  # 克隆远程仓库到本地
git remote -v  # 查看关联的远程仓库
git remote add <name> <url>  # 添加新的远程仓库
git fetch <remote>  # 获取远程分支更新但不合并
git pull <remote> <branch>  # 拉取并合并远程分支(=fetch+merge)
git push <remote> <branch>  # 推送本地提交到远程
git push --set-upstream <remote> <branch>  # 首次推送时建立追踪关系

分支协作管理

bash 复制代码
git branch -a  # 查看所有分支(本地+远程)
git checkout -b <new_branch>  # 创建并切换到新分支
git merge <branch>  # 合并指定分支到当前分支
git rebase <base_branch>  # 变基当前分支到目标分支
git branch -d <branch>  # 删除本地分支
git push origin --delete <branch>  # 删除远程分支

冲突解决与历史修改

bash 复制代码
git diff  # 查看工作区变更
git status  # 检查冲突文件状态
git log --graph --oneline  # 图形化查看提交历史
git reset <commit> --soft  # 回退到指定提交(保留更改)
git commit --amend  # 修改最后一次提交
git revert <commit>  # 创建新的提交来撤销指定提交

团队协作流程示例

多人协作时推荐使用功能分支工作流:

bash 复制代码
git checkout main
git pull origin main  # 确保本地main分支最新
git checkout -b feature/login  # 创建功能分支
# 进行多次提交后...
git push origin feature/login  # 推送分支到远程
# 在Git平台创建Pull Request请求合并

标签与版本管理

bash 复制代码
git tag v1.0.0  # 创建轻量标签
git tag -a v1.0.0 -m "Release version"  # 创建附注标签
git push origin --tags  # 推送所有标签到远程
git checkout v1.0.0  # 切换到特定标签版本

临时存储变更

bash 复制代码
git stash  # 暂存当前工作目录修改
git stash list  # 查看暂存列表
git stash pop  # 恢复最近暂存的修改
git stash apply stash@{1}  # 恢复指定暂存
相关推荐
高山莫衣1 小时前
git rebase多次触发冲突
大数据·git·elasticsearch
码农藏经阁1 小时前
工作中常用的Git操作命令(一)
git
kobe_OKOK_2 小时前
【团队开发】git 操作流程
git·elasticsearch·团队开发
码农垦荒笔记2 小时前
Git 安装闭坑指南(仅 Windows 环境)
windows·git
CC码码12 小时前
管理你的多个 Git 密钥(多平台多账号)
git·gitlab·github
CC码码12 小时前
管理你的多个 Git 密钥(单平台多账号)
git·gitlab·github
大卫小东(Sheldon)12 小时前
GIM 1.5发布了! 支持Windows系统了
git·ai·rust
flying jiang12 小时前
将大仓库拆分为多个小仓库
git
李boyang10 天前
Git(四):远程操作
git
荻野泽溪10 天前
Git新建分支并同步到远程
git