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

时间:2018-03-21 14:56:20

标签: regex git

好的......做出git checkout -p HEAD的提示......

  

从索引和工作树[y,n,q,a,d,/,j,J,g,e,?]中丢弃此块?

?显示帮助,/是......

  

/ - 搜索与给定正则表达式匹配的大块

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

如果我做一个git diff,我可以看到工作区中的几个文件和帅哥。 (包括sillyString一个)。

如何使用/选项查找我的sillyString大块。

当我输入/时,它会提示

  

搜索正则表达式?

我已经尝试了几种选择,但它仍然提供了目前正在使用的大块。

我应该输入什么作为我的正则表达式字符串来搜索大块中的sillyString

我目前拥有的git差异

我已将其匿名删除,但它显示了我想要尝试实现的重要内容。

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!"  
+
 }

从差异你可以看到我在一个文件中移动了一些代码(该文件中有相同代码的添加和删减)。然后我添加了我想要搜索的示例块。

1 个答案:

答案 0 :(得分: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'

所以现在我结帐了。它显示了第一个大块头,我搜索“傻”,它显示了第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,?]?

如果你一遍又一遍地获得相同的大块头,这可能意味着你的正则表达式也匹配那个大块头。

相关问题