Git常用命令分类汇总

一、基础操作
-
初始化仓库
bashgit init
-
添加文件到暂存区
bashgit add file_name # 添加单个文件 git add . # 添加所有修改
-
提交更改
bashgit commit -m "提交描述"
-
查看仓库状态
bashgit status
二、分支管理
-
创建/切换分支
bashgit branch branch_name # 创建分支 git checkout branch_name # 切换分支 git checkout -b new_branch # 创建并切换分支
-
合并分支
bashgit merge branch_name # 将指定分支合并到当前分支
-
删除分支
bashgit branch -d branch_name # 删除已合并的分支 git branch -D branch_name # 强制删除未合并的分支
三、远程仓库
-
关联远程仓库
bashgit remote add origin [email protected]:user/repo.git
-
推送代码
bashgit push origin branch_name # 首次推送需加 -u 参数
-
拉取代码
bashgit pull origin branch_name # 拉取并合并远程分支 git fetch origin # 仅获取远程更新(不合并)
-
克隆仓库
bashgit clone [email protected]:user/repo.git
四、撤销与回退
-
撤销工作区修改
bashgit checkout -- file_name # 撤回指定文件的修改
-
重置提交历史
bashgit reset HEAD~1 # 回退到前一次提交(保留修改) git reset --hard HEAD~1 # 强制回退(丢弃修改)
-
修改最近提交
bashgit commit --amend # 修正提交描述或内容
五、日志与对比
-
查看提交历史
bashgit log # 完整日志 git log --oneline --graph # 简化版可视化日志
-
查看文件差异
bashgit diff # 工作区与暂存区的差异 git diff HEAD # 工作区与最新提交的差异
六、其他实用命令
-
暂存临时修改
bashgit stash # 保存未提交的修改 git stash pop # 恢复暂存的修改
-
标签管理
bashgit tag v1.0 # 创建标签 git push origin --tags # 推送所有标签到远程
注意事项
- 使用
git reset --hard
前需确认已保存所有修改 - 协作开发时,建议频繁
git pull --rebase
减少冲突 - 敏感操作(如强制推送)前备份代码