Tuesday, 15 May 2012

Confused by git checkout -



Confused by git checkout -

i new git , trying wrap head around way branches work. according documentation git checkout

updates files in working tree match version in index or specified tree. if >no paths given, git checkout update head set specified branch >current branch.

so understand files in directory work in (the file performed git init in) should alter according branch in. confused because not happen when alter between branches. edits working on before switched branches nowadays in branch switched to. doing wrong or git checkout not work way , misunderstanding docs?

git has general issue of cramming 8 or 10 different things 1 command.

git checkout can update working tree, , does.

it can alter head points, , does, doesn't.

it can overwrite work did file, in case want reset file , undo work. or can reject overwrite work did file, leaving unchanged while either changing head, or not changing head.

the thing that, while it's remarkably hard describe, makes sense , after while used , find 1 command mean, of time. (it's "most of time" can problem, of course....)

anyway, particular behavior you're seeing deliberate feature. let's start out on branch master, repositories do:

$ git clone ... $ git branch * master $

at point might edit file(s), work going, , realize: "gah! meant on branch develop!"1

what git lets @ point switch (or create) branch develop, keeping modifications, under 1 condition: switching develop not require wiping them out. let's modified file f1 , created new f2, , want create , check out local branch develop should start from, , automatically "track",2 origin/develop:

$ git checkout develop

(in old versions of git, have spell git checkout -b develop --track origin/develop).

let's file f1 same @ tips of branch master , branch develop.3 means, git, can checkout, because not have modify file f1, can leave existing changes f1 in place.

if file f2 also same in both commits, or (as in case) not exist in either one, no files clobbered, , git checkout create new local branch develop, modifying work tree match origin/develop needed—and not include modifying f1, nor removing f2, work have done far remains intact.

this allows commit new changes local develop.

(if run cases git does have undo changes, still want "move" them branch, usual trick utilize git stash script. sounds simple thing, , git stash simple use, quite complicated little beast under covers. don't worry until need it, though.)

1this happens me all time. many times want create new non-tracking branch, bit simpler switching existing branch, principle still applies.

2this automatic tracking allows more bring in changes other people have done: 1 time git picks them git fetch, git inform other-people's-changes, , allow utilize git merge or git rebase combine changes theirs, without lot of poking-about figure out changes go where.

3since you're new git, concepts distinguishing "the tip of branch", specific commit, "the branch", ambiguous—there branch labels, , there branch structures formed commit tree—is else should ignore while. main thing note there's special file in git repository named head, , in special file, git writes string ref: refs/heads/master or ref: refs/heads/develop, in order maintain track of branch you're on. git checkout x write ref: refs/heads/x head 1 time switches branch x.

meanwhile, set of special files in repository tell git branch master refers 1 of big ugly sha-1s c06f8d11b75e28328cdc809397eddd768ebeb533. "tip" of branch master. when create new commit on master, git creates new commit "one past old tip", writes new sha-1 branch files, master new commit.

the precise details don't matter much thought new commits advance branch-tip.

git

No comments:

Post a Comment