0

When you create a new branch which already exits in a remote repository but don't in a local repository, probably you can run these two commands:git checkout -b hotfix origin/hotfix and git branch hotfix origin/hotfix where hotfix is exactly the branch I hypothesized. So, what is exactly the difference between these two commands? It seems that both makes a new branch already tracking the upstream branch.

Can anyone explain it?

Kazuya Tomita
  • 667
  • 14
  • 34

1 Answers1

2

git branch only create the branch, but head still remain pointed to previous branch .. for example if your current branch was master and you execute git branch abc, abc branch will be created but current branch still be master.

git checkout -b abc, first create the branch plus checkout on top of branch creation... so if master was current branch, post execution abc will be current branch

yes both makes a new branch.. but first first one is creation of branch + checkout of that branch

  • In addition, when using `git checkout -b` to create a new branch, the creation-and-switch are *atomic* in that either the new branch is created *and* checked out, or if the checkout fails, the new branch is *not* created at all. With `git branch []` the new branch is created even if a subsequent `git checkout` of that new branch fails. Usually this difference is irrelevant, but if you wish to delete the new branch automatically if the checkout fails, you can obtain that with `git checkout -b`. – torek Jan 14 '17 at 15:26
  • @torek Very intriguing comment! However, under what situation do we fail a checkout although creating the branch is done successfully? – Kazuya Tomita Jan 15 '17 at 01:22
  • @KazuyaTomita: Without a `` the only thing that should cause checkout to fail is a local file system error (e.g., out of disk space). With a `` you'll get a failure to switch to the new commit if you have uncommitted changes in index and/or work-tree that must be, yet cannot be, carried over. See http://stackoverflow.com/q/22053757/1256452 for details. – torek Jan 15 '17 at 01:32
  • Thanks, Girish and torek! – Kazuya Tomita Jan 15 '17 at 10:47