企业项目管理、ORK、研发管理与敏捷开发工具平台

网站首页 > 精选文章 正文

Git常用命令及操作指南_git的基本操作命令

wudianyun 2025-09-01 16:01:53 精选文章 5 ℃

Git 是开发者必备的版本控制工具,以下是 最常用 Git 命令 的整理,适合日常开发使用:


1. 仓库操作

命令

说明

git init

初始化新仓库

git clone <url>

克隆远程仓库(如 git clone
https://github.com/user/repo.git

git remote -v

查看远程仓库地址

git remote add origin <url>

添加远程仓库


2. 提交与修改

命令

说明

git status

查看文件状态(红色未暂存/绿色已暂存)

git add <file>

添加文件到暂存区(git add . 添加所有)

git commit -m "消息"

提交到本地仓库(-m 加提交说明)

git commit --amend

修改最后一次提交(可修正消息或漏掉的文件)


3. 分支管理

命令

说明

git branch

查看本地分支(-a 查看所有分支,包括远程)

git branch <name>

创建新分支

git checkout <branch>

切换分支

git checkout -b <new-branch>

创建并切换到新分支

git merge <branch>

合并指定分支到当前分支

git branch -d <branch>

删除分支(-D 强制删除未合并的分支)


4. 拉取与推送

命令

说明

git pull

拉取远程分支并合并(相当于 git fetch + git merge

git pull --rebase

拉取并变基(保持提交线整洁)

git push origin <branch>

推送本地分支到远程

git push -u origin <branch>

推送并关联远程分支(首次推送时用)


5. 撤销与回退

命令

说明

git restore <file>

撤销工作区的修改(未 add 的文件)

git reset <file>

从暂存区撤回文件(add 后反悔)

git reset --hard <commit-id>

回退到指定提交(慎用,会丢失改动)

git revert <commit-id>

撤销某次提交(生成新提交,更安全)


6. 查看记录

命令

说明

git log

查看提交历史(--oneline 简洁模式)

git diff

查看工作区与暂存区的差异

git show <commit-id>

查看某次提交的详情


7. 临时保存

命令

说明

git stash

临时保存未提交的改动

git stash pop

恢复最近保存的改动


高频场景示例

  1. 首次推送本地项目到远程
git init
git add .
git commit -m "first commit"
git remote add origin <url>
git push -u origin main
  1. 合并分支后删除旧分支
git checkout main
git merge feature-branch
git branch -d feature-branch
  1. 拉取远程分支并切换
git fetch origin
git checkout -b new-branch origin/new-branch
最近发表
标签列表