Programming

git checkout --track origin / branch와 git checkout -b branch origin / branch의 차이점

procodes 2020. 5. 15. 21:11
반응형

git checkout --track origin / branch와 git checkout -b branch origin / branch의 차이점


원격 분기를 전환하고 추적하기 위해이 두 명령의 차이점을 아는 사람이 있습니까?

git checkout -b branch origin/branch
git checkout --track origin/branch

둘 다 원격 지점을 추적하여 변경 사항을 원점에서 지점으로 푸시 할 수 있다고 생각합니까?

실질적인 차이점이 있습니까 ??

감사!


두 명령은 동일한 효과를 나타냅니다 ( Robert Siemer의 답변 덕분에 ).

실질적인 차이점은 다른 이름의 로컬 브랜치를 사용할 때 발생합니다 .

  • git checkout -b mybranch origin/abranch만들고 mybranch추적합니다origin/abranch
  • git checkout --track origin/abranchabranch이름이 다른 분기가 아닌 ' ' 만 만듭니다 .

( 세바스찬 그라프 (S Sebastian Graf)의 의견따르면 로컬 지점이 아직 존재 하지 않으면 필요합니다. )
git checkout -B abranch origin/abranch


참고 : Git 2.23 (Q3 2019)에서는 새로운 명령을git switch 사용합니다 .

git switch -c <branch> --track <remote>/<branch>

브랜치가 여러 리모트에 존재하고 그 중 하나가 checkout.defaultRemote구성 변수에 의해 이름이 지정된 경우, <branch>모든 리모트에서 고유하지 않은 경우에도 명확성을 위해 해당 브랜치를 사용합니다 .
예를 들어 모호하지만 '원점'원격에 존재하는 checkout.defaultRemote=origin경우 항상 원격 지점을 체크 아웃 하도록 설정하십시오 <branch>.

여기서 ' -c'는 새로운 ' -b'입니다.


첫째, 일부 배경 : 추적 은 로컬 지점의 업스트림이 원격 지점으로 설정되었음을 의미합니다.

# git config branch.<branch-name>.remote origin
# git config branch.<branch-name>.merge refs/heads/branch

git checkout -b branch origin/branch 의지:

  • 에서 branch참조하는 지점으로 작성 / 재설정 합니다 origin/branch.
  • 브랜치 생성 branch(을 가진 git branch)와 원격 지점 추적 추적 origin/branch.

로컬 분기가 원격 추적 분기에서 시작되면 Gitbranch.<name>.remotebranch.<name>.mergegit pull 은 원격 추적 분기에서 적절히 병합 되도록 분기 (특히 구성 항목)를 설정합니다 .
이 동작은 전역 branch.autosetupmerge구성 플래그 를 통해 변경 될 수 있습니다 . 이 설정은 --trackand --no-track옵션 을 사용하여 재정의하고 나중에 git branch를 사용하여 변경할 수 있습니다 --set-upstream-to.


)와 git checkout --track origin/branch동일하게 수행됩니다 git branch --set-upstream-to.

 # or, since 1.7.0
 git branch --set-upstream upstream/branch branch
 # or, since 1.8.0 (October 2012)
 git branch --set-upstream-to upstream/branch branch
 # the short version remains the same:
 git branch -u upstream/branch branch

또한 ' branch' 의 업스트림을 설정했습니다 .

(참고 : git1.8.0은 더 이상 사용되지 않으며 다음 git branch --set-upstream으로 대체됩니다 git branch -u|--set-upstream-to: git1.8.0-rc1 announce 참조 )


로컬 지점에 업스트림 지점을 등록하면 다음과 같은 이점이 있습니다.

  • git에게 에서 두 분기 사이의 관계git statusgit branch -v표시하도록 지시 하십시오 .
  • 새로운 브랜치가 체크 아웃 될 때 git pull 인수없이 업스트림에서 끌어 오도록 지시 합니다 .

자세한 내용은 " 기존 git 브랜치 트랙을 원격 브랜치로 만드는 방법 "을 참조하십시오.


전혀 차이가 없습니다!

1) git checkout -b branch origin/branch

어떤이없는 경우 --track--no-track, --track기본값으로지지 않습니다. 설정으로 기본값을 변경할 수 있습니다 branch.autosetupmerge.

실제로 1)은 다음과 같이 동작 git checkout -b branch --track origin/branch합니다.

2) git checkout --track origin/branch

"편의상"으로 암시 --track하지 않고 " 논쟁 "으로 간주됩니다. 추측은 구성 변수에 의해 결정 됩니다.-b-b-bremote.origin.fetch

실제로 2)는 다음과 같이 동작 git checkout -b branch --track origin/branch합니다.

보다시피 차이가 없습니다.

그러나 더 좋아집니다.

삼) git checkout branch

또한 동일하다 git checkout -b branch --track origin/branch"분기"아직 존재하지 않지만 "원산지 / 지사는"않는 경우 1 .


세 가지 명령 모두“branch”의“upstream”을“origin / branch”(또는 실패)로 설정합니다.

업스트림의 기준점으로 사용되는 인수리스 git status, git push, git merge따라서 git pull(디폴트 또는 거의 기본값 인 (즉, 같은 구성된 경우)).

예를 들어 git status업스트림에서 어느 정도 앞뒤로 구성되어 있는지 알려줍니다.

git push is configured to push the current branch upstream by default2 since git 2.0.

1 ...and if “origin” is the only remote having “branch”
2 the default (named “simple”) also enforces for both branch names to be equal


The book seems to indicate that those commands yield the same effect:

The simple case is the example you just saw, running git checkout -b [branch] [remotename]/[branch]. If you have Git version 1.6.2 or later, you can also use the --track shorthand:

$ git checkout --track origin/serverfix 
Branch serverfix set up to track remote branch serverfix from origin. 
Switched to a new branch 'serverfix' 

To set up a local branch with a different name than the remote branch, you can easily use the first version with a different local branch name:

$ git checkout -b sf origin/serverfix

That's particularly handy when your bash or oh-my-zsh git completions are able to pull the origin/serverfix name for you - just append --track (or -t) and you are on your way.


You can't create a new branch with this command

git checkout --track origin/branch

if you have changes that are not staged.

Here is example:

$ 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:   src/App.js

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

// TRY TO CREATE:

$ git checkout --track origin/new-branch
fatal: 'origin/new-branch' is not a commit and a branch 'new-branch' cannot be created from it

However you can easily create a new branch with un-staged changes with git checkout -b command:

$ git checkout -b new-branch
Switched to a new branch 'new-branch'
M       src/App.js

참고URL : https://stackoverflow.com/questions/10002239/difference-between-git-checkout-track-origin-branch-and-git-checkout-b-branch

반응형