> 技术文档 > 常用git命令

常用git命令


常用 Git 指令 ⌨️

命令 (Command) 描述 (Description) 仓库初始化与克隆 (Repository Initialization & Cloning) git init 在当前目录初始化一个新的 Git 仓库。 git clone [url] 克隆一个远程仓库到本地。 git clone -b [branch-name] [url] 克隆远程仓库的特定分支配置 (Configuration) git config --global user.name \"[name]\" 设置全局的提交者名称。 git config --global user.email \"[email]\" 设置全局的提交者邮箱。 git config --local user.name \"[name]\" 设置当前仓库的提交者名称 (覆盖全局设置)。 git config --local user.email \"[email]\" 设置当前仓库的提交者邮箱 (覆盖全局设置)。 git config --list 显示当前的 Git 配置 (包括全局、用户和本地)。 git config --global --edit 编辑全局配置文件git config --local --edit 编辑当前仓库的配置文件。 git config core.autocrlf [true/false/input] 配置行尾换行符的处理方式。 git config --global alias.[alias-name] \"[command]\" 创建 Git 命令的全局别名。 文件操作与暂存 (File Operations & Staging) git add [file] 将文件添加到暂存区。 git add . 添加所有更改过的文件到暂存区。 git add -p 交互式地添加文件更改的部分内容到暂存区。 git status 查看工作区和暂存区的状态。 git status -s 以简短格式显示状态。 git rm [file] 从工作区和暂存区删除文件。 git rm --cached [file] 从暂存区删除文件,但保留在工作区。 git mv [old-name] [new-name] 重命名文件,并将其添加到暂存区。 提交 (Committing) git commit -m \"[message]\" 将暂存区的文件提交到本地仓库,并附带提交信息。 git commit -a -m \"[message]\" 将所有已跟踪文件的更改添加到暂存区并提交 (跳过 git add 步骤)。 git commit --amend 修改最后一次提交 (可以修改提交信息或添加新的更改)。 分支 (Branching) git branch 列出所有本地分支。 git branch -a 列出所有本地和远程分支。 git branch [branch-name] 创建一个新的分支。 git branch -d [branch-name] 删除一个已经合并的本地分支。 git branch -D [branch-name] 强制删除一个本地分支 (即使未合并)。 git checkout [branch-name] 切换到指定分支。 git checkout -b [branch-name] 创建并立即切换到一个新的分支。 git checkout - 切换到上一个分支。 合并与变基 (Merging & Rebasing) git merge [branch-name] 将指定分支的更改合并到当前分支。 git merge --abort 中止当前的合并操作 (如果发生冲突)。 git rebase [branch-name] 将当前分支的提交“变基”到指定分支的顶部。 git rebase -i [commit-ish] 交互式地变基,可以修改、合并、删除提交。 git rebase --abort 中止当前的变基操作。 git rebase --continue 在解决冲突后继续变基操作。 查看历史与差异 (Viewing History & Diffs) git log 查看提交历史。 git log --oneline 以更简洁的单行格式查看提交历史。 git log --graph 以图形方式显示提交历史。 git log --decorate 显示指向提交的分支和标签。 git log --author=\"[name]\" 查看指定作者的提交。 git log -p [file] 查看指定文件的提交历史和具体更改。 git log [branch1]..[branch2] 查看在 branch2 但不在 branch1 中的提交。 git diff 查看工作区与暂存区的差异。 git diff --staged 查看暂存区与上一次提交的差异。 git diff [commit-hash] 查看工作区与指定提交的差异。 git diff [commit1] [commit2] 查看两次提交之间的差异。 git show [commit-hash/tag-name] 显示特定提交或标签的详细信息。 git blame [file] 逐行显示文件的每一行是由谁在哪个提交中最后修改的。 远程仓库 (Remote Repositories) git remote -v 查看配置的远程仓库。 git remote add [name] [url] 添加一个新的远程仓库。 git remote rm [name] 删除一个远程仓库。 git remote rename [old-name] [new-name] 重命名一个远程仓库。 git fetch [remote-name] 从指定的远程仓库下载对象和引用,但不会自动合并。 git fetch --all 从所有配置的远程仓库下载。 git pull [remote-name] [branch-name] 从远程仓库拉取最新的更改并合并到当前分支 (相当于 git fetch + git merge)。 git push [remote-name] [branch-name] 将指定的本地分支推送到指定的远程仓库。 git push [remote-name] --tags 推送所有本地标签到远程仓库。 git push [remote-name] :[remote-branch-name] 删除远程分支 (推送一个空的分支)。 git push --force [remote-name] [branch-name] 强制推送本地分支到远程仓库。谨慎使用,可能会覆盖他人的提交。 撤销与重置 (Undoing & Resetting) git reset [file] 取消暂存文件,但保留工作区的更改。 git reset [commit-hash] 将 HEAD 移动到指定提交,暂存区同步,工作区不变 (默认模式 --mixed)。 git reset --soft [commit-hash] 将 HEAD 移动到指定提交,暂存区和工作区都不变。 git reset --hard [commit-hash] 将 HEAD、暂存区和工作区都重置到指定的提交状态。谨慎使用,会丢失未提交的更改。 git revert [commit-hash] 创建一个新的提交来撤销指定提交的更改 (更安全的方式来撤销公共历史)。 git clean -n 显示将要被删除的未跟踪文件 (演习)。 git clean -f 删除未跟踪的文件。谨慎使用。 git clean -fd 删除未跟踪的文件和目录。谨慎使用。 临时保存 (Stashing) git stash 临时保存当前工作目录的更改,以便切换到其他任务。 git stash save \"[message]\" 带消息地保存临时更改。 git stash list 查看所有保存的临时更改列表。 git stash pop 恢复最近一次保存的临时更改,并从 stash 列表中删除。 git stash apply [stash@{n}] 恢复指定的临时更改 (例如 stash@{0}), 但保留在 stash 列表中。 git stash drop [stash@{n}] 删除指定的临时更改。 git stash clear 清空所有保存的临时更改。 标签 (Tagging) git tag 列出所有标签。 git tag [tag-name] 为当前提交创建一个轻量标签。 git tag -a [tag-name] -m \"[message]\" 创建一个带附注的标签 (推荐)。 git tag -d [tag-name] 删除一个本地标签。 git push [remote-name] [tag-name] 推送指定标签到远程仓库。 git push [remote-name] --delete [tag-name] 删除远程标签。 其他有用的命令 (Other Useful Commands) git help [command] 显示指定命令的帮助信息。 git archive --format=zip --output=[file].zip [branch/commit] 将指定分支或提交打包成 zip 文件。 git bisect start [bad-commit] [good-commit] 使用二分查找来定位引入 bug 的提交。 git bisect good/bad 在二分查找过程中标记当前提交是好的还是坏的。 git bisect reset 结束二分查找。