LWN.net Logo

Git Magic

Git Magic may not be exactly new, but some of us have stumbled across it later than others. It is a highly readable introduction to git with lots of examples of how to get things done. "As Arthur C. Clarke observed, any sufficiently advanced technology is indistinguishable from magic. This is a great way to approach Git: newbies can ignore its inner workings and view Git as a gizmo that can amaze friends and infuriate enemies with its wondrous abilities. Rather than go into details, we provide rough instructions for particular effects. After repeated use, gradually you will understand how each trick works, and how to tailor the recipes for your needs."
(Log in to post comments)

Simplified porcelains?

Posted Aug 5, 2008 7:37 UTC (Tue) by epa (subscriber, #39769) [Link]

I remember when git was first announced, it was described more as a library than a finished
application; a content-addressable filesystem that happened to contain a version control tool,
just as Emacs is a Lisp environment that happens to contain an editor.  The idea was that
different 'porcelains' could be written with the same 'plumbing'.  Sure enough, the Cogito
front end appeared, and possibly others.

But now that seems to have receded; Cogito is deprecated, and the momentum looks to be behind
just using git as distributed, with all its two hundred commands or however many there are.  I
haven't tried git yet because I am worried it is not suitable for stupid people.  The 'git
magic' page doesn't really allay my fears; is there an introduction that just lists the three
or four commands you need to start using git as if it were CVS or SVN?

(The assumption in this is that one should use git, rather than hg, bzr or darcs, because it's
what everyone else uses...  I think that might well be true.)

Simplified porcelains?

Posted Aug 5, 2008 9:05 UTC (Tue) by alex (subscriber, #1355) [Link]

In reality the day to day usage of git comes down to the same 5-6 commands. The rest is just
plumbing which is useful in a very small set of circumstances. However the reason Cogitco fell
by the wayside is you could do all the porcelain type actions with the core git commands.

I believe there was talk about removing a bunch of the git-* commands from the search path
(they are just symlinks anyway). I'm not sure where that has gotten too. 

Simplified porcelains?

Posted Aug 5, 2008 9:10 UTC (Tue) by epa (subscriber, #39769) [Link]

The thing that worries me from the article is that 'git checkout HEAD' (and perhaps other
commands) lose any uncommitted changes.  With svn, I know that it is always safe to update to
the latest version or to any version, since it will merge the changes with what's in my
working copy.

It's clear that git never permanently loses a commit, unless you run the prune command, but I
would want the revision control system to take just as much care with the contents of my
working directory, even if not committed yet.

What about Easy Git, is that still going?

Simplified porcelains?

Posted Aug 5, 2008 9:38 UTC (Tue) by nix (subscriber, #2304) [Link]

'git checkout HEAD' fails unless your tree is clean. 'git reset --hard HEAD' will throw your
changes away, but that's fairly difficult to type unless you know what you're doing. ('git
stash' is worth looking at if you're often trying to move around with uncommitted changes in
your tree.)

Simplified porcelains?

Posted Aug 5, 2008 11:19 UTC (Tue) by iabervon (subscriber, #722) [Link]

"git checkout HEAD" doesn't do anything (expect possibly display a bit of useful information).
It's "git checkout HEAD ." (note the filename "." at the end) that will discard uncommitted
changes, because (like in CVS), it's a request to cause the working directory to match "HEAD"
with respect to the path ".".

But I think that section shouldn't really be in that document; it's talking about a
non-recommended method for keeping two computers in sync by confusing one of them and then
intentionally losing the false changes that the computer thinks it has.

Git is actually extremely careful about uncommitted changes. The only ways to lose uncommitted
changes with git are to either ask to discard them (with "checkout <filename>" or "reset") or
ask to have working-directory files modified or removed (e.g., "apply" or "rm"). By default,
git won't even overwrite modified working directory files with merge results like other
version control systems will.

Simplified porcelains?

Posted Aug 5, 2008 11:40 UTC (Tue) by newren (subscriber, #5160) [Link]

Yes, EasyGit (eg) is still going. I've used it on a daily basis for nearly the last 6 months, and many others have told me it's helped them in getting started with git.

Just download eg and fire off eg help to get started, or look at the detailed comparison to subversion.

EasyGit basically provides beginner-oriented built-in documentation, checks for and warns about common gotchas with git, and layers things in an orderly fashion. You can switch back and forth between eg and normal git commands too; in fact, there are --translate and --debug options designed to show you what git commands eg is running behind the scenes (in addition to clearly documented differences between eg and git commands, where such differences exist).

I've been a little slow at responding at times, and haven't published some updates and commentary I've been meaning too, but eg is perfectly usable for daily work. I have too many users to abandon it...

Simplified porcelains?

Posted Aug 5, 2008 21:47 UTC (Tue) by benlynn (guest, #53270) [Link]

My bad: in the old days, git was less forgiving. As others have noted, nowadays a checkout
fails on unclean trees. That is, unless you specify particular paths in which case only those
particular paths are overwritten. I've updated the guide to reflect this.

Simplified porcelains?

Posted Aug 5, 2008 9:14 UTC (Tue) by Sutoka (guest, #43890) [Link]

> "In reality the day to day usage of git comes down to the same 5-6 commands."

This is definitely true for me. The day-to-day git commands I use are:
git diff (incredibly useful, use it constantly)
git commit -a (automatically commit only the changed files that are already in the repo)
git push (push my commits to the remote server)

This are really all the commands I use frequently. Occasionally I'll use 'git add <filename'
if I add a new file I wanted monitored, 'git init' is useful when you want to create a new
repo in a folder (not something you'll need very often probably), and off the top of my head I
don't remember the command to switch branches (I haven't gotten to the point where I use them
at all regularly).

Git really is quite simple if you have simple needs like me, and it's also quite nice because
theres several hosts that'll provide free hosting for repos without having to do lots of
effort to setup an account then get the project approved.

/etc is a good candidate

Posted Aug 5, 2008 9:23 UTC (Tue) by alex (subscriber, #1355) [Link]

Every time I go into a sub directory in /etc to edit a file for config for the first time the
first thing I do is:

git-init-db; git-add .; git-commit -m "Initial Version"

I'd tried various approaches with CVS before but it quickly turned ugly. Now I can easily roll
back changes, start again or fork of in a new direction without loosing track of what I did
and why.

Good idea

Posted Aug 5, 2008 9:30 UTC (Tue) by dmarti (subscriber, #11625) [Link]

Good idea, but have a look at etckeeper. It will also automatically track any changes that the package manager makes in /etc.

for now I'll hand roll

Posted Aug 5, 2008 10:48 UTC (Tue) by alex (subscriber, #1355) [Link]

For now I'll hand roll. I started with my whole /etc in git, no I tend to have mini-repos for
each bit of /etc so it's not tied to the rest of the system. Hence I can pull my apache or
exim config without the mess of the rest of /etc involved :-)

/etc is a good candidate

Posted Aug 5, 2008 11:19 UTC (Tue) by mikachu (guest, #5333) [Link]

You should be using "git init; git add .; git commit -m 'Initial Version'", since init-db has
been deprecated since before i can remember. git-* have been since 1.5, and will not be
available by default in 1.6.0.

/etc is a good candidate

Posted Aug 6, 2008 0:32 UTC (Wed) by alex (subscriber, #1355) [Link]

Well all the servers which I muck around with the config on run Debian which is a little
behind the bleeding git edge ;-)

/etc is a good candidate

Posted Aug 6, 2008 1:51 UTC (Wed) by nix (subscriber, #2304) [Link]

Storing /etc in git seems like a bad idea to me, as git doesn't preserve 
permissions (other than the executable bit), ownership information, hard 
links, or any type of file other than regular files and directories.

/etc is a good candidate

Posted Aug 6, 2008 2:44 UTC (Wed) by alex (subscriber, #1355) [Link]

I don't store the whole of etc, usually only the bits I need to tweak/configure, e.g.
/etc/apache2 or /etc/exim4

/etc is a good candidate

Posted Aug 6, 2008 5:07 UTC (Wed) by rleigh (subscriber, #14622) [Link]

There are, however, people who want git to be able to do this:

  [git storage backend for chroots]
  http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=477942

This is essentially using git directly as a snapshottable filesystem; 
cloning the chroot would be effectively a git clone (and/or branch).  In 
this case, file ownership, permissions, special files and empty 
directories are all features we would like git to store.  I have heard 
there are patches in the works to implement (some of) the above, though I 
haven't seen them myself.

This would also allow /etc to be stored directly in git.


Regards,
Roger

/etc is a good candidate

Posted Aug 6, 2008 10:42 UTC (Wed) by iabervon (subscriber, #722) [Link]

Ideally, it should be possible to have a more flexible mapping between the index and the
working directory, such that the repository's contents are of the same type as any other
repository (and it could be checked out as a description), but, when it's used as an actual
chroot, it works directly. For example, it should be possible to work on a chroot image for
some other machine without being root on the machine you're working on; this is easy if the
image is stored with the "special stuff" as plain files that only translate into device nodes
and permissions when in the chroot checkout.

(Actually, I suspect that the ideal thing is a fakeroot/git team effort, where git stores what
fakeroot stores, not what the actual filesystem stores, and you can check this out as a real
chroot image if you're root, or fake it in fakeroot, or as a set of control files and data if
you want that.)

/etc is a good candidate

Posted Aug 6, 2008 15:33 UTC (Wed) by rickmoen (subscriber, #6943) [Link]

nix wrote:

Storing /etc in git seems like a bad idea to me, as git doesn't preserve permissions (other than the executable bit), ownership information, hard links, or any type of file other than regular files and directories.

etckeeper's author (Joey Hess) was well aware of this; that's why it uses metastore to encode that information transparently, so that git can track it.

(etckeeper's integration into Debian/*buntu package management is also a killer advantage, over just checking /etc into an RCS: Changes to /etc resulting from a package operation get checked in automatically.)

Rick Moen
rick@linuxmafia.com

/etc is a good candidate

Posted Aug 6, 2008 18:25 UTC (Wed) by bronson (subscriber, #4806) [Link]

It doesn't use metastore anymore.  A certain lwn reader didn't like having a big binary-only
file in /etc and sent Joey some patches to replace it with a text file and simple find
commands.  ~March 17-20, 2008

I like etckeeper.  When the bad stuff starts flying, it sure is nice to be able to roll back
to a known good config.  And the cryptographic integrity makes a nice sanity check.

Simplified porcelains?

Posted Aug 5, 2008 11:24 UTC (Tue) by jwb (subscriber, #15467) [Link]

git push only works if your "origin" is somehow magically configured in a dotfile which is
very poorly explained deep in the 50th page of the manual.  Unless that's been fixed in the
last year or so.

Simplified porcelains?

Posted Aug 5, 2008 13:10 UTC (Tue) by bronson (subscriber, #4806) [Link]

Still not fixed.  :/  On the other hand, if you clone a repo, it automatically sets it up so
that push with no arguments pushes back to that repo.  That way, at least, most people will
never need to worry about it.

Simplified porcelains?

Posted Aug 5, 2008 14:30 UTC (Tue) by bfields (subscriber, #19510) [Link]

git push only works if your "origin" is somehow magically configured in a dotfile

Yes, that's true of a plain "git push" with no arguments, though of course you can always just give a url (or the name of a remote).

which is very poorly explained deep in the 50th page of the manual.

You're talking about this? Actually I'm not sure if it ever explicitly says that "origin" is the default, though it does show how to add a new remote.

Any concrete suggestions would be appreciated. I do agree that remotes could be better explained.

Simplified porcelains?

Posted Aug 5, 2008 14:36 UTC (Tue) by Sutoka (guest, #43890) [Link]

The tutorial I used to learn git was: http://www.kernel.org/pub/software/scm/git/docs/gittutorial.html

For a while it vanished from google so I had lost it until just now when I searched for it. To get git push working all I did was just followed the instructions on my Gitorious project page:
You can run "git push git@gitorious.org:[project]/mainline.git", or you can setup a remote by doing the following:
git remote add origin git@gitorious.org:[project]/mainline.git
# to push the master branch to the origin remote we added above:
git push origin master
# after that you can just do:
git push

Basically, it was extremely for me easy to setup, it might be harder if you're trying to setup the remote server yourself, but I tried that several times with SVN and CVS and never once got it to work (plus the tutorials I kept finding were having me install apache and a crap load of other services).

My only 'problem' with Git is that I didn't really know how to use Vim so I kinda had to learn that on the fly. IMO, Git is extremely easy to learn and takes very little effort to use (definitely more intuitive than Vim :P).

Simplified porcelains?

Posted Aug 5, 2008 15:30 UTC (Tue) by bronson (subscriber, #4806) [Link]

Git just uses EDITOR.  So, for instance, if you wanted to edit your commit message using nano,
run "EDITOR=/usr/bin/nano git commit"

Or put "setenv EDITOR=/usr/bin/nano" in your .bashrc to make the change permanent.

Editor

Posted Aug 6, 2008 0:40 UTC (Wed) by alex (subscriber, #1355) [Link]

I find bringing up emacs each commit can be a pain so I tend to just use the -m switch:


git-commit -m "My commit message, maybe over serveral lines" filaA fileB

Editor

Posted Aug 6, 2008 1:52 UTC (Wed) by nix (subscriber, #2304) [Link]

Set your EDITOR to emacsclient and leave an emacs running.

Editor

Posted Aug 6, 2008 2:48 UTC (Wed) by alex (subscriber, #1355) [Link]

Sadly I have yet to come up with a solution that works well with all my needs. I tend to run
at least one emacs per "project" which is long running as well as temporary ones for commits
or editing pages. I suspect I just need to think about rules for starting an emacs server in
my .emacs.

The other problem is running text mode emacs on remote screen sessions. I don't want to switch
to another (v)tty when EDITOR gets invoked. I've heard there is a thing called multi-tty but I
have yet to investigate it and it's implications.

Pointers to pimped up emacsserver examples are welcomed :-)

Editor

Posted Aug 6, 2008 3:04 UTC (Wed) by nix (subscriber, #2304) [Link]

What I'd suggest is an interactive function bound to some key which does a 
gnudoit (? is this the Emacs name?) to stop whatever server exists via 
(server-start t), then does a (server-start) in the current instance. So 
then you can switch which emacs your emacsclient invocation talks to with 
a single keystroke.

Alternatively you could set `server-name' on the servers and use 
the --socket-name argument to emacsclient.

There are more elaborate methods but these two should work.

I'm afraid as an XEmacs user the multi-tty stuff just works for me ;)

Editor

Posted Aug 6, 2008 8:54 UTC (Wed) by newren (subscriber, #5160) [Link]

You could just put the commit message in a file and use the -F flag.  e.g.  git commit -F
file-with-commit-message

Simplified porcelains?

Posted Aug 5, 2008 10:39 UTC (Tue) by carenas (subscriber, #46541) [Link]

for a side by side command list compared to svn there is :

  http://git.or.cz/course/svn.html

and for using git with a svn repository use :

  git svn

Simplified porcelains?

Posted Aug 6, 2008 3:52 UTC (Wed) by dpotapov (guest, #46495) [Link]

epa wrote: "I am worried it is not suitable for stupid people."

I think you should not worry much about stupid people, at least, not as much as about close
minded ones.  As practice has shown, learning basic commands is not so difficult if you were
given proper instructions as to what should be learned first.

However, if someone approaches to it with the attitude -- I want to work with Git in the exact
same way as I do with CVS then it is a problem.  Git does not try to emulate CVS commands, so
the only emulation it provides is git cvsserver, which enables usual CVS clients to work with
the Git repository.

For everyday work with Git, you do not have to know many more commands than with CVS. In fact,
it may be only one or two commands more. This is because when you commit, you do that locally,
so you need to know the "git push" command to push your changes to the remote server. Thus,
roughly speaking: cvs commit = git commit -a && git push

However, the ability to commit locally is a great advantage of Git (or any other DVCS). It is
not only allow you to work offline, but it allows you to save your intermediate steps that you
may want to re-work or even completely remove later without showing anyone. With CVS or SVN,
you usually do not commit, unless you are satisfied with result of your work and run all tests
on it, as result, you commit a big patch-bomb. Git allows you to save your changes
incrementally in logical steps and rework them if necessary later; and only when you happy
with results of your work, you do "git push".

Finally, someone may ask if for most everyday work, you need to know only 5 or 6 Git commands,
why Git has about 150 commands? My answer is simple: you may need them one day, and it is nice
to have the ready solution for you. Look at it in this way: Notepad has about 5 key bindings,
but you do not use it to write your programs, do you? And your favorite editor is likely to
have by an order or two more key bindings than Notepad. Still, you don't think that Notepad is
easier to use than any more feature-rich editor, right?

Simplified porcelains?

Posted Aug 6, 2008 4:15 UTC (Wed) by epa (subscriber, #39769) [Link]

For everyday work with Git, you do not have to know many more commands than with CVS. In fact, it may be only one or two commands more. This is because when you commit, you do that locally, so you need to know the "git push" command to push your changes to the remote server. Thus, roughly speaking: cvs commit = git commit -a && git push
I think that is the biggest difference. 99% of people do not use branches in CVS or Subversion, so they don't need to consider differences in how branches or merging are handled.

Is there a true equivalent of 'svn diff', that is, show the differences between your local directory (whether changes have been committed or not) and some remote repository? With svn, you can commit something and know that it's definitely there on the server, being backed up every night. You can say 'svn status' for reassurance that you don't have any changes which are not in a safe place. How do you get that same warm fuzzy feeling with git?

Simplified porcelains?

Posted Aug 6, 2008 8:50 UTC (Wed) by newren (subscriber, #5160) [Link]

You can use 'eg diff' to get the direct equivalent of svn diff, if you install EasyGit (eg). Otherwise, use 'git diff HEAD'. (one caveat: 'git diff HEAD' doesn't make sense in the case of an incomplete merge, because there isn't just one last revision to compare to, but two since you are merging things together. In such a case, you'd use something else; though eg diff works just fine.)

Simplified porcelains?

Posted Aug 6, 2008 10:07 UTC (Wed) by tialaramex (subscriber, #21167) [Link]

I think there's several questions buried in there. One thing you'd probably like if you
haven't seen it already is (type it exactly as shown)

git log origin..

This asks for a log of commits between 'origin' and your present reference which is probably
the HEAD of your local repository or a branch thereof. 'origin' is the remote repository
(either set up manually following the convention, or as a result of using 'git clone')

Thus, anything on this list has been committed locally but not pushed upstream yet and it's
your responsibility to back it up or push it if you don't want to lose the changes when your
5yo nephew throws the laptop into the bird pond.

Of course you also want to look at 'git status' for reassurance that you don't have any
changes that aren't checked in locally. But after a little while using 'git' you get so used
to the speed and convenience of local commits that you use them with the same unconscious ease
that you use the "save" feature in your text editor of choice.

Simplified porcelains?

Posted Aug 6, 2008 11:10 UTC (Wed) by iabervon (subscriber, #722) [Link]

Actually, everybody uses what git calls branches in CVS (although CVS branches are something
different). If you're doing work in your local directory, and someone else makes a commit to
the central server, you don't see that change until you update; these two simultaneous
different states of the same project are what git calls branches. Git just makes this
explicit. Furthermore, everybody uses CVS's basic merging in update, and git is pretty similar
in the same situations.

The main difference between CVS and git in this regard is that CVS requires you to resolve
conflicts before committing, and git requires you to commit before resolving conflicts.
Obviously, if you sometimes fail when trying to resolve conflicts, git is nicer (because you
can get back your version and try again or try merging with only some of the changes at a time
or something).

Simplified porcelains?

Posted Aug 6, 2008 15:14 UTC (Wed) by dpotapov (guest, #46495) [Link]

epa wrote: "99% of people do not use branches in CVS or Subversion"

Of course, they don't, because it is so painful with CVS or Subversion that people go at great
length avoiding them wherever possible. But Git developers use branches not because they are
"cool features", but because their usage significantly simplifies and speeds up development.
And the speed at which Git develops is the best testament to that.


epa: "how the differences between your local directory (whether changes have been committed or
not) and some remote repository"

If you already fetch changes from that remote. Then you can run:
git diff remote-name/branch-name


epa: "With svn, you can commit something and know that it's definitely there on the server,
being backed up every night."

The difference is that with SVN, you CANNOT backup your work in progress, while Git allows you
to that easily. You can backup all your local repo on the server (or even on a few servers if
you are really paranoiac). You can do that automatically on every commit or follow whatever
backup policy you like. Git is fully distributed system and mirroring changes is not a problem
at all!


epa: "You can say 'svn status' for reassurance that you don't have any changes which are not
in a safe place."

And that is how the central CVS and SVN repositories slowly but surely are turning in mess...
Just let any developer to commit without review and proper testing, just because they feel
that they need to save their work at the end of their day, and you are going to have a lot of
mess soon.

BTW, in respectable companies like Google, there is such a thing as code review, which means
that you can wait sometimes for days (happens even for weeks!) before you can check-in...

Even without code review, the full cycle of testing and building on different platforms and
different configurations may take hours. Are you going to wait at the end of your working day
when these tests have finished?

With Git, you are going home knowing that your changes are properly backed and in testing now,
and if results are okay (and you do not have to wait for someone's else approval), you can
push changes to the official repo. (In some workflows, most developers never push to the
official repo. So just send an email to the team lead saying that you have accomplished the
task, and then the team lead will either pull those changes or get back to you saying what
s/he does not like about your changes.)

Simplified porcelains?

Posted Aug 7, 2008 8:45 UTC (Thu) by BenHutchings (subscriber, #37955) [Link]

"Is there a true equivalent of 'svn diff', that is, show the differences between your local
directory (whether changes have been committed or not) and some remote repository?"

'svn diff' doesn't touch the repository either, unless you give it URLs.

Simplified porcelains?

Posted Aug 7, 2008 15:44 UTC (Thu) by magnus (subscriber, #34778) [Link]

> How do you get that same warm fuzzy feeling with git? 

Perhaps you could use git push --dry-run to see that nothing needs to be pushed to the server.

With respect to server backups, it's worth noting that since each developer keeps a full copy
of the repository, this by itself works as a distributed backup. 

Git Magic

Posted Aug 5, 2008 20:10 UTC (Tue) by yarikoptic (subscriber, #36795) [Link]

vcs-home 'project' of madduck might be of interest for some of you who likes to keep your home
(or biggest part of it) under vcs
http://vcs-home.madduck.net/

I used to keep my home under CVS but slowly moving away to git + mr. Some kind of a summary my
setup is under
http://lists.madduck.net/pipermail/vcs-home/2008-July/000...


Git Magic

Posted Aug 5, 2008 20:21 UTC (Tue) by yarikoptic (subscriber, #36795) [Link]

There is one imho important  hint is given in the article, which is not unrolled well: "The
truly paranoid should always write down the latest 20-byte SHA1 hash of the HEAD somewhere
safe. It has to be safe, not private. For example, publishing it in a newspaper would work
well, because it's hard for an attacker to alter every copy of a newspaper."

Well -- if you are only up to using basic commands like fetch/merge/commit/... and not playing
with 'magic' rebase (or resets), then this hint is not so important to you. But git being
versatile could be somewhat fragile from the user's point of view -- you could easily lose
your HEAD while rebasing things around. Since git itself doesn't remove any known state
identified by sha-sum, it would remain in the repository even it is not within any of visible
branches (unless you do "git gc"). Thus you might loose your HEAD and get hours of your work
(or days) disappear magically (unless you pushed it somewhere or someone pulled from your
repository), but if you know SHA-sum you could easily reset your HEAD back to the desired
state.

Being totally paranoid (at times) I just tar-gzip whole repository before doing major rebases,
although now I can't even recall when I restored my repository back from the tarballs in the
event of the disaster ;)

Git Magic

Posted Aug 5, 2008 21:37 UTC (Tue) by newren (subscriber, #5160) [Link]

Why not just use the reflog to recover?  For example:

$ git reflog show HEAD

You can use a branch name, such as 'master', in the place of 'HEAD' too.  That will show the
different sha1's that the branch (or HEAD) has stored over time with information about what
caused the change (a commit, a merge, a reset, a checkout, a rebase, etc.)  You can also use
the named versions to obtain an old revision, e.g.

$ git reset --hard HEAD@{12} 

Git Magic

Posted Aug 5, 2008 21:58 UTC (Tue) by yarikoptic (subscriber, #36795) [Link]

learn new things every day! Now I will use it. not sure if for critical changes I would
abandon full-backup but now I would feel safer - Linus watched my back ;)

Can "git clone" checkout a specified branch now?

Posted Aug 5, 2008 22:23 UTC (Tue) by bbbush (subscriber, #17456) [Link]

Actually what I want to do is

mkdir folder1
cd folder1 && git init
git pull <url> <branch>

but I wonder if I can use "git clone <url> -b <branch>" instead of several commands?

Can "git clone" checkout a specified branch now?

Posted Aug 6, 2008 0:11 UTC (Wed) by dh (subscriber, #153) [Link]

Hi,

that doesn't make sense. "clone" always clones the complete repository 
including all branches. You then only decide which branch should drive 
your working tree. So, my workflow always is

git clone <some-url>
git checkout <wanted-branch>

Best regards,
Dirk

Can "git clone" checkout a specified branch now?

Posted Aug 6, 2008 0:18 UTC (Wed) by bbbush (subscriber, #17456) [Link]

Only doesn't make sense for "clone" itself but for usability

Can "git clone" checkout a specified branch now?

Posted Aug 6, 2008 9:05 UTC (Wed) by newren (subscriber, #5160) [Link]

I've wanted that as well, but no, I haven't seen it in git. I've used the commands you gave sometimes, though I also once put a script together with the following commands:

git clone --no-checkout REPOSITORY
git symbolic-ref HEAD refs/heads/BRANCH
git read-tree -m -u -v HEAD HEAD

Typically users aren't supposed to be exposed to symbolic-ref or read-tree; they're low-level "plumbing" commands. But it can be useful in a script if you have a reason for wrapping git as part of some other build tool or something.

Can "git clone" checkout a specified branch now?

Posted Aug 6, 2008 12:13 UTC (Wed) by iabervon (subscriber, #722) [Link]

No, but it should be pretty easy to add now. In builtin-clone, around line 491, it looks up
what HEAD points to on the remote repository, with remote_head being the "HEAD is <hash>" info
and head_points_at being the "refs/heads/master is <hash>" info (where the left part of that
is what HEAD is determined to be a reference to), if there is one. Just look up the user's
specified branch for both if the option was used instead of using locate_head() to find them.

(head_points_at is used so that you get a local branch with the same name as the remote
branch, if the remote repository doesn't have HEAD not on a branch; remote_head is used so
that you check out whatever tree it was that the remote had as HEAD, regardless of whether it
was a branch or not.)

Copyright © 2008, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds