sourcecode

Git에서 SHA 해시에 의한 커밋으로 되돌리시겠습니까?

codebag 2023. 4. 13. 20:49
반응형

Git에서 SHA 해시에 의한 커밋으로 되돌리시겠습니까?

어떻게 하는지는 잘 모르겠다git revert예를 들어 머리 뒤에 있는 커밋6 커밋으로 되돌리고 중간 커밋의 모든 변경을 되돌립니다.

SHA 해시는56e05fced214c44a37759efa2dfc25a65d8ae98d그럼 왜 난 그냥 이렇게 할 수 없는거야?

git revert 56e05fced214c44a37759efa2dfc25a65d8ae98d

다른 커밋으로 현재 HEAD 위에 정확한 상태를 커밋하고 모든 중간 커밋을 취소하려면 다음 명령을 사용합니다.reset올바른 인덱스 상태를 생성하여 커밋합니다.

# Reset the index and working tree to the desired tree
# Ensure you have no uncommitted changes that you want to keep
git reset --hard 56e05fced

# Move the branch pointer back to the previous HEAD
git reset --soft "HEAD@{1}"

git commit -m "Revert to 56e05fced"

git-revert가 하는 일은 특정 커밋에서 이루어진 변경을 되돌리는 커밋을 작성하는 것입니다.이 커밋은 특정 커밋의 역방향(역방향)입니다.그러므로

git revert <SHA-1>

효과가 있습니다.

지정된 커밋으로 되돌아가고 싶은 경우, 그리고 이력이 아직 공개되지 않았기 때문에 이 작업을 수행할 수 있는 경우 git-revert가 아닌 git-reset을 사용해야 합니다.

git reset --hard <SHA-1>

(주의:--hard작업 디렉토리에서 수정되지 않은 모든 변경 내용이 손실됩니다).

기타 주의사항

그건 그렇고, 분명하진 않겠지만 서류상으로는 어디든지<commit>또는<commit-ish>(또는<object>커밋의 SHA-1 식별자(풀 또는 단축)를 입력할 수 있습니다.

상기 커밋을 되돌립니다.즉, 커밋의 반대편에 커밋이 추가됩니다.이전 버전을 체크 아웃하려면 다음 작업을 수행합니다.

git checkout 56e05fced214c44a37759efa2dfc25a65d8ae98d

특정 커밋으로 롤백하는 최선의 방법은 다음과 같습니다.

git reset --hard <commit-id>

그 후, 다음과 같이 입력합니다.

git push <reponame> -f

변경이 이미 퍼블릭 공유 리모트에 푸시되어 있는 경우, 그 사이에 있는 모든 커밋을 되돌리려면HEAD그리고.<sha-id>다음으로 커밋 범위를 전달할 수 있습니다.git revert,

git revert 56e05f..HEAD

그리고 그 사이에 있는 모든 커밋을 되돌립니다.56e05f그리고.HEAD(레인지의 시작점은 제외),56e05f).

갱신일 :

그 사이에 머지 커밋이 없는 경우, 이 답변은 보다 간단한 방법을 제공합니다.https://stackoverflow.com/a/21718540/541862

그러나 하나 이상의 병합 커밋이 있을 경우 해당 답변은 작동하지 않으므로 이 항목(모든 경우에 해당)을 사용하십시오.

원답:

# Create a backup of master branch
git branch backup_master

# Point master to '56e05fce' and
# make working directory the same with '56e05fce'
git reset --hard 56e05fce

# Point master back to 'backup_master' and
# leave working directory the same with '56e05fce'.
git reset --soft backup_master

# Now working directory is the same '56e05fce' and
# master points to the original revision. Then we create a commit.
git commit -a -m "Revert to 56e05fce"

# Delete unused branch
git branch -d backup_master

2개의 명령어는git reset --hard그리고.git reset --soft여기 마법이 있어요.첫 번째는 작업 디렉토리를 변경하지만 헤드(현재 브랜치)도 변경합니다.두 번째로 머리를 고정합니다.

이것은 보다 이해하기 쉽습니다.

git checkout 56e05fced -- .
git add .
git commit -m 'Revert to 56e05fced'

효과가 있었음을 증명하기 위해:

git diff 56e05fced

다음과 같이 단순해야 합니다.

git reset --hard 56e05f

그러면 특정 시점으로 돌아갈 수 있습니다.

다음과 같이 동작할 수 있습니다.

git checkout 56e05f
echo ref: refs/heads/master > .git/HEAD
git commit

언급URL : https://stackoverflow.com/questions/1895059/revert-to-a-commit-by-a-sha-hash-in-git

반응형