Programming

커밋의 파일 변경 사항 되돌리기

procodes 2020. 8. 27. 22:31
반응형

커밋의 파일 변경 사항 되돌리기


특정 커밋에 의한 변경 사항을 주어진 파일로만 되돌리고 싶습니다.

이를 위해 git revert 명령을 사용할 수 있습니까?

다른 간단한 방법이 있습니까?


내가 본 가장 깨끗한 방법은 여기에 설명되어 있습니다.

git show some_commit_sha1 -- some_file.c | git apply -R

VonC의 응답 git show유사하지만 git apply.


커밋 기록을 변경해도 좋다고 가정하면 다음은 이전 커밋에서 단일 파일의 변경 사항을 되 돌리는 워크 플로입니다.

예를 들어 badfile.txtcommit 에서 1 개 파일 ( )의 변경 사항을 되돌리려 고합니다 aaa222.

aaa333 Good commit
aaa222 Problem commit containing badfile.txt
aaa111 Base commit

기본 커밋을 기반으로 문제 커밋을 수정하고 계속하십시오.

1) 대화 형 리베이스 시작 :

git rebase -i aaa111

2) 문제를 마크 변경하여 편집기에서 편집 커밋 picke(편집 용) :

e aaa222
pick aaa333

3) 변경 사항을 잘못된 파일로 되돌립니다.

git show -- badfile.txt | git apply -R

4) 변경 사항 추가 및 커밋 수정 :

git add badfile.txt
git commit --amend

5) 리베이스 완료 :

git rebase --continue

git revert 커밋 내의 모든 파일 내용을위한 것입니다.

단일 파일의 경우 다음과 같이 스크립팅 할 수 있습니다 .

#!/bin/bash

function output_help {
    echo "usage: git-revert-single-file <sha1> <file>"
}

sha1=$1
file=$2

if [[ $sha1 ]]; then
git diff $sha1..$sha1^ -- $file | patch -p1
else
output_help
fi

( smtlaissezfairegit-shell-scripts 유틸리티에서 )


노트 :

현재 수정 사항을 아직 커밋하지 않은 경우 여기에 다른 방법이 설명되어 있습니다 .

git checkout -- filename

git checkout 파일에 대한 몇 가지 옵션이 있으며 HEAD에서 파일을 수정하고 변경 사항을 덮어 씁니다.


Dropped.on.Caprica댓글에서 언급합니다 .

git에 별칭을 추가하여 git revert-file <hash> <file-loc>특정 파일을 되돌릴 수 있습니다. 이 요점을
참조하십시오 .

[alias]
  revert-file = !sh /home/some-user/git-file-revert.sh

I would simply use the --no-commit option to git-revert and then remove the files you don't want reverted from the index before finally committing it. Here's an example showing how to easily revert just the changes to foo.c in the second most recent commit:

$ git revert --no-commit HEAD~1
$ git reset HEAD
$ git add foo.c
$ git commit -m "Reverting recent change to foo.c"
$ git reset --hard HEAD

The first git-reset "unstages" all files, so that we can then add back just the one file we want reverted. The final git-reset --hard gets rid of the remaining file reverts that we don't want to keep.


Much simpler:

git reset HEAD^ path/to/file/to/revert

then

git commit --amend   

and then

git push -f

the file is gone and commit hash, message, etc is the same.


git reset HEAD^ path/to/file/to/revert/in/commit

The above command will take file out of commit, but it will reflect in git status.

git checkout path/to/file/to/revert/in/commit

The above command will revert the changes (as a result you get file same as HEAD).

git commit

(Pass --amend to amend commit.)

git push

With this, the file which is already in the commit is removed and reverted.

The above steps should be followed from the the branch where the commit is made.


You can follow this procedure:

  1. git revert -n <*commit*> (-n revert all the changes but won't commit them)
  2. git add <*filename*> (name of the file/s you want to revert & commit)
  3. git commit -m 'reverted message' (add a message for reverting)
  4. after committing discard the other files changes so the files stay updated with the changes you committed before the revert

참고URL : https://stackoverflow.com/questions/2620775/revert-changes-to-a-file-in-a-commit

반응형