xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

gif animation & git commands Diagram Tutorial All In One

gif animation & git commands Diagram Tutorial All In One

gif 动画图解 git 命令 All In One

  • git merge
  • git rebase
  • git reset
  • git revert
  • git cherry-pick
  • git fetch
  • git pull
  • git reflog

git merge

  1. fast-forward merge / 快速合并
    (--ff)

如果当前的所在的分支 master 上的 commit HEAD 在要合并的分支 dev 之前,而且 master 上没有做过额外的提交记录;
那么在分支 master 上合并分支 dev 的过程不会产生的新的提交记录,而是直接将分支 dev 上的提交添加进来,这称为 fast-forward 合并。

$ git checkout master
$ git pull

# 把 dev 分支合并到 master 分支中
$ git merge dev

image

步骤 图解
git merge
SourceTree 图解
  1. no-fast-foward merge / 非快速合并
    (--no-ff)

上面的 fast-foward 场景实际团队多人开发中很少会遇到,
如果是在当前分支 master 上创建了分支 dev 后,dev 分支提交了一些修改 commits, 同时 master 也上提交了一些修改 commits;
这个情况使用 git merge,就会触发 no-fast-forward 策略

在 no-fast-forward 策略下,Git 会在当前分支 master(active branch)上额外创建一个新的合并提交merging commit),
且这条提交记录既指向当前分支 master,又指向合并分支 dev

$ git branch
$ git checkout dev
$ git pull

$ git checkout master
$ git pull

# 把 dev 分支合并到 master 分支中
$ git merge dev

$ git commit
$ git push


image

步骤
图解
git merge 产生新的 merge commit,
同时触发 prepare-commit-msg 的 git hooks
gpsgit push -u origin
SourceTree 图解
  1. git merge conflicts / 合并冲突

如果是在当前分支 master 上创建了分支 dev 后,dev 分支提交了对某些文件的某些行的一些修改 commits, 同时 master 也对某些文件的某些行同一位置上提交了一些修改 commits;
这个情况使用就产生了 git merge conflicts,因此就需要先解决掉 conflicts, 才能进行合并

解决 git merge conflicts 时候, git 会在当前分支 master 上额外创建一个新的冲突合并提交(conflict merging commit),
且这条提交记录既指向当前分支 master,又指向合并分支 dev

$ git checkout master
$ git pull

# 把 dev 分支合并到 master 分支中,要先解决冲突
$ git merge dev

$ git commit
$ git push

步骤
图解
git merge 冲突
git merge 冲突位置
git merge 产生新的 conflict merge commit,
同时触发 prepare-commit-msg 的 git hooks
SourceTree 图解

git rebase

除了 git merge,还能使用 git rebase 来合并分支。

git rebase 指令会 复制 当前分支的所有最新提交,然后将这些提交添加到指定分支提交记录之上。

# 变基
$ git rebase master

image

使用 git rebase 在 git branch tree 上不会产生分叉 ✅

image

交互式变基

git rebase 时,我们还能对当前分支上的提交记录做修改!
采用交互式变基(Interactive Rebase) 形式。

变基时提供了 6 种操作模式:

● reword:改写,修改历史提交信息
● edit:编辑,修改历史提交
squash:压缩,把当前提交合并到之前的历史提交中
● fixup:将当前提交合并到之前的提交中,不保留提交的日志消息
● exec:在每一个需要变基的提交上执行一条命令
drop:丢弃, 删除历史提交

drop commit

# 删除 3  个 git 历史提交记录
$ git  rebase i HEAD~3
# 等价于 
# $ git  rebase i HEAD^2

# pick => drop

image

squash multiple commits

GitHub PR 合并多个git 历史提交记录

# 合并 3  个 git 历史提交记录
$ git  rebase i HEAD~3
# 等价于 
# $ git  rebase i HEAD^2
# pick => squash

image

want to squash multiple commits together to get a cleaner history

想要将多个提交压缩在一起以获得更清晰的历史记录

git reset

撤回提交, 回滚

git reset 可以控制当前分支回撤到某次提交时的状态。

  1. soft reset / 软重置

A soft reset moves HEAD to the specified commit (or the index of the commit compared to HEAD), without getting rid of the changes that were introduced on the commits afterward!

软重置将 HEAD 移动到指定的提交(或与 HEAD 相比的提交索引),而不会删除随后在提交中引入的更改!

$ git reset --sort HEAD~2

image

  1. hard reset / 硬重置 (⚠️ 危险操作

don't want to keep the changes that were introduced by certain commits.
Git should simply reset its state back to where it was on the specified commit:
this even includes the changes in your working directory and staged files! 💣

不想保留某些提交引入的更改。
Git 应该简单地将其状态重置回指定提交时的状态:
这甚至包括工作目录和暂存文件中的更改! 💣

$ git reset --hard HEAD~2

image

https://github.com/xgqfrms/git/issues/39

git revert

恢复, 还原, 撤销

还原某次提交的修改,会创建一个包含已还原更改的新提交记录!

git revert 可以在不修改分支历史的前提下,还原某次提交引入的更改。

$ git revert

image

https://github.com/xgqfrms/git/issues/40

git cherry-pick

挑樱桃, 摘樱桃

某个分支上的某些次提交的修改是当前分支需要的,可以使用 cherry-pick 命令挑选出某些次的提交更改作为新的提交添加到当前分支上面, 类似挑选樱桃一样

$ git cherry-pick

image

https://github.com/xgqfrms/git/issues/41

git fetch

将远程分支上的最新的修改下载到本地分支

$ git fetch origin master

image

git pull

git pull === git fetch + git merge

Although a git fetch is very useful in order to get the remote information of a branch, we can also perform a git pull.
A git pull is actually two commands in one: a git fetch, and a git merge.

虽然 git fetch 对于获取分支的远程信息非常有用,但我们也可以执行 git pull。
一个 git pull 实际上是两个命令合二为一:一个 git fetch 和一个 git merge。

$ git pull origin master

image

git reflog (🎉兜底保命操作)

Everyone makes mistakes, and that's totally okay! Sometimes it may feel like you've screwed up your git repo so badly that you just want to delete it entirely.

每个人都会犯错,这完全没关系!有时你可能会觉得你把你的 git repo 搞砸了,以至于你只想完全删除它。

git reflog is a very useful command in order to show a log of all the actions that have been taken!
This includes merges, resets, reverts: basically any alteration to your branch.

git reflog 是一个非常有用的命令,用于显示所有已采取的操作的日志!
这包括合并、重置、恢复:基本上是对分支的任何更改

$ git reflog

image

$ git reset

$ git reflog

image

git hooks

https://git-scm.com/book/zh/v2/自定义-Git-Git-钩子

git workflow

https://www.cnblogs.com/xgqfrms/p/11767393.html

Changing Multiple Commit Messages

$ git rebase -i HEAD~3
# 等价于
# $ git rebase -i HEAD^2

# 修改 commit
$ git commit --amend

$ git rebase --continue

image

https://www.cnblogs.com/xgqfrms/p/16465388.html

refs

https://www.cnblogs.com/xgqfrms/p/16822322.html#5116165

https://dev.to/lydiahallie/cs-visualized-useful-git-commands-37p1

https://zhuanlan.zhihu.com/p/129854679

https://www.yuque.com/zhangbao/weekly/day-80

visualize technical concepts / 可视化技术概念

https://dev.to/lydiahallie

https://github.com/lydiahallie



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2022-10-27 17:13  xgqfrms  阅读(88)  评论(6编辑  收藏  举报