{"id":20973655,"url":"https://github.com/seayxu/git-guide","last_synced_at":"2026-02-28T16:30:58.760Z","repository":{"id":92629029,"uuid":"85903927","full_name":"seayxu/git-guide","owner":"seayxu","description":"Git 简易指南","archived":false,"fork":false,"pushed_at":"2017-04-25T07:22:06.000Z","size":4,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-04-02T18:07:17.111Z","etag":null,"topics":["git","git-flow","git-guide","github"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/seayxu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2017-03-23T03:42:03.000Z","updated_at":"2024-07-20T15:23:50.000Z","dependencies_parsed_at":"2023-03-04T10:30:55.883Z","dependency_job_id":null,"html_url":"https://github.com/seayxu/git-guide","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seayxu%2Fgit-guide","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seayxu%2Fgit-guide/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seayxu%2Fgit-guide/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seayxu%2Fgit-guide/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/seayxu","download_url":"https://codeload.github.com/seayxu/git-guide/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254142137,"owners_count":22021465,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["git","git-flow","git-guide","github"],"created_at":"2024-11-19T04:20:35.309Z","updated_at":"2026-02-28T16:30:53.723Z","avatar_url":"https://github.com/seayxu.png","language":null,"readme":"# Git 简易指南\n日常常用 Git 命令。 \n\n# 什么是 Git\n\u003e Git 是一个开源的分布式版本控制系统，可以有效、高速的处理从很小到非常大的项目版本管理。\n\n# 常见的 Git 代码托管平台\n\n- [Gitlab](https://gitlab.com/)\n- [Github](https://github.com/)\n- [Git@OSC](http://git.oschina.net/)\n- [Coding](https://coding.net/)\n\n# 名词解释\n\n- **仓库（Repository）**：通常就是一个项目，也可以是多个项目在一起。\n- **版本库**：就是仓库根目录下的`.git`文件夹（是隐藏文件夹），用于跟踪版本修改，可退回。\n- **分支（Branch）**：是基于仓库的平行空间，Git 一般有默认的分支`master`。\n- **工作区（Working Directory）**：按照文件系统就是一个文件夹，项目的根目录，所有的文件内容修改都是在工作区完成。\n- **暂存区（Stage）**：就是即将提交到某分支中的临时存储区域。\n\n\n# 本地初始化（init）\n```\ncd \u003cdir\u003e\ngit init\n```\n\n# 仓库克隆（clone）\n```\ngit clone \u003cremote_repo_url\u003e\n# 指定克隆的目录\ngit clone \u003cremote_repo_url\u003e \u003cdir\u003e\n```\n\n# 远程仓库（remote）\n一般的默认 `remote_name` 为 `origin`。\n\n- 查看\n\n```\n# 查看远程主机\ngit remote\n\n# 查看远程主机的地址\ngit remote -v\n\n# 查看指定远程主机\ngit remote show \u003cremote_name\u003e\n```\n\n- 添加远程主机\n\n```\ngit remote add \u003cremote_name\u003e \u003cremote_repo_url\u003e\n```\n\n- 删除远程主机\n\n```\ngit remote rm \u003cremote_name\u003e\n```\n\n- 重命名远程主机名称\n\n```\ngit remote rename \u003cremote_name\u003e \u003cremote_name_new\u003e\n```\n\n# 添加至暂存区（add）\n\n```\ngit add .\ngit add -A\ngit add --all\ngit add . -f\n# -f是强制操作\n```\n\n# 查看状态（status）\n\n```\ngit status\n```\n\n# 提交修改（commit）\n\n```\ngit commit -m \"提交的描述文字\"\n```\n\n# 查看历史提交记录（log）\n\n```\ngit log\n```\n\n# 查看命令历史（reflog）\n\n```\ngit reflog\n```\n\n# 版本回退（reset）\n在 Git 中 用 `HEAD` 表示当前版本，`HEAD^`表示上一个版本，`HEAD^^`表示上上一个版本，依次类推。由于当退回版本过早这种表示方法不方法，于是就有了简便的写法：`HEAD~回退版本值`。例如：倒数第10次版本就是`HEAD~10`。\n\n```\n# 回退到上次版本\ngit reset --hard HEAD^\n# 回退到上上次版本\ngit reset --hard HEAD^^\n# 回退到倒数第10版本\ngit reset --hard HEAD~10\n\n# 回退到指定提交id的版本\ngit reset --hard commit_id\n```\n\n# 分支操作（branch）\n\n- 创建分支\n\n```\ngit branch \u003cbranch-name\u003e\n```\n\n- 重命名本地分支\n\n```\ngit branch -m \u003cold-branch-name\u003e \u003cnew-branch-name\u003e\n```\n\n- 分支切换\n\n```\ngit checkout \u003cbranch-name\u003e\n```\n\n- 创建分支并切换到该分支\n\n```\ngit checkout -d \u003cbranch-name\u003e\n```\n\n- 删除本地分支：\n\n```\ngit branch -d/-D \u003cbranch-name\u003e\n# -d：删除\n# -D：强制删除\n```\n\n- 查看所有分支\n\n```\ngit branch -a\n```\n\n# 标签管理（tag）\n\n- 查看本地所有标签\n\n```\ngit tag\n```\n\n- 创建标签\n\n给当前分支添加tag\n```\ngit tag \u003ctag-name\u003e\n```\n\n给当指定提交sha-1值添加tag\n```\ngit tag \u003ctag-name\u003e [sha-1]\n```\n\n- 删除本地tag\n\n```\ngit tag -d \u003ctag-name\u003e\n```\n\n# 取回（fetch）\n\n```\n# 将更新全部取回本地\ngit fetch [\u003cremote_repo\u003e | \u003cremote_repo_url\u003e]\n\n# 将指定分支更新取回本地\ngit fetch [\u003cremote_repo\u003e | \u003cremote_repo_url\u003e] \u003cremote-branch-name\u003e\n```\n\n# 拉取（pull）\n相当于 *pull \u0026 merge* 两个操作。\n\n```\n# 拉取远程分支到当前分支\ngit pull origin \u003cremote-branch-name\u003e\n\n# 拉取远程分支到一个新的分支\ngit pull origin \u003cremote-branch-name\u003e:\u003clocal-branch-name\u003e\n```\n\n# 推送（push）\n\n## 分支（branch）\n- 推送本地分支：\n\n```\n# 推送本地分支至远程仓库\ngit push \u003cremote\u003e \u003clocal-branch-name\u003e\n\n# 推送本地分支至远程仓库指定分支名称\ngit push \u003cremote\u003e \u003clocal-branch-name\u003e:\u003cremote-branch-name\u003e\n```\n\n- 查看远程分支\n\n```\ngit branch -r\n```\n\n- 删除远程分支：\n\n```\ngit push origin --delete \u003cbranch-name\u003e\n\n# 推送一个空分支到远程分支，其实就相当于删除远程分支\ngit push origin :\u003cbranch-name\u003e\n```\n\n- 删除不存在对应远程分支的本地分支：\n\n```\ngit remote prune origin\ngit fetch -p\n```\n\n## 标签（tag）\n- 推送本地所有标签到远程仓库\n\n```\ngit push --tags\n```\n\n- 获取远程标签\n\n```\ngit fetch origin tag \u003ctag-name\u003e\n```\n\n- 删除远程标签\n\n```\ngit push origin --delete tag \u003ctag-name\u003e\n\n# 推送一个空标签到远程\ngit push origin :refs/tags/\u003ctag-name\u003e\n```\n\n# 提取内容\n\n从其它分支提取文件\n```\ngit checkout \u003cbranch-name\u003e -- \u003cfile name\u003e\n```\n\n从历史版本提取文件\n```\ngit reset commit_id \u003cfile name\u003e\ngit checkout -- \u003cfile name\u003e\n```\n\n# 合并(merge)\n\n## 1.本仓库内\n\n```\ngit checkout \u003cbase-on-branch\u003e\ngit merge \u003cbe-merge-branch-name\u003e\n# git add .\n# git commit -m \"\"\ngit push origin \u003cbase-on-branch\u003e\n```\n\n## 2.非本仓库内\n\n**第一步: 创建并切换一个新分支，然后拉取要合并的分支。**\n\n```\ngit checkout -b \u003cnew-branch-name\u003e \u003cbase-on-branch\u003e\ngit pull \u003c[remote-http[s] | ssh-url]\u003e \u003cmerge-branch-name\u003e\n```\n\n**第二步: 本地执行合并操作，然后提交合并[推送至远程仓库]。**\n\n```\ngit checkout \u003cbase-on-branch\u003e\ngit merge --no-ff \u003cnew-branch-name\u003e\n# git add .\n# git commit -m \"\"\n# git push origin \u003cbase-on-branch\u003e\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fseayxu%2Fgit-guide","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fseayxu%2Fgit-guide","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fseayxu%2Fgit-guide/lists"}