git 常规操作

65 阅读1分钟

git 常规操作

配置

命令描述
git config --global user.name "Your Name"设置全局用户名
git config --global user.email "youremail@example.com"设置全局用户邮箱
git config --list查看当前 Git 配置信息

创建和克隆仓库

命令描述
git init在当前目录初始化新的 Git 仓库
git clone <repository_url>克隆现有仓库到本地

基本操作

命令描述
git status查看工作区状态
git add 将文件添加到暂存区
git add .将所有更改添加到暂存区
git commit -m "Message"提交暂存区的文件并添加提交信息
git commit -am "Message"将所有已跟踪文件的更改提交
git reset HEAD 取消暂存区的文件
git rm 从版本控制中删除文件
git mv <old_file> <new_file>重命名或移动文件

分支管理

命令描述
git branch查看本地分支
git branch <branch_name>创建新的分支
git checkout <branch_name>切换到特定分支
git checkout -b <branch_name>创建并切换到新的分支
git merge <branch_name>合并指定分支到当前分支
git branch -d <branch_name>删除本地分支

远程操作

命令描述
git remote add 添加远程仓库
git push 将本地分支推送到远程仓库
git pull 拉取远程仓库的变化并合并到本地
git fetch 从远程仓库获取最新变更但不合并到本地
git remote -v查看远程仓库列表

查看历史和信息

命令描述
git log查看提交日志
git log --oneline查看简化版提交日志
git diff查看工作区与暂存区的差异
git diff 查看指定文件在工作区与暂存区的差异
git diff HEAD查看工作区与最新提交的差异
git show <commit_hash>查看特定提交的详细信息
git blame 逐行显示文件的修改历史