返回

Git 常用命令速查手册:从入门到精通

前端

目录

  • 入门
  • 分支管理
  • 提交和合并
  • 解决冲突
  • 暂存和忽略
  • 远程仓库
  • 高级命令

入门

  • git init: 初始化一个新的 Git 仓库。
  • git clone <url>: 克隆一个现有的 Git 仓库。
  • git status: 显示当前工作目录的状态。
  • git add <file>: 将文件添加到暂存区。
  • git commit -m "commit message": 提交更改到本地仓库。

分支管理

  • git branch <branch-name>: 创建一个新的分支。
  • git checkout <branch-name>: 切换到另一个分支。
  • git merge <branch-name>: 将另一个分支合并到当前分支。
  • git rebase <branch-name>: 将当前分支重新基于另一个分支。

提交和合并

  • git commit -a: 提交所有更改到本地仓库。
  • git push <remote-name> <branch-name>: 将本地更改推送到远程仓库。
  • git pull <remote-name> <branch-name>: 从远程仓库拉取更改。
  • git merge <branch-name>: 将另一个分支合并到当前分支。

解决冲突

  • git diff <file>: 查看两个版本之间的差异。
  • git mergetool <file>: 使用合并工具解决冲突。
  • git add <file>: 将冲突后的更改添加到暂存区。
  • git commit -m "conflict resolution": 提交冲突解决。

暂存和忽略

  • git add <file>: 将文件添加到暂存区。
  • git stash: 将未暂存的更改暂时保存。
  • git stash pop: 恢复暂存的更改。
  • git rm <file>: 从 Git 跟踪中删除文件。
  • .gitignore: 指定要忽略的文件和目录。

远程仓库

  • git remote add <name> <url>: 添加一个远程仓库。
  • git push <remote-name> <branch-name>: 将本地更改推送到远程仓库。
  • git pull <remote-name> <branch-name>: 从远程仓库拉取更改。
  • git clone <url>: 克隆一个现有的 Git 仓库。

高级命令

  • git bisect: 查找错误引入的提交。
  • git cherry-pick <commit-hash>: 从另一个分支选择并应用单个提交。
  • git revert <commit-hash>: 撤消一个或多个提交。
  • git blame <file>: 查看文件的提交历史。
  • git log --graph: 以图形方式查看提交历史。

结论

掌握 Git 命令是现代开发工作流程的关键部分。本手册提供了 Git 常用命令的全面概述,从入门到精通。通过遵循这些命令,您可以有效地管理您的代码、协作开展项目并提升您的开发效率。