Programming

마지막 git add를 어떻게 취소 할 수 있습니까?

procodes 2020. 5. 16. 11:24
반응형

마지막 git add를 어떻게 취소 할 수 있습니까?


그것은 변화 (커밋되지 않은) 개최 지난 unstage하는 것이 가능 자식을 ? 현재 브랜치에 파일이 많았고 일부는 준비되었지만 일부는 아니라고 가정합니다. 어떤 시점에서 어리석은 프로그래머가 실수로 다음을 실행했습니다.

git add -- .

...대신에:

git checkout -- .

이 프로그래머는 이제 마법의 git 명령으로 마지막 변경 사항을 취소 할 수 있습니까 ? 아니면 처음 실험하기 전에 커밋 했어야합니까?


사용할 수 있습니다 git reset. 마지막 커밋 후에 추가 한 모든 파일을 '무대 해제'합니다.

일부 파일 만 스테이지 해제하려면을 사용하십시오 git reset -- <file 1> <file 2> <file n>.

또한을 사용하여 파일의 일부 변경 내용을 스테이지 해제 할 수 있습니다 git reset -p.


당신은 취소 할 수 없습니다 최신 자식 추가,하지만 당신은 모든 취소 할 수 있습니다 add마지막 커밋 이후들. git reset커밋 인수가 없으면 인덱스가 재설정됩니다 (단계별 스테이지 변경 사항).

git reset

따라서 실제 답변

이 프로그래머는 이제 마법의 git 명령으로 마지막 변경 사항을 취소 할 수 있습니까?

실제로 : 아니요, 마지막 단계 만 스테이지 해제 할 수 없습니다 git add.

우리가 다음 상황에서와 같이 질문을 해석하는 경우입니다.

초기 파일 :

void foo() {

}

main() {
    foo();
}

첫 번째 변경 다음에 git add:

void foo(int bar) {
    print("$bar");
}

main() {
    foo(1337);
}

두 번째 변화는 다음과 git add같습니다.

void foo(int bar, String baz) {
    print("$bar $baz");
}

main() {
    foo(1337, "h4x0r");
}

이 경우 git reset -p가장 작은 단위가 선이므로 도움이되지 않습니다. git다음의 중간 상태에 대해 알지 못합니다.

void foo(int bar) {
    print("$bar");
}

main() {
    foo(1337);
}

더 이상.


사용할 수 있습니다 git reset( docs 참조 )


날짜 git 프롬프트에서 :

  1. use "git rm --cached <file>..." to unstage if files were not in the repo. It unstages the files keeping them there.
  2. use "git reset HEAD <file>..." to unstage if the files were in the repo, and you are adding them as modified. It keeps the files as they are, and unstages them.

At my knowledge you cannot undo the git add -- but you can unstage a list of files as mentioned above.


  • Remove the file from the index, but keep it versioned and left with uncommitted changes in working copy:

    git reset head <file>
    
  • Reset the file to the last state from HEAD, undoing changes and removing them from the index:

    git reset HEAD <file>
    git checkout <file>
    
    # If you have a `<branch>` named like `<file>`, use:
    git checkout -- <file>
    

    This is needed since git reset --hard HEAD won't work with single files.

  • Remove <file> from index and versioning, keeping the un-versioned file with changes in working copy:

    git rm --cached <file>
    
  • Remove <file> from working copy and versioning completely:

    git rm <file>
    

If you want to remove all added files from git for commit

git reset

If you want to remove an individual file

git rm <file>

Depending on size and scale of the difficultly, you could create a scratch (temporary) branch and commit the current work there.

Then switch to and checkout your original branch, and pick the appropriate files from the scratch commit.

At least you would have a permanent record of the current and previous states to work from (until you delete that scratch branch).

참고URL : https://stackoverflow.com/questions/12132272/how-can-you-undo-the-last-git-add

반응형