development

"git reset"과 "git checkout"의 차이점은 무엇입니까?

big-blog 2020. 2. 21. 22:29
반응형

"git reset"과 "git checkout"의 차이점은 무엇입니까?


난 항상 생각했습니다 git resetgit checkout모두가 특정 커밋을하는 프로젝트 등을 가지고 있다는 점에서, 동일로. 그러나 중복 될 수 있기 때문에 정확히 동일 할 수는 없다고 생각합니다. 이 둘의 실제 차이점은 무엇입니까? svn svn co은 커밋을 되돌려 야하기 때문에 약간 혼란 스럽습니다 .

추가

VonC과 찰스의 차이점을 설명 git reset하고 git checkout정말 잘합니다. 내 현재 이해는 git reset모든 변경 사항을 특정 커밋으로 되 돌리는 반면 git checkout지점을 준비하는 것은 다소 있습니다. 다음 두 다이어그램 이이 이해에 매우 유용하다는 것을 알았습니다.

http://a.imageshack.us/img651/1559/86421927.png http://a.imageshack.us/img801/1986/resetr.png

추가 3

에서 http://think-like-a-git.net/sections/rebase-from-the-ground-up/using-git-cherry-pick-to-simulate-git-rebase.html , 체크 아웃 및 재설정 할 수의 에뮬레이션 rebase.

여기에 이미지 설명을 입력하십시오

git checkout bar 
git reset --hard newbar 
git branch -d newbar 

여기에 이미지 설명을 입력하십시오


  • git reset특히 HEAD를 옮기고 인덱스업데이트하는 것에 관한 것 입니다.
  • git checkout작업 트리업데이트하는 것입니다 (인덱스 또는 지정된 트리로). 분기를 체크 아웃 할 때만 HEAD를 업데이트합니다 (그렇지 않으면 HEAD분리됩니다 ).
    (실제로 Git 2.23 Q3 2019에서는 git restore반드시 그런 것은 아닙니다. git checkout)

이에 비해 svn에는 인덱스가 없기 때문에 작업 트리 만 svn checkout지정된 개정을 별도의 디렉토리에 복사합니다.
더 가까운 git checkout것은 :

  • svn update (같은 지점에 있다면 같은 SVN URL을 의미)
  • svn switch (예를 들어 동일한 지점이지만 다른 SVN 저장소 URL에서 체크 아웃 한 경우)

그 모든 세 가지 작업 트리 수정 ( svn checkout, update, switch) 자식에 하나의 명령이 있습니다 git checkout.
그러나 git에는 인덱스 개념 (리포지토리와 작업 트리 사이의 "스테이징 영역")도 있으므로 git reset.


Thinkeye주석 에서 " Remy Demystified " 기사를 언급합니다 .

예를 들어, 다른 커밋을 가리키는 ' master'와 ' develop'의 두 가지 분기 가 있고 현재 ' develop'(HEAD 가 가리킴 )에 있고 git reset master' develop' 가 실행 되면 ' '자체는 이제 동일한 커밋을 가리 킵니다. master'않습니다.

반면에 우리가 대신 달리면 git checkout master' develop'는 움직이지 않을 HEAD것입니다. HEAD이제 ' master'를 가리 킵니다 .

따라서 두 경우 모두 HEADcommit을 가리 키도록 이동 하고 A있지만 그렇게하는 방법은 매우 다릅니다. reset분기 HEAD지점을 가리키고, 결제는 HEAD다른 지점을 가리 키도록 이동 합니다.

http://git-scm.com/images/reset/reset-checkout.png

그러나 그 점에서 :

LarsH주석에 다음을 추가합니다 .

그러나이 답변의 첫 번째 단락은 오해의 소지가 있습니다. " git checkout... 분기를 체크 아웃 한 경우에만 HEAD가 업데이트됩니다 (그렇지 않은 경우 HEAD가 분리됨)".
사실이 아님 : git checkout브랜치가 아닌 커밋을 체크 아웃하더라도 HEAD를 업데이트합니다 (그렇습니다. 분리 된 HEAD로 끝나지만 여전히 업데이트됩니다).

git checkout a839e8f updates HEAD to point to commit a839e8f.

De Novo의견에 동의 합니다 .

@LarsH가 정확합니다.
두 번째 글 머리 기호는 HEAD가 무엇인지에 대한 오해가 있으므로 지점을 체크 아웃하는 경우에만 HEAD가 업데이트됩니다.
머리는 그림자처럼 당신이 어디에 있든갑니다.
브랜치가 아닌 참조 (예 : 태그) 또는 커밋을 직접 확인하면 HEAD가 이동합니다. 분리 된 헤드는 HEAD에서 분리 된 것이 아니라 헤드가 분기 참조에서 분리 된 것을 의미합니다 (예 :) git log --pretty=format:"%d" -1.

  • 연결된 헤드 상태는로 시작합니다 (HEAD ->.
  • detached는 여전히을 표시 (HEAD하지만 분기 참조에 대한 화살표는 없습니다.

가장 간단한 형태로, reset작업 트리를 건드리지 않고 인덱스를 재설정하고 인덱스를 건드리지 않고 작업 트리를 checkout변경합니다.

HEAD작업 트리를 그대로두면 인덱스가 재설정됩니다 .

git reset

Conceptually, this checks out the index into the working tree. To get it to actually do anything you would have to use -f to force it to overwrite any local changes. This is a safety feature to make sure that the "no argument" form isn't destructive:

git checkout

Once you start adding parameters it is true that there is some overlap.

checkout is usually used with a branch, tag or commit. In this case it will reset HEAD and the index to the given commit as well as performing the checkout of the index into the working tree.

Also, if you supply --hard to reset you can ask reset to overwrite the working tree as well as resetting the index.

If you current have a branch checked out out there is a crucial different between reset and checkout when you supply an alternative branch or commit. reset will change the current branch to point at the selected commit whereas checkout will leave the current branch alone but will checkout the supplied branch or commit instead.

Other forms of reset and commit involve supplying paths.

If you supply paths to reset you cannot supply --hard and reset will only change the index version of the supplied paths to the version in the supplied commit (or HEAD if you don't specify a commit).

If you supply paths to checkout, like reset it will update the index version of the supplied paths to match the supplied commit (or HEAD) but it will always checkout the index version of the supplied paths into the working tree.


One simple use case when reverting change:
1. Use reset if you want to undo staging of a modified file.
2. Use checkout if you want to discard changes to unstaged file/s.


Atlassian give us an excellent explanation about git reset, git checkout and so, git revert. In this article, is explained the different uses of these commands on a different levels - file, staged snapshot and commit.

https://www.atlassian.com/git/tutorials/resetting-checking-out-and-reverting


The key difference in a nutshell is that reset moves the current branch reference, while checkout does not (it moves HEAD).

As the Pro Git book explains under Reset Demystified,

The first thing reset will do is move what HEAD points to. This isn’t the same as changing HEAD itself (which is what checkout does); reset moves the branch that HEAD is pointing to. This means if HEAD is set to the master branch (i.e. you’re currently on the master branch), running git reset 9e5e6a4 will start by making master point to 9e5e6a4. [emphasis added]

See also VonC's answer for a very helpful text and diagram excerpt from the same article, which I won't duplicate here.

물론 어떤 매개 변수가 사용되는지에 따라 인덱스 및 작업 트리에 미치는 영향 checkout영향에 대한 자세한 내용이 reset있습니다. 두 명령간에 많은 유사점과 차이점이있을 수 있습니다. 그러나 내가 알 수 있듯이 가장 중요한 차이점은 현재 지점의 끝을 움직 일지 여부입니다.


두 명령 (재설정 및 체크 아웃)은 완전히 다릅니다.

checkout X 아니다 reset --hard X

X가 지점 이름이면 checkout X현재 지점은 변경하지만 변경 reset --hard X하지는 않습니다.

참고 URL : https://stackoverflow.com/questions/3639342/whats-the-difference-between-git-reset-and-git-checkout



반응형