Git checkout -p HEAD 正则表达式搜索

5

好的……执行git checkout -p HEAD命令后,会弹出提示:

Discard this hunk from index and worktree [y,n,q,a,d,/,j,J,g,e,]?

这里的?表示需要帮助,/表示……

/ - 查找与给定正则表达式匹配的hunk

所以,我想尝试一下。我在一个文件中添加了一行代码:let mySillyString = "Hello, world"

如果我执行git diff命令,就可以看到工作树中的多个文件和hunk(包括)。

如何使用/选项来查找我的 hunk呢?

当我输入/时,它会显示提示:

search for regex?

我尝试了几种选项,但它仍然只提供当前所在的hunk。

我应该输入什么作为我的正则表达式字符串,以便在hunk中搜索?

我目前的git diff

我已经匿名处理了它以删除一些内容,但它显示了我想要尝试实现的重要部分。

diff --git a/MyProject/AViewController.swift b/MyProject/AViewController.swift index 1b14a4522..3341eb355 100644
--- a/MyProject/AViewController.swift
+++ b/MyProject/AViewController.swift 

@@ -99,12 +99,6 @@ class AViewController: UIViewController, Str
         print("doing something here")
     }

-    func getImageURL() -> URL? {
-        print("doing something here")
-        print("doing something here")
-        return theImageURL
-    }
-
     func addPopGesture() {
         print("doing something here")

@@ -174,6 +168,12 @@ extension AViewController {

         stackView.addArrangedSubviews(views: someViews)
     }
+
+    private func getImageURL() -> URL? {
+        print("doing something here")
+        print("doing something here")
+        return theImageURL
+    }
 }

 extension AViewController { 

diff --git a/MyProject/AView.swift b/MyProject/AView.swift index b9e3349aa..962e255e2 100644
--- a/MyProject/AView.swift
+++ b/MyProject/AView.swift 

@@ -300,5 +300,7 @@ fileprivate extension UIImage {

         return someData
     }
+
+    let mySillyString = "Hello, world!"  
+
 }

从差异中可以看出,我已经将一个文件中的一些代码移动了(该文件中有相同代码的添加和减少)。然后我添加了我想要搜索的示例块。


我一直在研究这个问题,但还无法撰写完整的答案。为了提供有意义的示例,我认为有两个代码块会很有帮助。 - Code-Apprentice
@Code-Apprentice 好的...我会添加一个我拥有的git diff示例。 - Fogmeister
1个回答

4

简短版:

/silly 带你直接到想要添加的巨块。它只搜索与你当前所在的巨块同一文件中的巨块。

详细版:

我没有你的文件,所以我用我的一个文件作为示例。我添加了3条评论,使差异看起来像这样:

diff --git a/Gemfile b/Gemfile
index 751838d..626c553 100644
--- a/Gemfile
+++ b/Gemfile
@@ -5,6 +5,8 @@ git_source(:github) do |repo_name|
   "https://github.com/#{repo_name}.git"
 end

+# This is change 1
+
 # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
 gem 'rails', '~> 5.1.5'
 # Use sqlite3 as the database for Active Record
@@ -18,6 +20,8 @@ gem 'uglifier', '>= 1.3.0'
 # See https://github.com/rails/execjs#readme for more supported runtimes
 # gem 'therubyracer', platforms: :ruby

+# This is change 2
+
 gem 'coffee-rails', '~> 4.2'
 # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
 gem 'turbolinks', '~> 5'
@@ -39,6 +43,8 @@ group :development, :test do
   gem 'selenium-webdriver'
 end

+# This is a silly change
+
 group :development do
   # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
   gem 'web-console', '>= 3.3.0'

现在我要进行结账。它显示了第一个块,我搜索了“silly”,它显示了第3个块。我仍然需要回答是否要添加该块,这只是使在具有许多块的文件中移动更快的方式。

$ git checkout -p HEAD
diff --git a/Gemfile b/Gemfile
index 751838d..626c553 100644
--- a/Gemfile
+++ b/Gemfile
@@ -5,6 +5,8 @@ git_source(:github) do |repo_name|
   "https://github.com/#{repo_name}.git"
 end

+# This is change 1
+
 # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
 gem 'rails', '~> 5.1.5'
 # Use sqlite3 as the database for Active Record
Discard this hunk from index and worktree [y,n,q,a,d,/,j,J,g,e,?]? /silly
@@ -39,6 +43,8 @@ group :development, :test do
   gem 'selenium-webdriver'
 end

+# This is a silly change
+
 group :development do
   # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
   gem 'web-console', '>= 3.3.0'
Discard this hunk from index and worktree [y,n,q,a,d,/,k,K,g,e,?]?

如果您一遍又一遍地得到相同的代码块,那么很可能是您的正则表达式也匹配了该代码块。


好的...看了你的回答后,我有了一个领悟。你所有的更改都在同一个文件中。如果我搜索/private,那么它就会跳转到带有private func...的块。谢谢(我感觉文档有点误导,哈哈) - Fogmeister

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接