Git常用命令

Git 详细命令列表

1. 项目初始化和克隆

  • git init:在当前目录初始化一个新的 Git 仓库。

  • git clone <repository> \: 克隆一个远程仓库到本地,可选指定本地目录。

2. 文件操作

  • git add <file>: 将文件添加到暂存区。

  • git add .: 将所有修改过的文件添加到暂存区。

  • git status: 查看仓库状态,包括修改过的文件、暂存区文件等。

  • git diff: 查看工作区和暂存区之间的差异。

  • git diff --cached: 查看暂存区和上次提交之间的差异。

  • git diff HEAD: 查看工作区和上次提交之间的差异。

  • git rm <file>: 从工作区和暂存区删除文件。

  • git mv <old-file> <new-file>: 重命名文件。

3. 提交操作

  • git commit -m "<message>": 将暂存区的文件提交到本地仓库,并附带提交信息。

  • git commit -a -m "<message>": 将所有已跟踪文件的修改提交到本地仓库,并附带提交信息。

  • git commit --amend: 修改上次提交,包括提交信息和文件内容。

4. 分支操作

  • git branch: 列出所有分支,当前分支前面会有 * 号。

  • git branch <branch-name>: 创建一个新的分支。

  • git checkout <branch-name>: 切换到指定分支。

  • git checkout -b <branch-name>: 创建并切换到一个新的分支。

  • git merge <branch-name>: 将指定分支合并到当前分支。

  • git branch -d <branch-name>: 删除本地分支。

  • git branch -D <branch-name>: 强制删除本地分支,即使未合并。

5. 远程仓库操作

  • git remote: 列出所有远程仓库。

  • git remote -v: 列出所有远程仓库及其 URL。

  • git remote add <name> <url>: 添加一个新的远程仓库。

  • git remote remove <name>: 删除一个远程仓库。

  • git remote rename <old-name> <new-name>: 重命名一个远程仓库。

  • git fetch <remote>: 从远程仓库获取最新代码,但不合并到本地分支。

  • git pull <remote> <branch>: 从远程仓库获取最新代码并合并到当前分支。

    • --rebase: 使用 rebase 方式合并,而不是 merge。
  • git push <remote> <branch>: 将本地分支推送到远程仓库。

    • -u: 设置本地分支与远程分支的关联,下次推送可以直接使用 git push。

    • -f: 强制推送,即使远程分支有更新。

    • --tags: 推送所有标签。

6. 标签操作

  • git tag: 列出所有标签。

  • git tag <tag-name>: 创建一个新的标签。

  • git tag -a <tag-name> -m "<message>": 创建一个新的标签并附带信息。

  • git tag -d <tag-name>: 删除本地标签。

  • git push <remote> <tag-name>: 将标签推送到远程仓库。

  • git push <remote> --tags: 将所有标签推送到远程仓库。

7. 日志操作

  • git log: 查看提交历史记录。

    • -p: 显示每次提交的差异。

    • --oneline: 以一行显示每次提交的信息。

    • --graph: 以图形化方式显示提交历史。

    • --author=<author>: 筛选特定作者的提交。

    • --since=<date>: 筛选特定日期之后的提交。

    • --until=<date>: 筛选特定日期之前的提交。

8. 其他常用命令

  • git stash: 将当前工作区的修改保存到 stash 中。

  • git stash pop: 从 stash 中恢复最近保存的修改。

  • git stash list: 列出所有 stash。

  • git stash clear: 清空 stash。

  • git cherry-pick <commit-hash>: 将指定的提交应用到当前分支。

  • git rebase <branch-name>: 将当前分支的提交 rebase 到指定分支上。

  • git reset <commit-hash>: 将当前分支回滚到指定的提交。

    • --hard: 彻底回滚,工作区和暂存区也会被重置。

    • --soft: 只回滚提交记录,工作区和暂存区不受影响。

    • --mixed: 回滚提交记录和暂存区,工作区不受影响。

  • git revert <commit-hash>: 通过创建一个新的提交来撤销指定的提交。

相关推荐
CodexDave7 小时前
数据库连接池耗尽:排查顺序与三层兜底
服务器·前端·数据库·git·云原生·容器·kubernetes
乐观的Terry8 小时前
5、发布系统-Git 集成
大数据·git·elasticsearch
不怕犯错,就怕不做8 小时前
GIT的简单打patch应用format-patch and git am
linux·git·全文检索
Byron Loong8 小时前
【Git】如何检查 Ubuntu 系统上 gitLab 是否开启
git·ubuntu·gitlab
lar_slw10 小时前
git删除上一次提交
git
leoZ2311 天前
Git 集成实战完全指南(四):Git 冲突解决
大数据·git·elasticsearch
枫荷1 天前
Git LFS 大文件优化说明
git
techdashen1 天前
不用再反复 stash:用 Git Worktree 同时开发多个分支
大数据·git·elasticsearch
leoZ2312 天前
Git 集成实战完全指南(八):团队协作最佳实践
git
晴雨天️2 天前
Git工具使用指南
笔记·git