your programing

잘못된 Git 브랜치에 대한 커밋을 수정하는 방법은 무엇입니까?

lovepro 2020. 10. 3. 11:24
반응형

잘못된 Git 브랜치에 대한 커밋을 수정하는 방법은 무엇입니까?


방금 잘못된 지점에 대해 완벽하게 잘 수행했습니다. 마스터 브랜치에서 마지막 커밋을 실행 취소하고 동일한 변경 사항을 가져와 업그레이드 브랜치로 가져 오려면 어떻게해야합니까?


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

git reset --soft HEAD^

이것은 커밋을 되돌 리지만 커밋 된 변경 사항을 인덱스에 다시 넣습니다. 브랜치가 서로에 대해 상대적으로 최신 상태라고 가정하면 git은 다른 브랜치로 체크 아웃 할 수 있습니다. 그러면 간단히 커밋 할 수 있습니다.

git checkout branch
git commit

단점은 커밋 메시지를 다시 입력해야한다는 것입니다.


주제에 대해 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로컬 지점이 아닌 원격 하나입니다.


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

하나의 커밋을 롤백하려면 (다음 단계에서 커밋의 해시를 기록해야합니다) :

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 (Bash가 아닌 Windows 명령 줄 사용)에서는 실제로 1 개가 ^^^^아닌 4 개 이므로

git reset --hard HEAD^^^^

나는 최근에 다른 지점에 전념해야했을 때 실수로 마스터에게 변경을 저질렀던 동일한 일을했습니다. 그러나 나는 아무것도 밀지 않았다.

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

// 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을 수행 할 수 있습니다.

Also, if you ended up committing to the wrong branch, and also ended up write some more code before realizing that you committed to the wrong branch, then you could use git stash to save your in-progress work:

// 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

NOTE: I used this website as a reference https://www.clearvision-cm.com/blog/what-to-do-when-you-commit-to-the-wrong-git-branch/


So if your scenario is that you've committed to master but meant to commit to another-branch (which may or not may not already exist) but you haven't pushed yet, this is pretty easy to fix.

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

Now all your commits to master will be on another-branch.

Sourced with love from: http://haacked.com/archive/2015/06/29/git-migrate/


To elaborate on this answer, in case you have multiple commits to move from, e.g. develop to 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

If you run into this issue and you have Visual Studio, you can do the following:

Right-click on your branch and select View History:

enter image description here

Right-click on commit you want to go back to. And Revert or Reset as needed.

enter image description here


If the branch you wanted to apply your changes to already exists (branch develop, for example), follow the instructions that were provided by fotanus below, then:

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

And obviously you could use tempbranch or any other branch name instead of my_feature if you wanted.

Also, if applicable, delay the stash pop (apply) until after you've merged at your target branch.

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

반응형