【Git 学习笔记_24】Git 使用冷门操作技巧(四)——更多实用 git 别名设置、交互式新增提交

文章目录

    • [11.8 更多别名设置](#11.8 更多别名设置)
      • [别名1:只查看当前分支(git b)](#别名1:只查看当前分支(git b))
      • [别名2:以图表形式显示自定义格式的 git 日志(git graph)](#别名2:以图表形式显示自定义格式的 git 日志(git graph))
      • [别名3:查看由于合并分支导致的冲突后仍有冲突的、待合并的文件列表(git unmerged)](#别名3:查看由于合并分支导致的冲突后仍有冲突的、待合并的文件列表(git unmerged))
      • [别名4:查看 git 状态(git st)](#别名4:查看 git 状态(git st))
      • [别名5:查看 git 简要状态(git s)](#别名5:查看 git 简要状态(git s))
      • [别名6:查看最新版本的统计信息(git l1)](#别名6:查看最新版本的统计信息(git l1))
      • [别名7:查看最近 5 个版本的提交详情(git l5)](#别名7:查看最近 5 个版本的提交详情(git l5))
      • [别名8:显示带统计信息的提交列表,并对文件变更信息彩色显示(git ll)](#别名8:显示带统计信息的提交列表,并对文件变更信息彩色显示(git ll))
      • [别名9:查看上游跟踪分支(git upstream)](#别名9:查看上游跟踪分支(git upstream))
      • [别名10:根据 ID/SHA-1 信息查看版本对象信息详情(git details)](#别名10:根据 ID/SHA-1 信息查看版本对象信息详情(git details))
      • [别名11:查看当前路径返回仓库根目录需要的上翻次数(git root)](#别名11:查看当前路径返回仓库根目录需要的上翻次数(git root))
      • [别名12:查看当前仓库根目录的文件绝对路径(git path)](#别名12:查看当前仓库根目录的文件绝对路径(git path))
      • [别名13:舍弃变更内容(git abandon)](#别名13:舍弃变更内容(git abandon))
    • [11.9 交互式新增提交](#11.9 交互式新增提交)

本节所在位置:

  • 活用 git stash(一)
  • 保存并应用 stash(一)
  • 用 git bisect 进行调试(二)
  • 使用 git blame 命令(二)
  • 设置彩色命令行界面(三)
  • 自动补全(三)
  • 让 Bash 自带状态信息(三)
  • 更多别名设置(四) ✔️
  • 交互式新增提交(四) ✔️
  • 忽略文件
  • 展示与清理忽略文件

11.8 更多别名设置

早在第二章就介绍过一些常见别名,本节将作进一步扩展,介绍更多实用的别名(共 13 个),以提高日常工作效率。还是以本章示例 git 库为例:

bash 复制代码
# repo init
$ git clone https://github.com/PacktPublishing/Git-Version-Control-Cookbook-Second-Edition_tips_and_tricks.git moreAlias
$ cd .\moreAlias
$ git checkout aliases

分别尝试如下别名:

别名1:只查看当前分支(git b)

bash 复制代码
# Demo1: show the current branch only as 'git b'
$ git config alias.b "rev-parse --abbrev-ref HEAD"
aliases

别名2:以图表形式显示自定义格式的 git 日志(git graph)

bash 复制代码
# Demo2: prettier git log as 'git graph'
$ git config alias.graph "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative"
$ git graph origin/conflict aliases
# ... (omitted)

实测效果:

别名3:查看由于合并分支导致的冲突后仍有冲突的、待合并的文件列表(git unmerged)

bash 复制代码
# Demo3: git unmerged
$ git config alias.unmerged '!git ls-files --unmerged | cut -f2 | sort -u'
# test by merging the origin/conflict branch
$ git merge origin/conflict
fatal: refusing to merge unrelated histories
$ git merge origin/conflict --allow-unrelated-histories
CONFLICT (add/add): Merge conflict in spaceship.txt
Auto-merging spaceship.txt
Automatic merge failed; fix conflicts and then commit the result.
$ git unmerged
spaceship.txt
# about merge
$ git merge --abort

实测效果:

别名4:查看 git 状态(git st)

bash 复制代码
# Demo4: shorten git status as 'git st'
$ git config alias.st "status"
$ git st
On branch aliases
Your branch is up to date with 'origin/aliases'.

nothing to commit, working tree clean

别名5:查看 git 简要状态(git s)

bash 复制代码
# Demo5: shorter status with branch and file information as 'git s'
$ git config alias.s 'status -sb'
# test
$ touch test
$ echo testing >> foo
$ git s
## aliases...origin/aliases
 M foo
?? test

实测效果:

别名6:查看最新版本的统计信息(git l1)

bash 复制代码
# Demo6: show the latest commit with some stats as 'git l1'
$ config alias.l1 "log -1 --shortstat"
$ git l1
commit 75e6d446289ac917aeb18f0f611bd0c91f4b7033 (HEAD -> aliases, origin/aliases)
Author: John Doe <john.doe@example.com>
Date:   Tue Jun 12 22:07:08 2018 +0200

    Better spaceship design

 1 file changed, 9 insertions(+), 9 deletions(-)

实测结果:

别名7:查看最近 5 个版本的提交详情(git l5)

bash 复制代码
# Demo7: list latest 5 commits with more info as 'git l5'
$ git config alias.l5 "log -5 --decorate --shortstat"
$ git l5

实测结果:

别名8:显示带统计信息的提交列表,并对文件变更信息彩色显示(git ll)

bash 复制代码
# Demo8: Show commit listing with statistics on the changed files in color, as 'git ll'
$ git config alias.ll "log --pretty=format:'%C(yellow)%h%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset %Cred%d%Creset' --numstat"
$ git ll -5

实测发现,定义别名时外层必须使用双引号,否则定义不成功(书中为单引号)。效果如下:

:

别名9:查看上游跟踪分支(git upstream)

bash 复制代码
# Demo9: show the upstream/tracking branch by 'git upstream'
$ git config alias.upstream "rev-parse --symbolic-full-name --abbrev-ref=strict HEAD@{u}"
$ git upstream
origin/aliases 

别名10:根据 ID/SHA-1 信息查看版本对象信息详情(git details)

bash 复制代码
# Demo10: show the details of ID/SHA-1 (commit, tag, tree, blob) as 'git details'
$ git config alias.details "cat-file -p"
$ git details HEAD

实测结果:

别名11:查看当前路径返回仓库根目录需要的上翻次数(git root)

接下来这个别名在命令行脚本中非常实用 git root

bash 复制代码
# Demo11: show the number of cd-ups or '../'s, needed to go to the repository root
# as 'git root'
$ git config alias.root "rev-parse --show-cdup"
$ cd sub/directory/example
$ pwd
$ git root
$ cd $(git root)
$ pwd

实测效果:

别名12:查看当前仓库根目录的文件绝对路径(git path)

bash 复制代码
# Demo12: View the path of the repository on the filesystem as 'git path'
$ git config alias.path "rev-parse --show-toplevel"
$ git path
C:/Users/z/Desktop/moreAlias

别名13:舍弃变更内容(git abandon)

最后,若想舍弃暂存区(索引区)、工作区、抑或是某 commit 的改动;再或者,在保留版本管理以外的变更的情况下,将工作区重置(reset)到某个状态(或 commit ID),只需要跟一个 ref 引用即可,比如 HEAD:(线上版)

bash 复制代码
# Demo13: abandon changes as 'git abandon'
$ git config alias.abandon "reset --hard"
$ echo "new stuff" >> foo
$ git add foo
$ echo "other stuff" >> bar
$ git s
## aliases...origin/aliases
 M bar
M  foo
?? test
$ git abandon HEAD
$ git s
## aliases...origin/aliases
?? test

实测效果:

11.9 交互式新增提交

如果需要从一个文件中分出多次修改分别提交,如拆分为 bug 修复、代码重构、特性开发等 commit,就可以用到本节演示的交互式提交功能:

bash 复制代码
# repo init
$ git clone https://github.com/PacktPublishing/Git-Version-Control-Cookbook-Second-Edition_tips_and_tricks.git interAdd
$ cd interAdd
$ git checkout interactive
$ git reset 'HEAD^'
# add interactively
$ git add -p liberty.txt

最后一行的 -p--patch 表示可以成片地、选择性地添加代码变更。此外还可以使用 -i 表示交互模式。

结果如下:

输入 ? 可查看各操作符含义:

根据提示,Git 切分出的一块代码变更叫做一个 hunk,键入 y 表示整个加入暂存区,也可以用 s 进行细分;最后可以键入 a 将后面的所有 hunk 都加到暂存区。

该功能类似 SourceTree 的按需提交或撤回某行,只是 SourceTree 的操作界面更友好。如果没有 SourceTree,纯命令行也可以实现该功能。

相关推荐
handsome2135 小时前
WSL中使用GPU加速AMBER MD--测试
笔记·学习
WZF-Sang6 小时前
Linux权限理解【Shell的理解】【linux权限的概念、管理、切换】【粘滞位理解】
linux·运维·服务器·开发语言·学习
狂飙的张兴发6 小时前
认知小文2《成功之路:习惯、学习与实践》
学习·考研·职场和发展·跳槽·学习方法·改行学it·高考
爱编程的小新☆6 小时前
C语言内存函数
c语言·开发语言·学习
夜清寒风7 小时前
opencv学习:图像掩码处理和直方图分析及完整代码
人工智能·opencv·学习·算法·机器学习·计算机视觉
吃着火锅x唱着歌8 小时前
Go语言设计与实现 学习笔记 第七章 内存管理(1)
笔记·学习·golang
~在杰难逃~8 小时前
关于订单信息的Excel数据分析报告
笔记·数据分析·excel·数据分析报告
我命由我123458 小时前
2.使用 VSCode 过程中的英语积累 - Edit 菜单(每一次重点积累 5 个单词)
前端·javascript·ide·vscode·学习·编辑器·学习方法
胆小鬼~9 小时前
【DAY20240918】03教你轻松配置 Git 远程仓库并高效推送代码!
git
Pluses9 小时前
Datawhale X 李宏毅苹果书 AI夏令营 《深度学习详解》第十九章 ChatGPT
人工智能·笔记·深度学习·学习