Development Tip

파일 및 폴더를 포함하여 커밋되지 않은 변경 사항을 되 돌리는 방법은 무엇입니까?

yourdevel 2020. 9. 27. 14:05
반응형

파일 및 폴더를 포함하여 커밋되지 않은 변경 사항을 되 돌리는 방법은 무엇입니까?


작업 트리 및 색인에서 커밋되지 않은 모든 변경 사항을 되돌리고 새로 만든 파일과 폴더를 제거하는 git 명령이 있습니까?


다음 두 명령을 실행할 수 있습니다.

# Revert changes to modified files.
git reset --hard

# Remove all untracked files and directories. (`-f` is `force`, `-d` is `remove directories`)
git clean -fd

현재 작업 디렉토리에서만 변경 사항을 되돌리려면

git checkout -- .

그리고 그 전에 실제로 어떤 작업도 수행하지 않고 되돌릴 파일을 나열 할 수 있습니다.

git checkout --

"git checkout-..."을 사용하여 작업 디렉토리의 변경 사항을 삭제합니다.

git checkout -- app/views/posts/index.html.erb

또는

git checkout -- *

예를 들어 git 상태에서 준비되지 않은 파일에 대한 모든 변경 사항을 제거합니다.

modified:    app/controllers/posts.rb
modified:    app/views/posts/index.html.erb

사소하지 않은 한 가지 방법은 다음 두 명령을 실행하는 것입니다.

  1. git stash 그러면 변경 사항이 숨김으로 이동하여 HEAD 상태로 돌아갑니다.
  2. git stash drop 마지막 명령에서 생성 된 최신 숨김을 삭제합니다.

git clean -fd

도움이되지 않았고 새 파일이 남았습니다. 내가 한 일은 모든 작업 트리를 완전히 삭제 한 다음

git reset --hard

정리 옵션을 추가하는 방법에 대한 조언은 " git에서 내 로컬 작업 디렉토리를 지우려면 어떻게해야합니까? "를 참조하십시오 -x.

git clean -fdx

Note -x 플래그는 Git에서 무시한 모든 파일을 제거하므로주의하십시오 (제가 참조하는 답변의 토론 참조).


다음 명령을 사용할 수 있다고 생각합니다. git reset --hard


사라지지 않는 것처럼 보이는 파일이 여전히있을 수 있습니다. 편집되지 않은 파일 일 수 있지만 CRLF / LF 변경으로 인해 git이 파일을 편집중인 것으로 표시했을 수 있습니다. .gitattributes최근에 변경 사항이 있는지 확인하십시오 .

제 경우에는 CRLF 설정을 .gitattributes파일에 추가 했고 모든 파일은이 때문에 "수정 된 파일"목록에 남아 있습니다. .gitattributes 설정을 변경하면 사라졌습니다.


최근 커밋의 복사본으로 되돌리려는 커밋되지 않은 변경 사항 (작업 복사본에만 해당)이있는 경우 다음을 수행합니다.

git checkout filename

저장소에서 커밋되지 않은 모든 변경 사항을 되돌릴 수있는 다음 git 명령을 사용할 수 있습니다.

git checkout .

예:

ABC@ABC-PC MINGW64 /c/xampp/htdocs/pod_admin (master)
$ git status
On branch master
Your branch is up-to-date with 'origin/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:   application/controllers/Drivers.php
        modified:   application/views/drivers/add.php
        modified:   application/views/drivers/load_driver_info.php
        modified:   uploads/drivers/drivers.xlsx

no changes added to commit (use "git add" and/or "git commit -a")

ABC@ABC-PC MINGW64 /c/xampp/htdocs/pod_admin (master)
$ git checkout .

ABC@ABC-PC MINGW64 /c/xampp/htdocs/pod_admin (master)
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.

nothing to commit, working tree clean

Git 2.23은 git restore작업 트리 파일을 복원하는 명령을 도입했습니다 .

https://git-scm.com/docs/git-restore

현재 디렉터리의 모든 파일을 복원하려면

git restore.

색인의 버전과 일치하도록 모든 C 소스 파일을 복원하려면 다음을 수행 할 수 있습니다.

git restore '* .c'


안전하고 먼 길 :

  1. git branch todelete
  2. git checkout todelete
  3. git add .
  4. git commit -m "I did a bad thing, sorry"
  5. git checkout develop
  6. git branch -D todelete

나는 일반적으로 잘 작동하는 방법을 사용합니다.

mv fold/file /tmp
git checkout fold/file

사용하다:

git reset HEAD filepath

예를 들면 :

git reset HEAD om211/src/META-INF/persistence.xml

참고URL : https://stackoverflow.com/questions/5807137/how-to-revert-uncommitted-changes-including-files-and-folders

반응형