常用的git命令
时间: 2024-10-21 10:54:32
(部分内容来自网络,其真实性存疑,为了避免对您造成误导,请谨慎甄别。)
Git 是一个强大的版本控制系统,常用的 Git 命令可以帮助你管理和控制代码版本。以下是一些常用的 Git 命令及其简要说明:
1. 基本命令
- 初始化一个新的 Git 仓库
bashgit init
- 克隆一个远程仓库
bashgit clone <repository-url>
- 查看当前 Git 配置信息
bashgit config -l
2. 版本控制命令
- 查看当前状态
bashgit status
- 添加文件到暂存区
bashgit add <file> # 添加指定文件
git add . # 添加当前目录下所有更改的文件
- 提交更改
bashgit commit -m "commit message"
- 查看提交历史
bashgit log
- 查看更改的文件
bashgit diff # 查看未暂存的更改
git diff --cached # 查看已暂存的更改
3. 分支管理
- 查看所有分支
bashgit branch
- 创建新分支
bashgit branch <branch-name>
- 切换到指定分支
bashgit checkout <branch-name>
- 创建并切换到新分支
bashgit checkout -b <branch-name>
- 合并分支
bashgit merge <branch-name>
- 删除分支
bashgit branch -d <branch-name>
4. 远程仓库管理
- 查看远程仓库信息
bashgit remote -v
- 添加远程仓库
bashgit remote add <name> <repository-url>
- 推送到远程仓库
bashgit push <remote-name> <branch-name>
- 从远程仓库拉取更新
bashgit pull <remote-name> <branch-name>
- 获取远程仓库信息而不合并
bashgit fetch <remote-name>
5. 标签
- 创建标签
bashgit tag <tag-name>
- 查看标签
bashgit tag
- 推送标签到远程
bashgit push <remote-name> <tag-name>
6. 其他常用命令
- 撤销已暂存的更改
bashgit reset <file> # 从暂存区移除文件,但保持本地更改
- 撤销本地修改
bashgit checkout -- <file> # 还原文件到上一次提交的状态
- 在暂存区与工作区之间恢复文件
bashgit restore <file>
7. 查看帮助
- 查看 Git 命令帮助
bashgit help <command> # 例如:git help commit
这些是 Git 的一些常用命令,掌握这些基本命令可以有效地管理你的代码版本和协作开发。