Git 使用说明及常用命令

Git 是一个版本控制系统,允许开发人员跟踪其代码随时间的变化。它允许多个人同时处理同一个代码库,并且如果出现问题,可以轻松恢复到以前的代码版本。

一、版本检查

要检查系统上安装的 Git 版本,可以在终端或命令提示符中使用以下命令:

1
git --version

这将显示当前安装在系统上的 Git 的版本号。例如,输出可能如下所示:

1
git version 2.39.2

二、配置

要在本地配置 Git,需要设置一些基本属性,以标识你是在 Git 中所做更改的作者。以下是在本地配置 Git 的步骤:

1.设置用户名

1
 git config --global user.name "Your Name"

2.设置用户邮箱

1
git config --global user.email "your-email@example.com"

3.设置常用的文本编辑器

1
git config --global core.editor "editor-of-your-choice"

eg:将首选文本编辑器设置为Nano:

1
git config --global core.editor nano

4.检查git配置

1
 git config --list

如何启动git存储库

1.初始化git存储库

1
2
cd <directory>
git init

2.暂存并提交更改

1
2
3
git add <file> # 添加指定文件
git add .       # 添加所有更改
git commit -m "Your commit message"

file 为需要暂存的文件 Your commit message:具体的更改信息

3.连接远程仓库

1
git remote add origin <repository-url>

4.推送到远程仓库

1
git push origin <branch>

branch 为远程仓库分支

三、常用Git命令

功能 命令
查看状态 git status
查看提交历史 git loggit log --oneline
查看差异 git diff
创建分支 git branch <branch-name>
切换分支 git checkout <branch-name>git switch <branch-name>
合并分支 git merge <branch-name>
拉取远程更新 git pull origin <branch-name>
克隆仓库 git clone <repository-url>
回退版本 git reset --hard <commit-id>
查看分支列表 git branch -a
删除分支 git branch -d <branch-name>
Licensed under CC BY-NC-SA 4.0
最后更新于 Dec 02, 2025 13:34 UTC
使用 Hugo 构建
主题 StackJimmy 设计