IT박스

오래된 Git 커밋을 수정하는 방법?

itboxs 2020. 6. 16. 20:40
반응형

오래된 Git 커밋을 수정하는 방법? [복제]


이 질문에는 이미 답변이 있습니다.

나는 3 개의 자식 커밋을 만들었지 만 밀리지 않았다. 가장 오래된 것 (ddc6859af44)과 가장 최근의 것 (47175e84c)을 어떻게 수정합니까?

$git log
commit f4074f289b8a49250b15a4f25ca4b46017454781
Date:   Tue Jan 10 10:57:27 2012 -0800

commit ddc6859af448b8fd2e86dd0437c47b6014380a7f
Date:   Mon Jan 9 16:29:30 2012 -0800

commit 47175e84c2cb7e47520f7dde824718eae3624550
Date:   Mon Jan 9 13:13:22 2012 -0800

git rebase -i HEAD^^^

이제 수정하려는 항목을 표시 edit하거나 e(바꾸기 pick). 이제 저장하고 종료하십시오.

이제 변경 한 다음

git add -A
git commit --amend --no-edit
git rebase --continue

추가 삭제를 추가하려면 commit 명령에서 옵션을 제거하십시오. 메시지를 조정하려면 --no-edit옵션 만 생략하십시오 .


나는 오래된 커밋으로 수정하고 싶다는 커밋을 준비했으며 rebase -i가 커밋되지 않은 변경 사항이 있다고 불평하는 것을보고 놀랐습니다. 그러나 이전 커밋의 편집 옵션을 지정하여 다시 변경하고 싶지 않았습니다. 따라서 솔루션은 매우 쉽고 간단했습니다.

  1. 이전 커밋에 대한 업데이트 준비, 추가 및 커밋
  2. git rebase -i <commit you want to amend>^- ^텍스트 편집기에서 해당 커밋을 볼 수 있습니다.
  3. 당신은 이런 식으로 얻을 것입니다 :

    pick 8c83e24 use substitution instead of separate subsystems file to avoid jgroups.xml and jgroups-e2.xml going out of sync
    pick 799ce28 generate ec2 configuration out of subsystems-ha.xml and subsystems-full-ha.xml to avoid discrepancies
    pick e23d23a fix indentation of jgroups.xml
    
  4. 이제 e23d23a와 8c83e24를 결합하려면 라인 순서를 변경하고 다음과 같이 스쿼시를 사용할 수 있습니다.

    pick 8c83e24 use substitution instead of separate subsystems file to avoid jgroups.xml and jgroups-e2.xml going out of sync    
    squash e23d23a fix indentation of jgroups.xml
    pick 799ce28 generate ec2 configuration out of subsystems-ha.xml and subsystems-full-ha.xml to avoid discrepancies
    
  5. 파일을 작성하고 종료하면 커밋 메시지를 병합하는 편집기가 나타납니다. 그렇게하고 텍스트 문서를 저장 / 나가십시오

  6. 완료, 커밋 수정

크레딧은 다음으로갑니다 : http://git-scm.com/book/en/Git-Tools-Rewriting-History 또한 유용한 다른 유용한 git magic이 있습니다.


git rebase커밋 히스토리를 다시 쓰는 데 사용할 수 있습니다 . 이것은 변경 사항을 손상시킬 수 있으므로주의해서 사용하십시오.

먼저 "수정"변경 사항을 일반 커밋으로 커밋하십시오. 그런 다음 가장 오래된 커밋의 부모에서 시작하여 대화식 리베이스를 수행하십시오.

git rebase -i 47175e84c2cb7e47520f7dde824718eae3624550^

이것은 모든 커밋으로 편집기를 시작합니다. "수정"커밋이 수정하려는 커밋 아래에 오도록 순서를 변경하십시오. 그런 다음에 커밋 "수정"로 라인에서 첫 번째 단어를 대체 s(결합되는 S 는 이전에 커밋 파기을). 편집기를 저장하고 종료하고 지시 사항을 따르십시오.


몇 번 다른 방법을 사용했습니다. 실제로, 이것은 수동 git rebase -i이며 일부 커밋을 스쿼시하거나 나누는 것을 포함하여 여러 커밋을 재정렬하려는 경우 유용합니다. 주요 이점은 모든 커밋의 운명을 한 순간에 결정할 필요가 없다는 것입니다. 또한 리베이스와 달리 프로세스 중에 모든 Git 기능을 사용할 수 있습니다. 예를 들어 언제든지 원본과 재기록 된 기록의 로그를 표시하거나 다른 리베이스를 수행 할 수도 있습니다!

다음과 같은 방법으로 커밋을 참조하므로 쉽게 읽을 수 있습니다.

C # good commit after a bad one
B # bad commit
A # good commit before a bad one

처음에 당신의 역사는 다음과 같습니다

x - A - B - C
|           |
|           master
|
origin/master

우리는 이것을 다음과 같이 다시 만들 것입니다 :

x - A - B*- C'
|           |
|           master
|
origin/master

순서

git checkout B       # get working-tree to the state of commit B
git reset --soft A   # tell Git that we are working before commit B
git checkout -b rewrite-history   # switch to a new branch for alternative history

개선 이전 사용하여 커밋 git add( git add -i, git stash지금 등). 이전 커밋을 둘 이상으로 나눌 수도 있습니다.

git commit           # recreate commit B (result = B*)
git cherry-pick C    # copy C to our new branch (result = C')

중간 결과 :

x - A - B - C 
|    \      |
|     \     master
|      \
|       B*- C'
|           |
|           rewrite-history
|
origin/master

마치자 :

git checkout master
git reset --hard rewrite-history  # make this branch master

That's it, you can push your progress now.


You can use git rebase --interactive, using the edit command on the commit you want to amend.


In case the OP wants to squash the 2 commits specified into 1, here is an alternate way to do it without rebasing

git checkout HEAD^               # go to the first commit you want squashed
git reset --soft HEAD^           # go to the second one but keep the tree and index the same
git commit --amend -C HEAD@{1}   # use the message from first commit (omit this to change)
git checkout HEAD@{3} -- .       # get the tree from the commit you did not want to touch
git add -A                       # add everything
git commit -C HEAD@{3}           # commit again using the message from that commit

The @{N) syntax is handy to know as it will allow you to reference the history of where your references were. In this case it's HEAD which represents your current commit.

참고URL : https://stackoverflow.com/questions/8824971/how-to-amend-older-git-commit

반응형