0x00 摘要

Git 的 “ABC”

0x01 简介

Git is the open source distributed version control system.
Git是开源的分布式版本控制系统

0x02 Git Cheat Sheet

A Configure Tooling 〔配置工具〕

  • Configure user information for all local repositories
    为所有本地存储库配置用户信息

Sets the name you want attached to your commit transactions
设置要附加到提交事务的名字

git config --global user.name "[name]"

Sets the email you want attached to your commit transactions
设置要附加到提交事务的邮箱

git config --global user.email "[email address]"

Enables helpful colorization of command line output
启用命令行输出的辅助着色

git config --global color.ui auto

B Create Repository 〔创建存储库〕

  • Start a new repository or obtain one from an existing URL
    启动新的存储库或从现有URL获得一个存储库

Creates a new local repository with the specified name
使用指定的名称创建新的本地存储库

git init [project-name]

Downloads a project and its entire version history
下载项目及其完整版本历史记录

git clone [url]

C Make Changes 〔进行更改〕

  • Review edits and craft a commit transaction
    审查编辑并制定提交事务

Lists all new or modified files to be committed
列出所有要提交的新文件或修改过的文件

git status

Shows file differences not yet staged
显示尚未暂存的文件差异

git diff

Snapshots the file in preparation for versioning
快照文件以备版本控制

git add [file]

Shows file differences between staging and the last file version
显示暂存和上一个文件版本之间的文件差异

git diff --staged

Unstages the file, but preserve its contents
取消暂存文件,但保留其内容

git reset [file]

Records file snapshots permanently in version history
在版本历史记录中永久记录文件快照

git commit -m "[descriptive message]"

D Group Changes 〔组变更〕

Lists all local branches in the current repository
列出当前存储库中的所有本地分支

git branch

Creates a new branch
创建新分支

git branch [branch-name]

Switches to the specified branch and updates the working directory
切换到指定的分支并更新工作目录

git checkout [branch-name]

Combines the specified branch’s history into the current branch
将指定分支的历史记录合并到当前分支

git merge [branch]

Deletes the specified branch
删除指定的分支

git branch -d [branch-name]

E Relocate Filenames 〔重定位文件名〕

  • Relocate and remove versioned files
    重新定位并删除版本控制的文件

Deletes the file from the working directory and stages the deletion
从工作目录中删除文件并将删除过程分阶段进行

git rm [file]

Removes the file from version control but preserves the file locally
从版本控制中删除文件,但在本地保留文件

git rm --cached [file]

Changes the file name and prepares it for commit
更改文件名并准备提交

git mv [file-original] [file-renamed]

F Suppress Tracking 〔抑制追踪〕

  • Exclude temporary files and paths
    排除临时文件和路径

A text file named .gitignore suppresses accidental versioning of files and paths matching the specified patterns
名为.gitignore的文本文件可禁止意外地对与指定模式匹配的文件和路径进行版本控制

*.log
build/
temp-*

Lists all ignored files in this project
列出该项目中所有忽略的文件

git ls-files --other --ignored --exclude-standard

G Save Fragments 〔保存片段〕

Temporarily stores all modified tracked files
临时存储所有已修改的跟踪文件

git stash

Restores the most recently stashed files
还原最近保存的文件

git stash pop

Lists all stashed changesets
列出所有保存的变更集

git stash list

Discards the most recently stashed changeset
丢弃最近保存的变更集

git stash drop

H Review History 〔回顾历史〕

  • Browse and inspect the evolution of project files
    浏览并检查项目文件的演变

Lists version history for the current branch
列出当前分支的版本历史记录

git log

Lists version history for a file, including renames
列出文件的版本历史,包括重命名

git log --follow [file]

Shows content differences between two branches
显示两个分支之间的内容差异

git diff [first-branch]...[second-branch]

Outputs metadata and content changes of the specified commit
输出指定提交的元数据和内容更改

git show [commit]

I Redo Commits 〔重提交〕

  • Erase mistakes and craft replacement history
    清除错误和制作 「存疑」 更换历史

Undoes all commits after [commit] , preserving changes locally
撤消[commit]之后的所有提交,在本地保留更改

git reset [commit]

Discards all history and changes back to the specified commit
放弃所有历史记录并更改回指定的提交

git reset --hard [commit]

J Synchronize Changes 〔同步变更〕

  • Register a repository bookmark and exchange version history
    注册存储库书签并交换版本历史记录

Downloads all history from the repository bookmark
从存储库书签下载所有历史记录

git fetch [bookmark]

Combines bookmark’s branch into current local branch
将书签分支合并到当前本地分支

git merge [bookmark]/[branch]

Uploads all local branch commits to GitHub
上传所有本地分支提交到GitHub

git push [alias] [branch]

Downloads bookmark history and incorporates changes
下载书签历史记录并合并更改

git pull

0x03 常用命令

配置

  • 配置名字

    git config --global user.name "□□□□□"
    

    P.S. □□□□□ 为你的名字

  • 配置邮箱

    git config --global user.email "□□□□□"
    

    P.S. □□□□□ 为你的邮箱

  • 配置 SSH

    • 生成 SSH Key

      ssh-keygen -t rsa -C "□□□□□"
      

      P.S. □□□□□ 为你的邮箱

    • 查看并复制公钥

      cat □□□□□
      

      P.S. □□□□□ 为公钥路径,默认路径为 ~/.ssh/id_rsa.pub

    • 添加SSH公钥

      在 Github 中 Personal settings - SSH and GPG keys 添加公钥即可

使用

  • 提交至暂存区

    git add all
    
  • 提交至 HEAD

    git commit -m "□□□□□"
    

    P.S. □□□□□ 为代码提交信息

  • 提交至远端仓库

    git push
    

0x04 致谢