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

本节所在位置:

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

11.8 更多别名设置

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

# 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)

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

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

# 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)

实测效果:
git graph

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

# 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

实测效果:

git unmerged

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

# 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)

# 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

实测效果:

git s - with status & branch info

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

# 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(-)

实测结果:
git l1

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

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

实测结果:

git l5

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

# 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

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

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

# 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)

# 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

实测结果:

git details

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

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

# 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

实测效果:
git root

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

# 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:(线上版)

# 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

实测效果:

git abandon

11.9 交互式新增提交

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

# 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,纯命令行也可以实现该功能。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安冬的码畜日常

您的鼓励是我持续优质内容的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值