program story

이미 원격 브랜치에 푸시 된 병합 커밋을 되 돌리는 방법은 무엇입니까?

inputbox 2020. 9. 29. 07:44
반응형

이미 원격 브랜치에 푸시 된 병합 커밋을 되 돌리는 방법은 무엇입니까?


git revert <commit_hash>혼자는 작동하지 않습니다. -m지정해야하며, 그것에 대해 꽤 혼란 스럽습니다.

누구도 전에 이것을 경험 했습니까?


-m옵션은 상위 번호를 지정합니다 . 이는 병합 커밋에 둘 이상의 부모가 있고 Git은 어떤 부모가 메인 라인인지, 어떤 부모가 병합을 해제하려는 브랜치인지 자동으로 알지 못하기 때문입니다.

의 출력에서 ​​병합 커밋을 보면 git log다음으로 시작하는 줄에 부모가 나열되어있는 것을 볼 수 있습니다 Merge.

commit 8f937c683929b08379097828c8a04350b9b8e183
Merge: 8989ee0 7c6b236
Author: Ben James <ben@example.com>
Date:   Wed Aug 17 22:49:41 2011 +0100

Merge branch 'gh-pages'

Conflicts:
    README

이 상황에서은 ( git revert 8f937c6 -m 1는)에있는 그대로 트리를 가져 8989ee0오고에 git revert -m 2있는 그대로 트리를 복원합니다 7c6b236.

상위 ID를 더 잘 이해하려면 다음을 실행할 수 있습니다.

git log 8989ee0 

git log 7c6b236

누군가에게 도움이되기를 바라는 완전한 예가 있습니다.

git revert -m 1 <commit-hash> 
git commit -m "Reverting the last commit which messed the repo."
git push -u origin master

어디 <commit-hash>를 이용해서 복귀하고 싶다고 병합의 해시를 저지하고,의 설명에 명시된 바와 같이 이 응답 , -m 1당신이 병합하기 전에 먼저 부모의 트리로 복귀하고 싶은 것을 나타냅니다.

git commit ...세 번째 줄은 원격 지사에 밀어 변경 사항을 공개하게하면서 라인은 본질적으로 변경 사항을 커밋합니다.


벤은 어떻게 병합 커밋 되 돌리는 방법을 말했지만, 그건 아주 중요한 당신이 "이렇게하면 당신이 나무가 병합에 의해에서 가져온 변경됩니다 싶지는 않을 것이라고 선언 실현. 그 결과, 나중에 병합은 트리에 나타납니다이 도입 변경됩니다 이전에 되 돌린 병합의 조상이 아닌 커밋입니다. 이것은 원하는 것일 수도 있고 아닐 수도 있습니다. " (git-merge man page) .

기사 / 메일 링리스트 메시지 man 페이지에서 링크가 포함되는 메커니즘과 고려 사항에 대해 자세하게 설명합니다. 병합 커밋을 되 돌리면 나중에 분기를 다시 병합하고 동일한 변경 사항이 다시 나타날 것으로 기대할 수 없다는 것을 이해해야합니다.


다음 단계에 따라 잘못된 커밋을 되돌 리거나 원격 분기를 다시 올바른 HEAD / 상태로 재설정 할 수 있습니다.

  1. 원격 지점을 로컬 저장소로 체크 아웃하십시오.
    git checkout development
  2. git log에서 커밋 해시 (즉, 잘못된 커밋 직전 커밋의 ID)를 복사합니다. git log -n5

    산출:

    commit 7cd42475d6f95f5896b6f02e902efab0b70e8038 " 'wrong-commit'브랜치를 'development'에 병합"
    commit f9a734f8f44b0b37ccea769b9a2fd774c0f0c012 "이것은 잘못된 커밋입니다."
    commit 3779ab50e72908da92d2cfcd72256d7a09f446ba50e72908da92d2cfcd72256d

  3. 분기를 이전 단계에서 복사 한 커밋 해시로 재설정
    git reset <commit-hash> (i.e. 3779ab50e72908da92d2cfcd72256d7a09f446ba)

  4. (가) 실행 git status잘못된 커밋을의 일부 모든 변경 사항을 표시합니다.
  5. git reset --hard모든 변경 사항을 되돌리려면 실행하십시오 .
  6. force-push your local branch to remote and notice that your commit history is clean as it was before it got polluted.
    git push -f origin development

git revert -m 1 <merge-commit>

To keep the log clean as nothing happened (with some downsides with this approach (due to push -f)):

git checkout <branch>
git reset --hard <commit-hash-before-merge>
git push -f origin HEAD:<remote-branch>

'commit-hash-before-merge' comes from the log (git log) after merge.


Sometimes the most effective way to rollback is to step back and replace.

git log

Use the 2nd commit hash (full hash, the one you want to revert back to, before the mistake listed) and then rebranch from there.

git checkout -b newbranch <HASH>

Then delete the old branch, copy the newbranch over in its place and restart from there.

git branch -D oldbranch
git checkout -b oldbranch newbranch

If its been broadcast, then delete the old branch from all repositories, push the redone branch to the most central, and pull it back down to all.


If you want to revert a merge commit, here is what you have to do.

  1. First, check the git log to find your merge commit's id. You'll also find multiple parent ids associated with the merge (see image below).

enter image description here

Note down the merge commit id shown in yellow. The parent IDs are the ones written in the next line as Merge: parent1 parent2. Now...

Short Story:

  1. Switch to branch on which the merge was made. Then Just do the git revert <merge commit id> -m 1 which will open a vi console for entering commit message. Write, save, exit, done!

Long story:

  1. Switch to branch on which the merge was made. In my case, it is the test branch and I'm trying to remove the feature/analytics-v3 branch from it.

  2. git revert is the command which reverts any commit. But there is a nasty trick when reverting a merge commit. You need to enter the -m flag otherwise it will fail. From here on, you need to decide whether you want to revert your branch and make it look like exactly it was on parent1 or parent2 via:

git revert <merge commit id> -m 1 (reverts to parent2)

git revert <merge commit id> -m 2 (reverts to parent1)

You can git log these parents to figure out which way you want to go and that's the root of all the confusion.


I found creating a reverse patch between two know end-points and applying that patch would work. This presumes that you have created snapshots (tags) off of your master branch or even a back up of your master branch say master_bk_01012017.

Say the code branch you merged into master was mycodebranch.

  1. Checkout master.
  2. Create a full binary reverse patch between master and your backup. git diff --binary master..master_bk_01012017 > ~/myrevert.patch
  3. Check your patch git apply --check myrevert.patch
  4. Apply patch with sign-off git am --signoff < myrevert.patch
  5. If you will need to bring in this code again once it is fixed, you will need to branch off the reverted master and checkout the fix branch git branch mycodebranch_fix git checkout mycodebranch_fix
  6. Here you need to find the SHA key for the revert and revert the revert git revert [SHA]
  7. Now you can use your mycodebranch_fix to fix the issues, commit and re-merge into master once done.

The correctly marked answer worked for me but I had to spend some time to determine whats going on.. So I decided to add an answer with simple straightforward steps for cases like mine..

Lets say we got branches A and B.. You merged branch A into branch B and pushed branch B to itself so now the merge is part of it.. But you want to go back to the last commit before the merge.. What do you do?

  1. Go to your git root folder (the project folder usually) and use git log
  2. You will see the history of recent commits - the commits have commit/author/date properties while the merges also have a merge property - so you see them like this:

    commit: <commitHash> Merge: <parentHashA> <parentHashB> Author: <author> Date: <date>

  3. Use git log <parentHashA> and git log <parentHashB> - you will see the commit histories of those parent branches - the first commits in the list are the latest ones

  4. Take the <commitHash> of the commit you want, go to your git root folder and use git checkout -b <newBranchName> <commitHash> - that will create a new branch starting from that last commit you've chosen before the merge.. Voila, ready!

git doc about git revert -m provide a link exactly explain this: https://github.com/git/git/blob/master/Documentation/howto/revert-a-faulty-merge.txt


All the answers already covered most of the things but I will add my 5 cents. In short revering a merge commit is quite simple:

git revert -m 1 <commit-hash>

If you have permission you can push it directly to the "master" branch otherwise simply push it to your "revert" branch and create pull request.

You might find more useful info on this subject here: https://itcodehub.blogspot.com/2019/06/how-to-revert-merge-in-git.html


As Ryan mentioned, git revert could make merging difficult down the road, so git revert may not be what you want. I found that using the git reset --hard <commit-hash-prior-to-merge> command to be more useful here.

Once you have done the hard reset part, you can then force push to the remote branch, i.e. git push -f <remote-name> <remote-branch-name>, where <remote-name> is often named origin. From that point you can re-merge if you'd like.

참고URL : https://stackoverflow.com/questions/7099833/how-to-revert-a-merge-commit-thats-already-pushed-to-remote-branch

반응형