Programming

이미 분할 된 덩어리를 git으로 나눌 수 있습니까?

procodes 2020. 5. 19. 20:48
반응형

이미 분할 된 덩어리를 git으로 나눌 수 있습니까?


최근 patchadd명령 에 대한 git의 옵션을 발견 했으며 실제로 환상적인 기능이라고 말해야합니다. 또한 s를 쳐서 큰 덩어리를 작은 덩어리로 나눌 수 있다는 것을 발견했습니다 . 커밋의 정밀도가 높아집니다. 그러나 분할 덩어리가 충분히 작지 않으면 더 정밀하게하려면 어떻게해야합니까?

예를 들어, 이미 분할 된 덩어리를 고려하십시오.

@@ -34,12 +34,7 @@
   width: 440px;
 }

-/*#field_teacher_id {
-  display: block;
-} */
-
-form.table-form #field_teacher + label,
-form.table-form #field_producer_distributor + label {
+#user-register form.table-form .field-type-checkbox label {
   width: 300px;
 }

다음 커밋에만 CSS 주석 제거를 추가하려면 어떻게해야합니까? s옵션은 더 이상 사용할 수 없습니다!


를 사용 git add -p하고로 분할 한 후에도 s약간의 변경이 없으면 e패치를 직접 편집하는 데 사용할 수 있습니다.

약간 혼란 스러울 수 있지만, 누른 열리는 편집기 창의 지시 주의 해서 따르면 e괜찮을 것입니다. 인용 한 경우 -다음 줄의 시작 부분에서 공백을 공백으로 바꿉니다.

-
-form.table-form #field_teacher + label,
-form.table-form #field_producer_distributor + label {

... 다음 줄, 즉로 시작하는 줄을 삭제하십시오 +. 그런 다음 편집기를 저장하고 종료하면 CSS 주석 제거 만 준비됩니다.


example.css다음과 같은 모습을 가정 해 봅시다 .

.classname {
  width: 440px;
}

/*#field_teacher_id {
  display: block;
} */

form.table-form #field_teacher + label,
form.table-form #field_producer_distributor + label {
  width: 300px;
}

.another {
  width: 420px;
}

이제 중간 블록에서 스타일 선택기를 변경해 보겠습니다. 이제 주석이 달린 스타일을 삭제하면 더 이상 필요하지 않습니다.

.classname {
  width: 440px;
}

#user-register form.table-form .field-type-checkbox label {
  width: 300px;
}

.another {
  width: 420px;
}

그것은 쉬웠다, 이제 커밋하자. 그러나 잠깐, 간단한 단계적 코드 검토를 위해 버전 제어 변경 사항을 논리적으로 분리하고 팀과 커밋 내역을 쉽게 검색 할 수 있기를 원합니다.

기존 코드 삭제는 다른 스타일 선택기 변경과 논리적으로 분리되어 있습니다. 우리는 두 개의 별개의 커밋이 필요하므로 패치를 위해 덩어리를 추가합시다.

git add --patch
diff --git a/example.css b/example.css
index 426449d..50ecff9 100644
--- a/example.css
+++ b/example.css
@@ -2,12 +2,7 @@
   width: 440px;
 }

-/*#field_teacher_id {
-  display: block;
-} */
-
-form.table-form #field_teacher + label,
-form.table-form #field_producer_distributor + label {
+#user-register form.table-form .field-type-checkbox label {
   width: 300px;
 }

Stage this hunk [y,n,q,a,d,/,e,?]?

혹시, 변경 사항이 너무 가깝기 때문에 git이 변경했습니다.

스플릿이 정밀한 변경을 위해 충분히 세분화되지 않았기 때문에 프레스 를 통해 스플릿을 시도해도 s동일한 결과를 얻습니다. git이 패치를 자동으로 분할하려면 변경된 줄 사이에 변경되지 않은 줄이 필요 합니다.

을 눌러 수동으로 편집 하겠습니다e

Stage this hunk [y,n,q,a,d,/,e,?]? e

git will open the patch in our editor of choice.

# Manual hunk edit mode -- see bottom for a quick guide
@@ -2,12 +2,7 @@
   width: 440px;
 }

-/*#field_teacher_id {
-  display: block;
-} */
-
-form.table-form #field_teacher + label,
-form.table-form #field_producer_distributor + label {
+#user-register form.table-form .field-type-checkbox label {
   width: 300px;
 }

# ---
# To remove '-' lines, make them ' ' lines (context).
# To remove '+' lines, delete them.
# Lines starting with # will be removed.
#
# If the patch applies cleanly, the edited hunk will immediately be
# marked for staging. If it does not apply cleanly, you will be given
# an opportunity to edit again. If all lines of the hunk are removed,
# then the edit is aborted and the hunk is left unchanged.

Let's review the goal:

How can I add the CSS comment removal only to the next commit ?

We want to split this into two commits:

  1. The first commit involves deleting some lines (comment removal).

    To remove the commented lines, just leave them alone, they are already marked to track the deletions in version control just like we want.

    -/*#field_teacher_id {
    - display: block;
    -} */

  2. The second commit is a change, which is tracked by recording both deletions and additions:

    • Deletions (old selector lines removed)

      To keep the old selector lines (do not delete them during this commit), we want...

      To remove '-' lines, make them ' '

      ...which literally means replacing the minus - signs with a space character.

      So these three lines...

      -
      -form.table-form #field_teacher + label,
      -form.table-form #field_producer_distributor + label {

      ...will become (notice the single space at the first of all 3 lines):


      form.table-form #field_teacher + label,
      form.table-form #field_producer_distributor + label {

    • Additions (new selector line added)

      To not pay attention to the new selector line added during this commit, we want...

      To remove '+' lines, delete them.

      ...which literally means to delete the whole line:

      +#user-register form.table-form .field-type-checkbox label {

      (Bonus: If you happen to be using vim as your editor, press dd to delete a line. Nano users press Ctrl+K)

Your editor should look like this when you save:

# Manual hunk edit mode -- see bottom for a quick guide
@@ -2,12 +2,7 @@
   width: 440px;
 }

-/*#field_teacher_id {
-  display: block;
-} */

 form.table-form #field_teacher + label,
 form.table-form #field_producer_distributor + label {
   width: 300px;
 }

# ---
# To remove '-' lines, make them ' ' lines (context).
# To remove '+' lines, delete them.
# Lines starting with # will be removed.
#
# If the patch applies cleanly, the edited hunk will immediately be
# marked for staging. If it does not apply cleanly, you will be given
# an opportunity to edit again. If all lines of the hunk are removed,
# then the edit is aborted and the hunk is left unchanged.

Now let's commit.

git commit -m "remove old code"

And just to make sure, let's see the changes from the last commit.

git show
commit 572ecbc7beecca495c8965ce54fbccabdd085112
Author: Jeff Puckett <jeff@jeffpuckett.com>
Date:   Sat Jun 11 17:06:48 2016 -0500

    remove old code

diff --git a/example.css b/example.css
index 426449d..d04c832 100644
--- a/example.css
+++ b/example.css
@@ -2,9 +2,6 @@
   width: 440px;
 }

-/*#field_teacher_id {
-  display: block;
-} */

 form.table-form #field_teacher + label,
 form.table-form #field_producer_distributor + label {

Perfect - you can see that only the deletions were included in that atomic commit. Now let's finish the job and commit the rest.

git add .
git commit -m "change selectors"
git show
commit 83ec3c16b73bca799e4ed525148cf303e0bd39f9
Author: Jeff Puckett <jeff@jeffpuckett.com>
Date:   Sat Jun 11 17:09:12 2016 -0500

    change selectors

diff --git a/example.css b/example.css
index d04c832..50ecff9 100644
--- a/example.css
+++ b/example.css
@@ -2,9 +2,7 @@
   width: 440px;
 }

-
-form.table-form #field_teacher + label,
-form.table-form #field_producer_distributor + label {
+#user-register form.table-form .field-type-checkbox label {
   width: 300px;
 }

Finally you can see the last commit only includes the selector changes.


If you can use git gui, it allows you to stage changes line by line. Unfortunately, I don't know how to do it from the command line - or even if it is possible.

One other option I've used in the past is rolling back part of the change (keep the editor open), commit the bits I want, undo and re-save from the editor. Not very elegant, but gets the job done. :)


EDIT (git-gui usage):

I am not sure if the git-gui is the same in msysgit and linux versions, I've only used the msysgit one. But assuming it is the same, when you run it, there are four panes: top-left pane is your working directory changes, bottom-left is your stages changes, top-right is the diff for the selected file (be it working dir or staged), and bottom right is for description of the commit (I suspect you won't need it). When you click a file in the top-right one, you will see the diff. If you right-click on a diff line, you'll see a context menu. The two options to note are "stage hunk for commit" and "stage line for commit". You keep selecting "stage line for commit" on the lines you want to commit, and you are done. You can even select several lines and stage them if you want. You can always click the file in the staging box to see what you are bout to commit.

As for committing, you can use either the gui tool or the command line.


One way to do it is to skip the chunk, git add whatever else you need, and then run git add again. If this is the only chunk, you'll be able to split it.

If you're worried about the order of commits, just use git rebase -i.

참고URL : https://stackoverflow.com/questions/6276752/can-i-split-an-already-split-hunk-with-git

반응형