숨겨진 커밋되지 않은 변경 사항을 복구하는 방법
내 개발 브랜치에 커밋되지 않은 변경 사항이 있었고를 사용하여 숨겼 git stash
지만 숨긴 항목 중에서 매우 중요한 변경 사항이있었습니다. 이러한 변경 사항을 되돌릴 수있는 방법이 있습니까?
또한 그 이후로 은닉 된 코드 파일 위에 몇 가지 변경 사항을 적용했습니다.
가능한 경우 새 분기에 숨겨진 변경 사항을 검색 할 수 있습니까?
쉬운 질문에 대한 쉬운 대답은 git stash apply
변경하려는 브랜치를 확인한 다음 git stash apply
. 그런 다음 사용 git diff
하여 결과를 확인합니다.
변경 사항을 모두 완료 한 후 ( apply
좋아 보이고 더 이상 은닉 할 필요가 없음을 확신합니다) 을 사용 git stash drop
하여 제거하십시오.
난 항상 사용하는 것이 좋습니다 git stash apply
보다는 git stash pop
. 차이점은을 ( apply
를) 쉽게 재 시도 apply
하거나 살펴 보기 위해 보관함 을 남겨 두는 것입니다. 보관함 pop
을 추출 할 수 있으면 즉시 보관 drop
되며 갑자기 어딘가에서 추출하고 싶다는 것을 알게되면 else (다른 지점에 있음), 또는 --index
, 또는 그와 함께하는 것은 그렇게 쉽지 않습니다. 당신이 경우 apply
, 당신은 경우에 선택할 수 drop
.
그러나 그것은 모두 매우 사소한 일이며 초보자가 git에 대해서는 거의 동일해야합니다. (그리고 나머지는 모두 건너 뛸 수 있습니다!)
좀 더 진보적이거나 복잡한 일을하고 있다면 어떨까요?
"git stash를 사용하는 방법"에는 적어도 서너 가지가 있습니다. 위는 "방법 1", "쉬운 방법"에 대한 것입니다.
깨끗한 브랜치로 시작하여 몇 가지 변경 작업을 수행 한 후 잘못된 브랜치에서 수행하고 있음을 깨달았습니다. 지금 가지고있는 변경 사항을 다른 분기로 "이동"하기 만하면됩니다.
이것은 위에서 설명한 쉬운 경우입니다. 실행
git stash save
(또는 일반git stash
, 동일한 것). 다른 지점을 확인하고git stash apply
. 이것은 git의 다소 강력한 병합 메커니즘을 사용하여 이전 변경 사항에 git을 병합합니다. 결과를주의 깊게 (사용하여git diff
) 검사하여 마음에 드는지 확인하고, 마음에 들면을 사용git stash drop
하여 보관함을 삭제하십시오. 끝났습니다!일부 변경을 시작하고 숨겼습니다. 그런 다음 다른 지점으로 전환하고 더 많은 변경을 시작했습니다.
이제 이러한 변경 사항 을 유지하거나 이동 하고 숨김도 적용 하고 싶습니다.
변경 사항의 "스택"을 만드는
git stash save
것처럼 실제로 다시 할 수 있습니다git stash
. 그렇게하면 두 개의 은닉처가 있는데 하나는 방금 불렀지stash
만stash@{0}
하나 는 쓸 수도 있고 다른 하나는 철자stash@{1}
.git stash list
(언제든지) 사용 하여 모두 볼 수 있습니다. 최신은 항상 가장 낮은 번호입니다. 당신이git stash drop
, 그것은 최신을 삭제stash@{1}
하고 스택의 맨 위로 이동합니다. 더 많이 가지고있는 경우에는이stash@{2}
됩니다stash@{1}
.당신은 할 수있는
apply
한 후drop
특정 숨김도 :git stash apply stash@{2}
, 등등. 특정 은닉처를 삭제하면 더 높은 번호의 은닉처 만 다시 번호가 매겨집니다. 다시 말하지만, 숫자가없는 것도stash@{0}
.은닉 물을 많이 쌓아두면 상당히 지저분해질 수 있습니다. (내가 원하던 은닉 물 이었는지
stash@{7}
아니면 그랬stash@{4}
습니까? 잠깐, 방금 다른 것을 눌렀는데 이제 8과 5가 되었습니까?). 나는 개인적으로 이러한 변경 사항을 새 브랜치로 옮기는 것을 선호합니다. 브랜치에는 이름이 있고cleanup-attempt-in-December
나에게는stash@{12}
. (이git stash
명령은 선택적인 저장 메시지를 사용하며 도움이 될 수 있지만 어떻게 든 내 모든 보관함은 이름이으로 표시WIP on branch
됩니다.)(초 고급) 실행하기 전에 코드의 특정 부분을 사용
git stash save -p
했거나 신중하게 수정git add
및 / 또는 수정했습니다 . 스 태쉬 된 인덱스 / 스테이징 영역에 하나의 버전이 있고 작업 트리에 다른 (다른) 버전이 있습니다. 이 모든 것을 보존하고 싶습니다. 이제를 사용 하고 때로는 다음과 같이 실패합니다.git rm
git stash save
git stash apply --index
Conflicts in index. Try without --index.
git stash save --keep-index
"무엇을 커밋할지"테스트하기 위해 사용 하고 있습니다. 이것은이 답변의 범위를 벗어납니다. 대신 이 다른 StackOverflow 답변을 참조하십시오 .
복잡한 경우에는 먼저 "깨끗한"작업 디렉토리에서 시작하는 것이 좋습니다 (원하는 경우 새 브랜치에서). 이렇게하면 적용하는 "어딘가"에 다른 항목이 없으며 숨겨진 변경 사항을 시도하게됩니다.
git status # see if there's anything you need to commit
# uh oh, there is - let's put it on a new temp branch
git checkout -b temp # create new temp branch to save stuff
git add ... # add (and/or remove) stuff as needed
git commit # save first set of changes
이제 "깨끗한"출발점에 있습니다. 또는 다음과 같이 진행될 수도 있습니다.
git status # see if there's anything you need to commit
# status says "nothing to commit"
git checkout -b temp # optional: create new branch for "apply"
git stash apply # apply stashed changes; see below about --index
The main thing to remember is that the "stash" is a commit, it's just a slightly "funny/weird" commit that's not "on a branch". The apply
operation looks at what the commit changed, and tries to repeat it wherever you are now. The stash will still be there (apply
keeps it around), so you can look at it more, or decide this was the wrong place to apply
it and try again differently, or whatever.
Any time you have a stash, you can use git stash show -p
to see a simplified version of what's in the stash. (This simplified version looks only at the "final work tree" changes, not the saved index changes that --index
restores separately.) The command git stash apply
, without --index
, just tries to make those same changes in your work-directory now.
This is true even if you already have some changes. The apply
command is happy to apply a stash to a modified working directory (or at least, to try to apply it). You can, for instance, do this:
git stash apply stash # apply top of stash stack
git stash apply stash@{1} # and mix in next stash stack entry too
You can choose the "apply" order here, picking out particular stashes to apply in a particular sequence. Note, however, that each time you're basically doing a "git merge", and as the merge documentation warns:
Running git merge with non-trivial uncommitted changes is discouraged: while possible, it may leave you in a state that is hard to back out of in the case of a conflict.
If you start with a clean directory and are just doing several git apply
operations, it's easy to back out: use git reset --hard
to get back to the clean state, and change your apply
operations. (That's why I recommend starting in a clean working directory first, for these complicated cases.)
What about the very worst possible case?
Let's say you're doing Lots Of Advanced Git Stuff, and you've made a stash, and want to git stash apply --index
, but it's no longer possible to apply the saved stash with --index
, because the branch has diverged too much since the time you saved it.
This is what git stash branch
is for.
If you:
- check out the exact commit you were on when you did the original
stash
, then - create a new branch, and finally
git stash apply --index
the attempt to re-create the changes definitely will work. This is what git stash branch newbranch
does. (And it then drops the stash since it was successfully applied.)
Some final words about --index
(what the heck is it?)
What the --index
does is simple to explain, but a bit complicated internally:
- When you have changes, you have to
git add
(or "stage") them beforecommit
ing. - Thus, when you ran
git stash
, you might have edited both filesfoo
andzorg
, but only staged one of those. - So when you ask to get the stash back, it might be nice if it
git add
s theadd
ed things and does notgit add
the non-added things. That is, if youadd
edfoo
but notzorg
back before you did thestash
, it might be nice to have that exact same setup. What was staged, should again be staged; what was modified but not staged, should again be modified but not staged.
The --index
flag to apply
tries to set things up this way. If your work-tree is clean, this usually just works. If your work-tree already has stuff add
ed, though, you can see how there might be some problems here. If you leave out --index
, the apply
operation does not attempt to preserve the whole staged/unstaged setup. Instead, it just invokes git's merge machinery, using the work-tree commit in the "stash bag". If you don't care about preserving staged/unstaged, leaving out --index
makes it a lot easier for git stash apply
to do its thing.
git stash pop
will get everything back in place
as suggested in the comments, you can use git stash branch newbranch
to apply the stash to a new branch, which is the same as running:
git checkout -b newbranch
git stash pop
To make this simple, you have two options to reapply your stash:
git stash pop
- Restore back to the saved state, but it deletes the stash from the temporary storage.git stash apply
- Restore back to the saved state and leaves the stash list for possible later reuse.
You can read in more detail about git stashes in this article.
To check your stash content :-
git stash list
apply a particular stash no from stash list:-
git stash apply stash@{2}
or for applying just the first stash:-
git stash pop
참고URL : https://stackoverflow.com/questions/19003009/how-to-recover-stashed-uncommitted-changes
'program tip' 카테고리의 다른 글
Python의 open ()은 파일이 없으면 생성하지 않습니다. (0) | 2020.10.03 |
---|---|
객체가 jQuery 객체인지 확인 (0) | 2020.10.03 |
모든 Git 태그를 나열하는 방법은 무엇입니까? (0) | 2020.10.03 |
특정 클래스 나 속성이없는 요소를 선택하는 CSS 선택기를 작성할 수 있습니까? (0) | 2020.10.03 |
두 JavaScript 객체의 동등성을 결정하는 방법은 무엇입니까? (0) | 2020.10.03 |