‹ 返回笔记 Esc·

Git 命令

操作命令 分支名称:<branch_name> main | master 远程仓库名称:<remote_name> origin 仓库地址:<repository_url> https://gith…

操作命令

  • 分支名称:<branch_name> main | master
  • 远程仓库名称:<remote_name> origin
  • 仓库地址:<repository_url> https://github.com/user/repo.git
  • 提交记录:<commit_hash> HEAD~1 | 72ac7d5
  • 标签名:<tag_name> v1.0.0
  • 暂存区:提交的内容存入的地方
  • 工作区:就是你在电脑里能看到的目录
  • 状态:[U] Untracked (未追踪) ,[A] Index Added (已添加到暂存区) ,[M] Modified Added (已暂存后又修改)

克隆仓库

1git clone <repository_url>                                         # 克隆到当前目录
2git clone <repository_url> <directory_name>        # 克隆到指定目录
3git clone -b <branch_name> <repository_url>        # 克隆特定的分支(-b:--branch)
4git clone --depth 1 <repository_url>                    # 只克隆最新的提交(浅克隆),而不是整个历史

初始化仓库

1git init                                    # 首次初始化或重新初始化现有的仓库
2git init -b <branch_name>    # 初始化仓库并设置指定初始分支名称[--initial-branch=<branch_name>]
3git init -c user.name="n" -c user.email="u@e.com"    # 初始化并设置自定义配置(-c: --config)

文件状态管理

1git status                                            # 查看当前工作区状态
2git add <file>                                    # 添加文件到暂存区
3git reset <file>                                # 从暂存区移除文件,但保留工作区的文件
4git commit -m "commit message"    # 提交暂存区的内容(-m: --message)
5git commit -a -m "commit message"    # 自动将已修改的文件添加到暂存区并提交(-a: all)
6git commit --amend -m "right msg" # 修改最近一次的提交信息,同时保留该提交的文件更改
7git restore <file>                            # 撤销已暂存的文件修改,删除修改的内容
8git restore --staged <file>            # 撤销已暂存的文件修改,不删除修改的内容
9git rm <file>                                        # 删除文件并从暂存区移除
10git rm --cached <file>                    # 只从暂存区删除文件,保留本地文件
11git mv <old_path> <new_path>        # 移动或重命名文件和目录,会自动将这些变更暂存

分支&检出操作

1git branch                                            # 查看本地分支
2git branch -r                                        # 查看远程分支(-r: --remote)
3git branch <branch_name>                # 创建新分支
4git branch -d <branch_name>            # 删除本地分支(-d: --delete)
5git branch -dr <remote_name>/<branch_name>    # 删除单个远程分支引用
6git branch -u <remote_name>/<branch_name>        # 修改默认推送远程仓库(-u: --upstream)
7git checkout tags/<tag_name>                                # 切换到指定标签版本(只读)
8git switch / checkout <branch_name>                    # 切换到指定分支
9git switch -c / checkout -b <branch_name>        # 创建并切换新分支(-c: --create)

远程仓库管理

1git remote                                    # 查看已添加的远程仓库名
2git remote -v                                # 查看所有远程仓库地址(-v: --verbose)
3git remote add <remote_name> <repository_url>            # 添加远程仓库
4git remote rename <old_name> <new_name>                        # 重命名现有的远程仓库
5git remote remove <remote_name>                                        # 移除指定的远程仓库
6git remote set-url <remote_name> <repository_url>    # 设置远程关联分支
7git remote show <remote_name>                        # 显示远程仓库的详细信息
8git push <remote_name> <branch_name>        # 推送本地分支到远程仓库
9git push <remote_name> <branch_name> --force            # 强制推送
10git push <remote_name> -d <branch_name>    # 删除远程仓库中的分支
11git fetch <remote_name>                                    # 从远程仓库获取最新的代码(不进行合并)
12git fetch --prune                                                # 清理本地不存在的远程分支引用
13git pull <remote_name> <branch_name>        # 从远程仓库拉取更新

查看记录和差异

1git log                                                    # 查看提交历史
2git log --oneline                                # 查看简洁的提交历史
3git log <file>                                    # 查看指定文件的修改历史
4git reflog                                            # 查看引用日志
5git blame <file>                                # 查看文件每行的最后修改者和修改时间
6git diff                                                # 查看工作区与暂存区之间的差异
7git diff <commit1> <commit2>        # 查看两个特定提交之间的差异

合并&变基

1git merge <branch_name>                    # 合并指定分支到当前分支
2git rebase <branch_name>                # 变基当前分支到指定分支上

标签管理

  • 标签用于标记某个特定的版本发布或重要的里程碑
1git tag                                                                # 列出标签
2git tag <tag_name>                                        # 创建标签
3git tag -a <tag_name> -m "tag message"# 创建附注标签(-a: annotate)
4git tag -d <tag_name>                                    # 删除标签
5git tag <tag_name> <commit_hash>            # 给历史提交打标签
6git push <remote_name> <tag_name>            # 推送标签到远程仓库
7git push origin --tags                                # 推送所有标签
8git show <tag_name>                                        # 查看标签详细信息
9git push <remote_name> -d <tag_name>    # 删除远程仓库中的标签
10git diff <tag_name> <tag_name>                # 比较两个标签之间的差异

** 撤销与恢复**

1git revert <commit_hash>                    # 撤销特定提交并生成新提交
2git reset <commit_hast>                        # 软重置到指定的提交记录
3git reset --hard <commit_hash>        # 硬重置到指定的提交记录[--soft --mixed]
4git clean -f                                            # 删除本地未跟踪文件(-f: --force) [-d: 目录]

暂存代码

1git stash                                            # 保存当前更改
2git stash list                                # 查看存储列表
3git stash pop                                    # 恢复最近存储,并删除该存储
4git stash pop stash@{index}        # 恢复指定存储,并删除该存储
5git stash apply                                # 恢复最近存储,不删除该存储
6git stash apply stash@{index}    # 恢复指定存储,不删除该存储
7git stash drop stash@{index}    # 删除指定的存储
8git stash clear                                # 清空所有存储

配置

不加--global选项,设置只会应用于当前项目,--unset选项可以取消配置

系统级--system**:**适用于所有用户的配置,存储在 /etc/gitconfig,优先级最低

全局级--global**:**针对当前用户的配置,存储在 ~/.gitconfig,优先级中等

项目级--local**:**针对当前项目的配置,存储在项目的 .git/config,优先级最高

1# 查询配置信息列表(-l: --list),可指定级别查看
2git config -l
3# 查询各级配置文件的确切路径与配置参数
4git config --list --show-origin
5# 配置用户名
6git config --global user.name "Your Name"
7# 配置邮箱
8git config --global user.email "your-email@example.com"
9# 配置 Git 的默认编辑器 [vim | nano | emacs]
10git config --global core.editor "vim"
11# 配置全局忽略文件
12git config --global core.excludesfile ~/.gitignore_global
13# 配置保存认证信息,将凭据以文本形式保存在本地文件(~/.git-credentials)
14git config --global credential.helper store
15# 配置忽略换行符差异(input-Mac Linux true-Windows false)
16git config --global core.autocrlf input
17# 配置拉取操作时是否使用变基 (true: rebase, false: merge)
18git config --global pull.rebase false
19# 配置自动清理本地不存在的远程分支(每次执行 git fetch 或 pull 时)
20git config --global fetch.prune true
21
22# 设置日志显示的时间格式 可选:[iso | local | relative ...]
23git config --global log.date iso
24# 配置默认初始化分支(v2.28 之后为 main)
25git config --global init.defaultBranch main
26# 配置 Git 界面的颜色显示(v1.8.4 默认开启,终端支持颜色显示则开启,反之不显示)
27git config --global color.ui true
28# 配置网络代理
29git config --global http.proxy http://127.0.0.1:10809
30git config --global https.proxy http://127.0.0.1:10809
31# 配置提交信息的模板文件
32git config --global commit.template ~/.gitmessage.txt
33# 配置 git log 的显示格式(一行简短显示)
34git config --global format.pretty oneline
35# 配置安全目录(防止在多用户系统或共享目录中使用 Git 权限不一致的问题)
36git config --global --add safe.directory /opt/homebrew
37# 配置 GitHub 和 GitLab 的 Token
38git config --global github.user "your-username"
39git config --global github.token "your-token"

工作树

1# 查看当前仓库已存在的所有 worktree
2git worktree list
3# 以更详细的方式查看 worktree(显示路径、HEAD、分支)
4git worktree list --porcelain
5# 查看某个 worktree 的详细信息(Git 2.36+)
6git worktree list --verbose
7# 基于已有分支创建 worktree <path>:工作目录路径 <branch>:已有分支名
8git worktree add <path> <branch>
9# 示例:为 feature-login 分支创建独立工作目录
10git worktree add ../feature-login feature-login
11# 基于指定提交(detached HEAD)创建 worktree
12git worktree add <path> <commit>
13# 示例:基于某次提交只读查看代码
14git worktree add ../review a1b2c3d
15# 创建新分支并同时创建 worktree -b <new-branch>:新分支名 <start-point>:基分支或提交
16git worktree add -b <new-branch> <path> <start-point>
17# 示例:从 main 创建 hotfix 分支并独立工作
18git worktree add -b hotfix-001 ../hotfix main
19# 强制创建 worktree(路径已存在时使用,谨慎)
20git worktree add --force <path> <branch>
21# 移除指定 worktree(目录需为干净状态)
22git worktree remove <path>
23# 示例
24git worktree remove ../feature-login
25# 强制移除 worktree(存在未提交修改时,谨慎)
26git worktree remove --force <path>
27# 清理已失效的 worktree 记录
28# 用于:手动删除目录后,清理 Git 内部状态
29git worktree prune
30# 显示将被清理的 worktree(不真正删除)
31git worktree prune --dry-run
32# 设置 worktree 过期时间(默认 3 天)
33git config --global worktree.pruneExpire 7.days
34# 并行开发:主分支 + 功能分支
35git worktree add ../project-feature feature-x
36# 并行调试:生产版本 vs 开发版本
37git worktree add ../project-release release
38git worktree add ../project-dev develop
39# 回归测试:历史版本
40git worktree add ../project-v1 v1.0.0

其他

1git help <command>              # 帮助
2git show                                  # 显示最近一次提交的详细信息
3git show <commit>                  # 显示特定提交的详细信息
4git show <tag>                      # 显示特定标签的详细信息
5git grep "functionName"      # 在 Git 仓库中搜索特定的文本模式
6git prune                                  # 清理所有未被引用的对象