站内链接:

Classify

参考: git-tutorial

Setting up a repository

  • git init: 创建一个包含.git 目录的本地仓库
  • git clone: 拷贝已有的仓库到本地
  • git config: 配置

Saving Changes

  • git add: adds a change in the working directory to the staging area
  • git commit: commits the staged snapshot to the project history
  • git stash: temporarily shelves (or stashes) changes you’ve made to your working copy so you can work on something else, and then come back and re-apply them later on
  • .gitignore: Ignored files

Inspecting a repository

  • git status: displays the state of the working directory and the staging area
  • git log: displays committed snapshots

Undoing changes

  • git checkout: an easy way to “load” any of these saved snapshots onto your development machine
  • git clean: operates on untracked files
  • git revert: it figures out how to invert the changes introduced by the commit and appends a new commit with the resulting inverse content.
  • git reset: undoing changes

Legend

Basic

基本工作流.

Multiple

跨界操作

Command

Add

功能: 将 working directory 中文件添加到 staged

格式: git add

参考: git add

Commit

功能: staged 中文件生成快照并提交到 HEAD 中

参考: git commit

例子:

1
2
3
4
5
6
7
8
9
# 基本操作
# HEAD: HEAD~1(HEAD^)-上一次提交
Commit-id: git log; git show HEAD


# 跨界
git commit -a # 相当于git add . && git commit
git commit files # 相当于git add files && git commit, untracked file, 不生效.
git commit --amend # 覆盖上一次的提交(获取上一次的commit message, 更改并提交)

Reset

功能: 默认情况下, 同步 HEAD->staged, 覆盖后者中所有内容.

参考: git reset

Checkout

功能: 将文件从 staged 中迁出并覆盖 working directory 文件

参考: git checkout

Stash

功能: 临时将更改放到隔离的文件中, 之后再进行合并操作, 储藏可以获取你工作目录的中间状态——也就是你修改过的被追踪的文件和暂存的变更——并将它保存到一个未完结变更的堆栈中,随时可以重新应用。

参考: git stash

命令:

1
2
3
4
5
6
7
8
# 藏匿代码
git stash
# 更新分支信息(fetch + merge)
git pull origin url1
# 将藏匿去的代码推出
git stash pop
# 退出藏匿去
git stash clear

Roll-back

提交层面

reset

将一个分支的末端指向另外一个提交, 从而实现移除当前分支中的一些提交效果, 当然也可以指定文件, 实现 go-to 功能.

注意, 回滚之后, 会自动丢弃冗余的节点, 无法恢复哦.

checkout

将 HEAD 指针指向另外一个 branch, 不同于 reset 命令, 不涉及悬挂提交, 不会删除提交点, 从而实现快递的查看:旧版本;

但是, 在为分离的 HEAD 创建新的提交点时, 需要首先创建一个新的分支以保存提交点, 否则非常危险.

revert

撤销一个提交的同时, 创建一个新的提交, 从而实现类似 reset 的功能, 通俗讲:我把需要重置的都作为一次新的 update 操作, 类似 binlog 日志, 记录我的删除记录.

文件层面

revert 没有文件层面的操作, 文件层面的操作是最经常使用的场景.

reset

一旦检测到快照中存在某一个文件路径, 会单独对该文件执行 reset 操作.–soft, –mixed, –hard 对文件层面的 git reset 毫无用途,

因为 staged 中的文件一定会发生变化, working directory 一定不会发生变化.

checkout

一旦检测到快照中存在某一个文件路径, 将 HEAD 中的文件复制到 working directory, 不涉及 HEAD 移动, 分支的切换.

clean

对 untracked file 的操作, 需要一定的选项才可以进行, 否则会有报错信息.

1
2
-n          提示需要删除那些信息
-f 强制删除, 正常命令

Reset

参考: git reset

format

git reset [–soft| –mixed [-N] | –hard| –merge] [-q] []

Three trees

Introduction

Three trees – internal state management systems

Command

–hard: staged 和 working directory 都同步到指定的提交点
–soft: staged 和 working directory 都不会改变
–mixed: staged 同步到指定的提交点, working directory 不受影响, 默认选项

Command

command for commit

1
2
3
4
5
6
7
8
9
10
11
12
13
# HEAD 移动到"上一次提交点", staged和 HEAD 保持同步
# working directory可能会增加新文件.
git reset HEAD~1

# HEAD 移动到"最近一次提交点"(实际上没动), staged,
# working directory和 HEAD 保持同步.
git reset --hard HEAD

# HEAD 移动到"上一次提交点", staged, working directory和 HEAD 保持同步.
git reset --hard HEAD~1

# HEAD 移动到"上一次提交点", staged保持不变.
git reset --soft HEAD~1

command for file

1
2
# HEAD 不发生任何移动, 但是会将 HEAD~1中的file1复制到staged中.
git reset HEAD~1 file1

Log

git log

Intro

获取项目的提交历史, 可以列出, 过滤, 搜索指定的修改, 仅仅对提交历史进行上述操作.

git log

Command

1
2
3
4
5
6
7
git log                查看所有commit object id(SHA-1, author、email、time)
git log -p files 显示每次提交的差异内容
git log -p -2 files 最近的两次提交
git log --stat 查看每次提交的statistic统计信息
git log --pretty 指定不同于默认格式的方式展示提交历史
git log --pretty=format:"%h - %an, %ar : %s" 自定义格式展示
git log --pretty=format:"%h %s" --graph 图形化显示