질문
실수로 잘못된 파일을 Git에 커밋했지만 아직 서버에 대한 커밋을 누르지 않았습니다.
로컬 저장소에서 커밋을 취소하려면 어떻게합니까?
유일한 방법은 일부 종류의 GUI 텍스트 편집기에서 편집을 복사 한 다음 전체 로컬 클론을 지우고 저장소를 다시 복제 한 다음 편집을 다시 적용하는 것으로 보입니다.하지만,
- This can cause data loss.
- It's very hard to do this when only an accidental
git commit
was run.
더 좋은 방법이 있습니까?
답변
커밋 & 다시 회원 실행 취소
$ git commit -m "Something terribly misguided" # (0: Your Accident)
$ git reset HEAD~ # (1)
[ edit files as necessary ] # (2)
$ git add . # (3)
$ git commit -c ORIG_HEAD # (4)
This command is responsible for the undo. It will undo your last commit while leaving your working tree (the state of your files on disk) untouched. You'll need to add them again before you can commit them again).
Make corrections to working tree files.
git add
anything that you want to include in your new commit.Commit the changes, reusing the old commit message.
reset
copied the old head to.git/ORIG_HEAD
;commit
with-c ORIG_HEAD
will open an editor, which initially contains the log message from the old commit and allows you to edit it. If you do not need to edit the message, you could use the-C
option.
또는 이전 커밋 (또는 그 커밋 메시지 만) 편집하려면 COMMIT - 암시가 현재 인덱스 내의 변경 사항을 이전 커밋에 추가합니다.
서버에 푸시 된 커밋을 제거 (되돌리지 마십시오), Git Push Origin 마스터 --force와의 기록을 다시 작성해야합니다.
추가 읽기
어떻게 앞에 머리를 이전 위치로 다시 이동할 수 있습니까?(분리 된 헤드) & 취소를 취소합니다
위의 답변을 보여줌으로써 재판장을 되돌릴 수있는 뉴 SHA-1을 결정하는 데 사용할 수 있습니다.이 값이 있으면 위에 설명 된대로 명령 시퀀스를 사용하십시오.
머리 ~은 머리 ~ 1과 같습니다.기사 Git의 머리는 무엇입니까?여러 commits를 돌보기를 원한다면 유용합니다.
출처:
https://stackoverflow.com/questions/927358/how-do-i-undo-the-most-recent-local-commits-in-git
최근댓글