developer tip

푸시되지 않은 git 커밋 제거

optionbox 2020. 10. 3. 10:30
반응형

푸시되지 않은 git 커밋 제거


나는 git commit했지만 아직 저장소에 푸시하지 않았습니다. 그래서 내가 할 때 git status, 나는 '# Your branch is before the'master 'by 1 commit를 얻습니다.

따라서 최상위 커밋을 롤백하려면 다음을 수행하면됩니다.

git reset --hard eb27bf26dd18c5a34e0e82b929e0d74cfcaab316

git log내가 얻을 :

커밋 eb27bf26dd18c5a34e0e82b929e0d74cfcaab316
날짜 : 2009 년 9 월 29 일 화요일 11:21:41 -0700


커밋 db0c078d5286b837532ff5e276dcf91885df2296
날짜 : 2009 년 9 월 22 일 화요일 10:31:37 -0700

실제로를 사용할 때 git reset재설정 하려는 커밋을 참조해야 합니다 . 그래서 db0c078아마도 커밋 을 원할 것입니다.

더 쉬운 버전은 git reset --hard HEAD^현재 헤드 이전의 이전 커밋으로 재설정하는 것입니다 . 그렇게하면 커밋 ID를 복사 할 필요가 없습니다.

git reset --hard커밋되지 않은 변경 사항이 손실 될 수 있으므로을 수행 할 때주의하십시오 . 당신은 확인 할 수 git status있는지 당신의 작업 복사본이 깨끗한 지, 또는 당신이 거기있는 모든 변경 사항을 날려 싶지 않음을 확인합니다.

또한 origin/master주석에서 @bdonlan이 제안한대로 HEAD 대신 참조로 사용할 수 있습니다 .git reset --hard origin/master


변경 사항을 원격으로 푸시하지 않은 경우

git reset HEAD~1

작업 복사본이에서 깨끗한 지 확인하십시오 git status.

그렇지 않으면 변경 사항을 원격으로 푸시했습니다.

git revert HEAD

이 명령은 마지막 커밋 / 변경 사항을 되돌 리거나 제거합니다.


git reset --hard origin/master

원래 위치로 재설정합니다.

이것은 댓글 에 @bdonlan에 의해 게시되었습니다 . 댓글을 읽지 않는 사람들을 위해이 답변을 추가했습니다.


이 질문에는 두 가지 분기가 있습니다 (커밋을 롤백한다고해서 로컬 변경 사항을 모두 잃고 싶다는 의미는 아닙니다).

1. 최신 커밋을 되돌리고 커밋 된 파일의 변경 사항 을 취소하려면 다음을 수행하십시오.

git reset --hard HEAD~1

2. 최신 커밋을 되돌 리지만 로컬 변경 사항을 디스크에 유지하려면 다음을 수행하십시오.

git reset --soft HEAD~1

이 (나중의 명령) 당신이했을 경우의 상태로 당신을 데려 갈 것 git add입니다.

그 후에 파일을 언 스테이징하려면 다음을 수행하십시오.

git reset

이제 추가하고 다시 커밋하기 전에 더 많은 사항을 변경할 수 있습니다.


콘솔에 입력하기 만하면됩니다.

$ git reset HEAD~

이 명령은 원격 HEAD 앞의 모든 로컬 커밋을 삭제합니다.


푸시하기 전에 마지막 커밋 제거

git reset --soft HEAD~1

1두 개의 마지막 사용을 제거하려는 경우 마지막 커밋을 의미합니다. 2*


I have experienced the same situation I did the below as this much easier. By passing commit-Id you can reach to the particular commit you want to go:

git reset --hard {commit-id}

As you want to remove your last commit so you need to pass the commit-Id where you need to move your pointer:

git reset --hard db0c078d5286b837532ff5e276dcf91885df2296

This is what I do:

First checkout your branch (for my case master branch):

git checkout master

Then reset to remote HEAD^ (it'll remove all your local changes), force clean and pull:

git reset HEAD^ --hard && git clean -df && git pull

One way would be to delete the local branch and checkout that branch from the server if your local branch is ahead of remote by multiple commits and you need to uncommit all of them.


I believe that one of those will fit your need

1 - Undo commit and keep all files staged: git reset --soft HEAD~;

2 - Undo commit and unstage all files: git reset HEAD~;

3 - Undo the commit and completely remove all changes: git reset --hard HEAD~;

here is were I found the answer


I just had the same problem and ended up doing:

git rebase -i HEAD~N

(N is the number of commits git will show you)

That prompts your text editor and then you can remove the commit you want by deleting the line associated with it.

참고URL : https://stackoverflow.com/questions/1611215/remove-a-git-commit-which-has-not-been-pushed

반응형