your programing

공용 저장소에서 이전 Git 커밋으로 롤백

lovepro 2020. 9. 29. 08:19
반응형

공용 저장소에서 이전 Git 커밋으로 롤백


git의 특정 커밋으로 롤백하려면 어떻게해야합니까?

누군가 나에게 줄 수있는 가장 좋은 대답은 git revert내가 원하는 커밋에 도달 할 때까지 X 번 사용 하는 것입니다.

따라서 20 번의 커밋이 지난 커밋으로 되돌리고 싶다고 가정 해 봅시다. 20 번 실행해야합니다.

이 작업을 수행하는 더 쉬운 방법이 있습니까?

이 저장소는 공개되어 있으므로 재설정을 사용할 수 없습니다.


이 시도:

git checkout [revision] .

[revision]커밋 해시는 어디에 있습니까 (예 :) 12345678901234567890123456789012345678ab.

.아주 중요한 마지막 부분을 잊지 마세요 . 이것은 전체 트리에 변경 사항을 적용합니다. 이 명령은 git 프로젝트 루트에서 실행해야합니다. 하위 디렉토리에있는 경우이 명령은 현재 디렉토리의 파일 만 변경합니다. 그런 다음 커밋하면 잘해야합니다.

이 작업을 취소 할 수 있습니다.

git reset --hard 

작업 디렉토리 및 스테이징 영역에서 모든 수정 사항이 삭제됩니다.


특정 커밋으로 롤백하려면 :

git reset --hard commit_sha

10 개의 커밋을 롤백하려면 :

git reset --hard HEAD~10

기록을 다시 작성하지 않으려면 다음 게시물에서와 같이 "git revert"를 사용할 수 있습니다.

Git 저장소를 이전 커밋으로 되 돌리는 방법은 무엇입니까?


글쎄, 질문은 '롤백'이란 무엇을 의미합니까? reset공개되어 있기 때문에 할 수없고 커밋 기록을 그대로 유지하고 싶다면 작업 복사본이 특정 커밋을 반영하기를 원합니까? 사용 git checkout및 커밋 해시.

편집 : 주석에서 지적했듯이 git checkout분기를 지정하지 않고 사용 하면 "분기 없음"상태가됩니다. 사용 git checkout <commit> -b <branchname>지점에, 또는 체크 아웃에 git checkout <commit> .현재의 지점에 체크 아웃.


원래 포스터는 다음과 같이 말합니다.

누군가 나에게 줄 수있는 가장 좋은 대답은 git revert내가 원하는 커밋에 도달 할 때까지 X 번 사용하는 것이 었습니다 .

따라서 20 번의 커밋이 지난 커밋으로 되돌리고 싶다고 가정 해 봅시다. 20 번 실행해야합니다.

이 작업을 수행하는 더 쉬운 방법이 있습니까?

이 저장소가 공개되어 있기 때문에 재설정을 사용할 수 없습니다.

git revertX 번 사용할 필요는 없습니다 . git revert커밋 범위를 인수로 사용할 수 있으므로 한 번만 사용하면 커밋 범위를 되돌릴 수 있습니다. 예를 들어 지난 20 개의 커밋을 되돌리려면 다음을 수행하십시오.

git revert --no-edit HEAD~20..

커밋 범위 HEAD~20..는의 약어이며 HEAD~20..HEAD" HEAD 커밋 의 20 번째 부모 에서 시작하여 이후의 모든 커밋을 HEAD까지 되돌립니다"를 의미합니다.

병합 커밋이 없다고 가정 하고 마지막 20 개 커밋을 되돌 립니다. 병합 커밋이있는 경우 하나의 명령으로 모두 되돌릴 수 없으며 다음을 사용하여 개별적으로 되돌려 야합니다.

git revert -m 1 <merge-commit>

또한 git revertgit 버전 1.9.0 사용하여 범위를 사용하여 테스트했습니다 . 이전 버전의 git을 사용하는 경우 범위를 사용하면 git revert작동하거나 작동하지 않을 수 있습니다.

이 경우 git revertgit checkout.

참고는 달리 사용 말합니다이 답변git checkout , git revert실제로 당신이 복귀하고 있다는 커밋의에서 추가 된 파일을 제거합니다 이에게 개정의 범위를 되돌릴 수있는 올바른 방법을 만드는을.

선적 서류 비치


Step 1: fetch list of commits:

git log

You'll get list like in this example:

[Comp:Folder User$ git log
commit 54b11d42e12dc6e9f070a8b5095a4492216d5320
Author: author <author@gmail.com>
Date:   Fri Jul 8 23:42:22 2016 +0300

This is last commit message

commit fd6cb176297acca4dbc69d15d6b7f78a2463482f
Author: author <author@gmail.com>
Date:   Fri Jun 24 20:20:24 2016 +0300

This is previous commit message

commit ab0de062136da650ffc27cfb57febac8efb84b8d
Author: author <author@gmail.com>
Date:   Thu Jun 23 00:41:55 2016 +0300

This is previous previous commit message
...

Step 2: copy needed commit hash and paste it for checkout:

git checkout fd6cb176297acca4dbc69d15d6b7f78a2463482f

That's all.


git read-tree -um @ $commit_to_revert_to

will do it. It's "git checkout" but without updating HEAD.

You can achieve the same effect with

git checkout $commit_to_revert_to
git reset --soft @{1}

if you prefer stringing convenience commands together.

These leave you with your worktree and index in the desired state, you can just git commit to finish.


Want HEAD detached mode?

If you wish to rollback X time to a certain commit with a DETACHED HEAD (meaning you can't mess up anything), then by all means, use the following:

(replace X with how many commits you wish to go back)

git checkout HEAD~X

I.E. to go back one commit:

git checkout HEAD~1

I'm not sure what changed, but I am unable to checkout a specific commit without the option --detach. The full command that worked for me was: git checkout --detach [commit hash]

To get back from the detached state I had to checkout my local branch: git checkout master


Let's say you work on a project and after a day or so. You notice one feature is still giving you errors. But you do not know what change you made that caused the error. So you have to fish previous working commits. To revert to a specific commit:

git checkout 8a0fe5191b7dfc6a81833bfb61220d7204e6b0a9 .

Ok, so that commit works for you. No more error. You pinpointed the issue. Now you can go back to latest commit:

git checkout 792d9294f652d753514dc2033a04d742decb82a5 .

And checkout a specific file before it caused the error (in my case I use example Gemfile.lock):

git checkout 8a0fe5191b7dfc6a81833bfb61220d7204e6b0a9 -- /projects/myproject/Gemfile.lock

And this is one way to handle errors you created in commits without realizing the errors until later.


Here is an example to do that

    cd /yourprojects/project-acme 


    git checkout efc11170c78 .

You can find the commit id related to each commit in the commits section of GitHub/BitBucket/Gitlab. Its very simple, suppose your commit id is 5889575 then if you want to go back to this part in your code then you simply need to type

git checkout 5889575 .

This will take you to that point of time in your code.

참고URL : https://stackoverflow.com/questions/2007662/rollback-to-an-old-git-commit-in-a-public-repo

반응형