git 命令来比较 git status 输出的第 n 个修改文件

标签 git bash

我经常运行 git status然后想查看其中一个文件的具体差异。所以之后:

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:  path/to/modified/file1
    modified:  path/to/modified/file1

是否有可能使用 git 或作为后备 bash 到 git diff第 n 个修改的文件,因此 git diff <someMagic(2)>会减少到git diff path/to/modified/file2 .

我的主要问题是这是否存在于 git 中,如果不是为了帮助 bash 命令。

最佳答案

如果自动化不是您的目标,那么我会说 git add -i适合您的情况。

看看git add -i文档。

如果您正在寻找一个脚本,那么我已经做了一些完全符合您要求的事情:

#!/bin/bash
git diff $(git status | grep modified | sed 's/^.*:   //' | head -n "$1" | tail -n 1)

使用方法是调用它,然后传入要diff的修改文件的编号:<script_name> N ,其中 N 是“第 n 个修改的文件”的编号。

(例如,使用您的示例,如果我调用 <script>.sh 2,它会像这样调用 diff:git diff path/to/modified/file2)

关于git 命令来比较 git status 输出的第 n 个修改文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46305978/

相关文章:

bash - 在 bash 脚本中实现试运行

linux - 将plist中的字典解析为bash中的数组

Linux:从for中删除重复的字符串并剪切

git - 如何在 Heroku 上查看远程 Git 修订版

xcode - 尝试将 git 存储库克隆到 xcode 时卡在 checkout

git - 推 pull 的不同默认远程

c - bash readlline c 函数在命令行上插入

regex - posix 扩展正则表达式中的变量扩展

json - GitLab 5.2 后接收 WebHook

git - 使用 git-tfs bridge 的最佳实践和问题