git
是一个分布式版本控制系统,用于追踪代码的变更历史
初始化仓库
bash
git init # 在当前目录初始化一个新的Git仓库
配置
bash
git config --global user.name "Your Name" # 设置全局用户名
git config --global user.email "[email protected]" # 设置全局邮箱地址
添加文件到暂存区
bash
git add <file> # 将文件添加到暂存区
git add . # 将当前目录下所有更改添加到暂存区
提交更改
bash
git commit -m "Commit message" # 提交暂存区的更改到本地仓库
查看仓库状态
bash
git status # 查看仓库的当前状态
查看提交历史
bash
git log # 查看提交历史
git log --oneline # 查看简化的提交历史
分支管理
bash
git branch # 列出所有本地分支
git branch <branch-name> # 创建新分支
git checkout <branch-name> # 切换到指定分支
git checkout -b <branch-name> # 创建并切换到新分支
git merge <branch-name> # 合并指定分支到当前分支
git branch -d <branch-name> # 删除分支
远程仓库
bash
git remote add origin <remote-repository-url> # 添加远程仓库
git clone <remote-repository-url> # 克隆远程仓库到本地
git fetch origin # 从远程仓库获取更新,但不合并
git pull origin <branch-name> # 从远程仓库拉取更新并合并到当前分支
git push origin <branch-name> # 将本地分支推送到远程仓库
撤销操作
bash
git reset --hard HEAD # 重置当前HEAD为指定状态,丢弃工作区和暂存区的改动
git checkout -- <file> # 撤销工作区的修改
git revert <commit> # 创建一个新的提交,撤销之前的提交所做的更改
储藏更改
bash
git stash # 储藏当前工作区的修改
git stash pop # 应用最近储藏的修改并删除储藏
git stash list # 查看所有储藏
查看差异
bash
git diff # 查看工作区与暂存区的差异
git diff --cached # 查看暂存区与最新本地提交的差异
git diff <commit> # 查看当前分支与指定提交的差异
这些只是 git
命令的冰山一角,git
的功能非常强大且灵活,还有许多高级用法和选项等待你去探索。建议查阅官方文档或相关教程来深入了解 git
的使用方法。
