etc./StackOverFlow

Git에서 한 파일의 작업 복사본 수정을 취소하시겠습니까?

청렴결백한 만능 재주꾼 2021. 12. 31. 03:33
반응형

질문자 :hasen


마지막 커밋 후 작업 복사본에서 많은 파일을 수정했지만 가장 최근 커밋과 동일한 상태로 재설정하는 것처럼 해당 파일 중 하나에 대한 변경 사항을 취소하고 싶습니다.

그러나 나는 그 파일 하나만으로 작업 복사본 변경 사항을 취소하고 싶습니다.

어떻게 해야 하나요?



당신이 사용할 수있는

 git checkout -- file

-- (nimrodm에서 제안한 대로) 없이도 할 수 있지만 파일 이름이 분기나 태그(또는 다른 개정 식별자)처럼 보이면 혼동될 수 있으므로 -- 사용하는 것이 가장 좋습니다.

파일의 특정 버전을 체크아웃할 수도 있습니다.

 git checkout v1.2.3 -- file # tag v1.2.3 git checkout stable -- file # stable branch git checkout origin/master -- file # upstream master git checkout HEAD -- file # the version from the most recent commit git checkout HEAD^ -- file # the version before the most recent commit

Brian Campbell

그냥 사용

 git checkout filename

이렇게 하면 파일 이름이 현재 분기의 최신 버전으로 바뀝니다.

경고: 변경 사항이 삭제되며 백업이 유지되지 않습니다.


nimrodm

git checkout <commit> <filename>

drupal 6.10으로 업그레이드할 때 내 favicon이 몇 가지 커밋을 덮어썼다는 것을 깨달았기 때문에 오늘 이것을 사용했습니다. 그래서 다시 가져와야 했습니다. 내가 한 일은 다음과 같습니다.

 git checkout 088ecd favicon.ico

neoneye

파일이 이미 준비된 경우(파일 편집 후 git add 등을 수행할 때 발생) 변경 사항을 취소합니다.

사용하다

 git reset HEAD <file>

그 다음에

 git checkout <file>

아직 준비되지 않은 경우 다음을 사용하십시오.

 git checkout <file>

thanikkal

한 파일에 대한 이전 커밋의 변경 사항을 취소하려면 다음을 시도하십시오.

 git checkout branchname^ filename

이것은 마지막 커밋 이전과 같이 파일을 체크아웃합니다. 몇 번 더 커밋을 되돌리고 싶다면 branchname~n 표기법을 사용하세요.


sykora

git bash를 통해 완료했습니다.

(use "git checkout -- <file>..." to discard changes in working directory)

  1. 힘내 상태. [그래서 우리는 하나의 파일 뭉치가 수정된 것을 보았습니다.]
  2. git checkout -- index.html [index.html 파일에서 변경했습니다.
  3. git status [이제 변경 사항이 제거되었습니다]

여기에 이미지 설명 입력


Shivanandam

나는 항상 이것과 혼동하기 때문에 여기에 알림 테스트 케이스가 있습니다. git 을 테스트하기 위한 bash 스크립트가 있다고 가정해 보겠습니다.

 set -x rm -rf test mkdir test cd test git init git config user.name test git config user.email test@test.com echo 1 > a.txt echo 1 > b.txt git add * git commit -m "initial commit" echo 2 >> b.txt git add b.txt git commit -m "second commit" echo 3 >> b.txt

이 시점에서 변경 사항은 캐시에 준비되지 않으므로 git status 는 다음과 같습니다.

 $ git status On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: b.txt no changes added to commit (use "git add" and/or "git commit -a")

이 시점에서 git checkout 하면 결과는 다음과 같습니다.

 $ git checkout HEAD -- b.txt $ git status On branch master nothing to commit, working directory clean

대신 git reset 하면 결과는 다음과 같습니다.

 $ git reset HEAD -- b.txt Unstaged changes after reset: M b.txt $ git status On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: b.txt no changes added to commit (use "git add" and/or "git commit -a")

따라서 이 경우 변경 사항이 준비되지 않은 경우 git reset 은 차이가 없는 반면 git checkout 은 변경 사항을 덮어씁니다.


이제 위의 스크립트에서 마지막 변경 사항이 준비/캐시되었다고 가정해 보겠습니다. 즉, 끝에 git add b.txt

이 경우 git status 는 다음과 같습니다.

 $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: b.txt

이 시점에서 git checkout 하면 결과는 다음과 같습니다.

 $ git checkout HEAD -- b.txt $ git status On branch master nothing to commit, working directory clean

대신 git reset 하면 결과는 다음과 같습니다.

 $ git reset HEAD -- b.txt Unstaged changes after reset: M b.txt $ git status On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: b.txt no changes added to commit (use "git add" and/or "git commit -a")

따라서 이 경우 변경 사항이 단계적이면 git reset 은 기본적으로 단계적 변경 사항을 단계적 변경 사항으로 변경하지만 git checkout 은 변경 사항을 완전히 덮어씁니다.


sdaau

이 답변은 동일하거나 여러 폴더(또는 디렉토리)의 여러 특정 파일에 있는 로컬 변경 사항을 실행 취소하는 데 필요한 명령에 대한 것입니다. 이 답변은 사용자에게 둘 이상의 파일이 있지만 사용자가 모든 로컬 변경 사항을 실행 취소하기를 원하지 않는 질문을 구체적으로 다룹니다.

하나 이상의 파일이 있는 경우 다음과 같이 각 파일의 위치를 공백으로 구분하여 나열하여 각 git checkout -- file

 git checkout -- name1/name2/fileOne.ext nameA/subFolder/fileTwo.ext

위의 name1/name2/fileOne.ext nameA/subFolder/fileTwo.ext 사이의 공백을 염두에 두십시오.

동일한 폴더에 있는 여러 파일의 경우:

특정 디렉토리의 모든 파일에 대한 변경 사항을 취소해야 하는 경우 다음과 같이 git checkout을 사용하십시오.

 git checkout -- name1/name2/*

위의 별표는 name1/name2 아래 해당 위치의 모든 파일을 실행 취소하는 트릭을 수행합니다.

또한 다음과 유사하게 여러 폴더에 대한 모든 파일의 변경 사항을 취소할 수 있습니다.

 git checkout -- name1/name2/* nameA/subFolder/*

다시 위의 name1/name2/* nameA/subFolder/* 사이의 공백을 염두에 두십시오.

참고: name1, name2, nameA, subFolder - 이 모든 예제 폴더 이름은 해당 파일이 상주할 수 있는 폴더 또는 패키지를 나타냅니다.


Nirmal

SHA ID를 사용하여 파일을 복원합니다. 내가 하는 일은 git checkout <sha hash id> <file name>


Beto

나를 위해 이것 만 작동했습니다.

 git checkout -p filename

여기에 이미지 설명 입력


Ramesh Bhupathi

Git 2.23은 이러한 종류의 질문에 대한 답을 간단하게 만들기 위한 시도 restore

 git restore [--] <pathspec>...

언제나처럼 -- 가 필요할 수 있지만 파일 이름이 대시로 시작하는 경우. checkout restore 의 경계에는 분기가 포함되지 않기 때문에 분기 이름과 혼동할 수 없습니다.)

완료를 restore --staged 하여 준비된 파일을 복원하고 --source=<tree> HEAD 와 다른 커밋에서 복원할 수도 있습니다.


P-Gn

커밋을 아직 푸시하거나 공유하지 않은 경우:

 git diff --stat HEAD^...HEAD | \ fgrep filename_snippet_to_revert | cut -d' ' -f2 | xargs git checkout HEAD^ -- git commit -a --amend

Jesse Glick

이미 커밋된 경우 파일의 변경 사항을 되돌리고 다시 커밋한 다음 마지막 커밋으로 새 커밋을 스쿼시할 수 있습니다.


Gina

이유는 모르겠지만 코드를 입력하려고 하면 이미지로 나타납니다.

여기에 이미지 설명 입력


Gene

출처 : http:www.stackoverflow.com/questions/692246/undo-working-copy-modifications-of-one-file-in-git

반응형