Git命令速查

常用Git命令的详细说明和使用示例,是开发者必备的Git参考工具

基础git init

初始化一个新的Git仓库

示例:
git init
基础git clone

克隆远程仓库到本地

示例:
git clone https://github.com/user/repo.git
基础git status

查看工作区状态

示例:
git status
添加提交git add

添加文件到暂存区

示例:
git add filename.txt 或 git add .
添加提交git commit

提交暂存区的更改

示例:
git commit -m '提交信息'
添加提交git commit -am

添加并提交所有修改过的文件

示例:
git commit -am '提交信息'
分支git branch

列出所有本地分支

示例:
git branch 或 git branch -a
分支git branch <name>

创建新分支

示例:
git branch feature-branch
分支git checkout

切换分支

示例:
git checkout branch-name
分支git checkout -b

创建并切换到新分支

示例:
git checkout -b new-branch
分支git merge

合并指定分支到当前分支

示例:
git merge branch-name
分支git branch -d

删除本地分支

示例:
git branch -d branch-name
远程git remote

查看远程仓库

示例:
git remote -v
远程git remote add

添加远程仓库

示例:
git remote add origin https://github.com/user/repo.git
远程git fetch

从远程仓库获取最新更改

示例:
git fetch origin
远程git pull

拉取远程更改并合并

示例:
git pull origin main
远程git push

推送本地更改到远程

示例:
git push origin main
撤销git reset

撤销暂存区的文件

示例:
git reset HEAD filename.txt
撤销git checkout --

撤销工作区的修改

示例:
git checkout -- filename.txt
撤销git revert

撤销指定的提交

示例:
git revert commit-hash
撤销git reset --hard

强制回退到指定版本

示例:
git reset --hard commit-hash
日志git log

查看提交历史

示例:
git log --oneline
日志git log --graph

以图形方式查看分支历史

示例:
git log --graph --oneline --all
日志git diff

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

示例:
git diff
日志git show

查看指定提交的详细信息

示例:
git show commit-hash
标签git tag

列出所有标签

示例:
git tag
标签git tag -a

创建带注释的标签

示例:
git tag -a v1.0 -m '版本1.0'
标签git push --tags

推送所有标签到远程

示例:
git push origin --tags