prosource

잘못된 Git 브랜치에 대한 커밋을 수정하려면 어떻게 해야 합니까?

probook 2023. 4. 13. 20:57
반응형

잘못된 Git 브랜치에 대한 커밋을 수정하려면 어떻게 해야 합니까?

내가 잘못된 지사에 완전히 잘 헌신한 것뿐이야마스터 브랜치의 마지막 커밋을 취소하고 동일한 변경을 적용하여 업그레이드 브랜치로 가져오려면 어떻게 해야 합니까?

아직 변경을 푸시하지 않은 경우 소프트 리셋을 실행할 수도 있습니다.

git reset --soft HEAD^

이렇게 하면 커밋이 되돌아가지만 커밋된 변경 내용이 인덱스에 다시 저장됩니다.브런치가 서로 비교적 최신이라고 가정하면 git는 다른 브런치로 체크아웃을 할 수 있습니다.이것에 의해, 간단하게 다음과 같이 커밋할 수 있습니다.

git checkout branch
git commit -c ORIG_HEAD

-c ORIG_HEAD다시 하지 않는 합니다.part는 commit message를 다시 입력하지 않습니다.

4년 늦었지만 누군가에게 도움이 될 수도 있어요.

커밋하기 전에 새로운 브랜치를 작성하는 것을 잊어버리고 모든 것을 마스터로 커밋한 경우 커밋 수에 관계없이 다음 방법이 더 쉽습니다.

git stash                       # skip if all changes are committed
git branch my_feature
git reset --hard origin/master
git checkout my_feature
git stash pop                   # skip if all changes were committed

가 '마스터브런치'와 동등하게 .origin/master새로운 커밋은 모두 유효합니다.my_feature해 주세요.my_feature리모트 브런치가 아닌 로컬브런치입니다

깨끗한(수정되지 않은) 작업 복사본이 있는 경우

1개의 커밋을 롤백하려면(다음 단계에서 커밋의 해시를 메모해 둡니다).

git reset --hard HEAD^

이 커밋을 다른 브랜치로 끌어당기려면:

git checkout other-branch
git cherry-pick COMMIT-HASH

변경 내용을 수정하거나 추적하지 않은 경우

, 「 」, 「 」라고 하는 도 주의해 주세요.git reset --hard추적되지 않은 변경 및 변경된 변경을 모두 삭제합니다.따라서 변경사항이 있는 경우 다음 절차를 수행합니다.

git reset HEAD^
git checkout .

이미 변경을 푸시한 경우 HEAD를 리셋한 후 다음 푸시를 강제해야 합니다.

git reset --hard HEAD^
git merge COMMIT_SHA1
git push --force

경고: 하드 리셋을 실행하면 작업 복사본에서 커밋되지 않은 수정이 취소되고 강제 푸시는 원격 브랜치의 상태를 로컬 브랜치의 현재 상태로 완전히 덮어씁니다.

위해 Windows아닌 사용는 4개의 Windows(Bash)입니다.^^^^, '아, 아, 아, 아, 아, 아, 아, 아, 아, 아, 아, 아, 아, 아, 아, 아, 아...

git reset --hard HEAD^^^^

저도 최근에 같은 일을 했어요. 실수로 마스터로 변경했는데 다른 지사에 변경했어야 했어요.하지만 난 아무것도 강요하지 않았어.

잘못된 브랜치에 커밋했을 뿐 그 이후 아무것도 변경하지 않고 repo에 푸시하지 않은 경우 다음을 수행할 수 있습니다.

// rewind master to point to the commit just before your most recent commit.
// this takes all changes in your most recent commit, and turns them into unstaged changes. 
git reset HEAD~1 

// temporarily save your unstaged changes as a commit that's not attached to any branch using git stash
// all temporary commits created with git stash are put into a stack of temporary commits.
git stash

// create other-branch (if the other branch doesn't already exist)
git branch other-branch

// checkout the other branch you should have committed to.
git checkout other-branch

// take the temporary commit you created, and apply all of those changes to the new branch. 
//This also deletes the temporary commit from the stack of temp commits.
git stash pop

// add the changes you want with git add...

// re-commit your changes onto other-branch
git commit -m "some message..."

메모: 위의 예에서는 git reset HEAD~1로 커밋 1개를 되감고 있었습니다.단, n개의 커밋을 되감고 싶다면 git reset HEAD~n을 할 수 있습니다.

또한 잘못된 브랜치에 커밋하고 잘못된 브랜치에 커밋한 것을 깨닫기 전에 코드를 몇 개 더 작성하게 되면 git stash를 사용하여 진행 중인 작업을 저장할 수 있습니다.

// save the not-ready-to-commit work you're in the middle of
git stash 

// rewind n commits
git reset HEAD~n 

// stash the committed changes as a single temp commit onto the stack. 
git stash 

// create other-branch (if it doesn't already exist)
git branch other-branch

// checkout the other branch you should have committed to.
git checkout other-branch

// apply all the committed changes to the new branch
git stash pop

// add the changes you want with git add...

// re-commit your changes onto the new branch as a single commit.
git commit -m "some message..."

// pop the changes you were in the middle of and continue coding
git stash pop

메모: 이 웹사이트는 https://www.clearvision-cm.com/blog/what-to-do-when-you-commit-to-the-wrong-git-branch/ 참조용으로 사용하였습니다.

잘못된 브랜치에 여러 개의 커밋이 있는 경우

약 1회의 커밋으로 리셋이 가능한 다른 솔루션도 많이 있습니다.는 ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★master '이라고 부르자target커밋 이력을 잃고 싶지 않았습니다.

당신이 할 수 있는 일, 그리고 나를 구해준 것은 이 답변을 참고 자료로 사용한 입니다.이 답변은 4단계 프로세스입니다.

  1. Create a new temporary branch 새 임시 분기를 만듭니다.temp부에서master
  2. 지 병합temp into the branch originally intended for commits, i.e. 원래 커밋을 의도한 브랜치, 즉target
  3. Undo commits on 커밋 실행 취소일master
  4. Delete the temporary branch 임시 분기를 삭제합니다.temp.

자세한 절차는 다음과 같습니다.

  1. Create a new branch from the 에서 새 브랜치를 만듭니다.master 많은 (무심코 많은 변경을 가한 경우)

    git checkout -b temp
    

    ★★★★★★-b 때 합니다.
    요.git branch tempgit log츠키노

  2. 한 브런치( 커밋을 의도한 에 Marge .target.
    먼저 원래 분기로 전환합니다. target.git fetch하지 않은 )

    git checkout target
    

    :: " " " "-b
    임시 을 지금 해 보겠습니다.target

    git merge temp
    

    갈등이 있으면 여기서 해결해야 할 수도 있습니다.성공적으로 병합한 후 를 누르거나 다음 단계로 넘어갈 수 있습니다.

  3. master답변을 참조로 사용하여 먼저 로 전환합니다.master

    git checkout master
    

    다음 명령어를 사용하여 리모콘을 대조하기 위해 완전히 원래대로 되돌립니다(또는 필요에 따라 적절한 명령어를 사용하여 특정 커밋).

    git reset --hard origin/master
    

    다시 말씀드리지만git log의도한 변경사항이 적용되었는지 확인하기 위해 이전과 이후를 선택합니다.

  4. 증거를 지우는 것, 즉 임시 분기를 삭제하는 것입니다. 지사를 .temptarget((속속)을 있는 master하면 '하다'가 수 요.error: The branch 'temp' is not fully merged 한 번 해 볼까요?

    git checkout target
    

    그리고 나서 이 사고의 증거를 지우고

    git branch -d temp
    

'네'에 master 약속하는 하고 있습니다.another-branch(이미 존재하거나 존재하지 않을 수 있습니다) 하지만 아직 누르지 않으셨기 때문에 수정은 매우 간단합니다.

// if your branch doesn't exist, then add the -b argument 
git checkout -b another-branch
git branch --force master origin/master

으로, 「」에의 행해졌습니다.masteranother-branch.

출처: http://haacked.com/archive/2015/06/29/git-migrate/

이 답변에 대해 자세히 설명하자면, 예를 들어 여러 개의 커밋이 있는 경우입니다.develop로로 합니다.new_branch:

git checkout develop # You're probably there already
git reflog # Find LAST_GOOD, FIRST_NEW, LAST_NEW hashes
git checkout new_branch
git cherry-pick FIRST_NEW^..LAST_NEW # ^.. includes FIRST_NEW
git reflog # Confirm that your commits are safely home in their new branch!
git checkout develop
git reset --hard LAST_GOOD # develop is now back where it started

이 문제가 발생하고 Visual Studio가 있는 경우 다음을 수행할 수 있습니다.

하여 [ ]를 선택합니다.View History:

여기에 이미지 설명 입력

돌아갈 커밋을 오른쪽 클릭합니다.필요에 따라 되돌리거나 재설정합니다.

여기에 이미지 설명 입력

에서 인 경우masteryour feature다음 중 하나:

git checkout -B feature
git branch -f master origin/master

origin/master master가리킬 수 있습니다.를 들어, 「」를 합니다.HEAD~3의 을 3개의 로 포인트 HEAD , 「」a1b2c3d해당 해시가 있는 커밋을 가리키고 싶은 경우.

이 아이디어는 이 시스템을 재작성하는 것입니다.feature현재 커밋으로 분기하여 전환합니다. '우리'를 만들어 .master 합니다.origin/master.

일반적인 경우

에서 masterfeature하다

A---B---C---D  $old_master                   A---B---C---D  master
    |        \                                   |        \
    |         G---H---I  master <- HEAD  =>      |         G---H---I
    |                                            | 
    `-E---F  feature                             `-E---F---G'--H'--I' feature <- HEAD

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★masterfeature하다$old_mastermaster을 사용하다

git checkout feature
git cherry-pick $old_master..master
git branch -f master $old_master

에 따라서,의 변경 을 보존합니다.git stash --include-untracked에 풀어서 git stash pop.

이력을 다시 쓰지 않음

「」를 하는 에, 「」를 리셋 합니다.mastergit revertfeature★★★★★★ 。

git checkout feature
git cherry-pick $old_master..master
git checkout master
git revert $old_master..
git checkout feature

.git merge을 에 your your your your your your로 합니다.feature은, 「Marge」와「Marge」의 feature로 되돌아가다master왜냐면 우리가 그들을 되돌렸기 때문이야

rG,rH ★★★★★★★★★★★★★★★★★」rI (복귀 커밋)G,H ★★★★★★★★★★★★★★★★★」I하다

A---B---C---D             rG--rH--rI  master
    |        \           /
    |         G---H---I-`
    | 
    `-E---F---G'--H'--I' feature <- HEAD

제 경우, 이 문제는 제가 추진했던 약속을 되돌리고, 그 약속을 다른 지점으로 가져가는 것으로 해결되었습니다.

git checkout branch_that_had_the_commit_originally
git revert COMMIT-HASH
git checkout branch_that_was_supposed_to_have_the_commit
git cherry pick COMMIT-HASH

하시면 됩니다.git log을 사용법이러한 변경은 언제든지 적용할 수 있습니다.

변경을 적용하는 브런치가 이미 존재하는 경우(브런치 전개 등), 아래의 fotanus에서 제공하는 지시에 따릅니다.

git checkout develop
git rebase develop my_feature # applies changes to correct branch
git checkout develop # 'cuz rebasing will leave you on my_feature
git merge develop my_feature # will be a fast-forward
git branch -d my_feature

필요에 따라 my_feature 대신 tempbranch 등의 브랜치 이름을 사용할 수 있습니다.

또한 필요에 따라 타깃브런치에서 Marge할 때까지 stash pop(적용)을 연기합니다.

변경사항 유지 커밋 해제 간단한 해결책은 이 명령을 실행하는 입니다.

git reset HEAD^

이렇게 하면 변경 내용을 유지하면서 코드가 커밋 해제됩니다.

이를 위해 잘못된 지점에 6개 정도 커밋을 하고 눈치채기 전에 리모트에 밀어 넣었습니다.다음 단계를 수행했더니 도움이 되었습니다.

  1. Git 로그를 작성하여 커밋한 내용을 확인했습니다.

     git log --oneline
    
  2. 이전 커밋과 푸시 목록에서 헤드 즉, 리스트의 "head" 타이틀을 가진 첫 번째 커밋으로 하기 위해 필요한 커밋을 선택했습니다.이는 당신이 한 새로운 커밋의 전체 목록을 나타내기 때문입니다.아이디를 저장합니다![내 코드의 예](여기에서는 커밋 목록과 선택한 코드의 목록을 보여줍니다). 마지막 커밋의 선두였던 1위)

  3. 그리고 이 브랜치를 사용하기 전에 6개의 커밋으로 리셋합니다.

     git reset --hard HEAD~6
    
  4. 실수로 커밋한 커밋을 넣으려고 새로운 지점에 체크 아웃을 합니다.

     git checkout -b newbranch
    
  5. 사용하다

     git reset --hard < commit_id > 
    

커밋의 전체 목록을 새 브랜치로 가져옵니다.

  1. 그 후 사용

     git push --force 
    

리모트 리포트에도 이러한 변경을 가할 수 있습니다.

언급URL : https://stackoverflow.com/questions/2941517/how-to-fix-committing-to-the-wrong-git-branch

반응형