Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Git Tips And Workflows (durdn.com)
148 points by durdn on Dec 5, 2012 | hide | past | favorite | 38 comments


Has anybody seen a "git for people who have no idea how to use git" tutorial?

I use git for keeping track of some personal projects, but the extent to which I understand how to use it is:

"git commit origin master"

We had a hackathon this weekend, and our team had three people on it. I wanted to make it so that they could also commit code into the same repo (I think this it the right term?), but had absolutely no idea how to actually make this happen, and googling didn't seem to give answers that were geared towards somebody with my level-of-understanding of git.


When I started with git, I really enjoyed the PeepCode Git Internals book: https://peepcode.com/products/git-internals-pdf

It's $12 (or $9 according to the preview) and the first thing it tells you to do is forget everything you know about source control. If you know SVN and want to use that knowledge to learn Git, I don't think you'd like this book. If you think knowing the underlying data model will help you be more effective with git, this book is for you.

The book has lots of diagrams and I thought it was pretty well written (and not too slow).

I use git for personal projects (not production code). Unlike many who think git is a hostile hateful thing, I think git has a brilliant command-line user interface. I don't know if this book made me that way, but I also use Vim so that might explain it (again, I disagree with the prevailing opinion of hostile hateful thing).


There is always:

http://try.github.com

For an interactive tutorial on adding files and the like. It gives a pretty file-centric view of the whole operation though... I'm working on a branch-centric tutorial here:

http://petercottle.com/gitGraph/src/index.html?demo

But it's not done yet (levels in the works)


Read ProGit: http://git-scm.com/book

> "git commit origin master"

That's not how to do it :)

> I wanted to make it so that they could also commit code into the same repo

No. Each developer works in her private repo, for sharing work you use a bare repo people push to / fetch from.


>No. Each developer works in her private repo, for sharing work you use a bare repo people push to / fetch from

What do you mean by a "bare repo"? blhack wants to make it so that they commit code into the same repo. You could do that by setting up a repository somewhere, and then each person would clone it.


--bare ensures you don't have a working directory. It is the bare .git directory only. You don't want people pushing to your working directory. Simple as that.


No, you are mixing things up here. commit is a local operation.


I didn't mean to indicate it wasn't.

You could setup a remote and each person could clone that remote.

I left out the details about what you do after you clone (push/pull), but the point was that there is nothing special about the remote repository as far as it being "bare". Which turns out isn't right. You can set up a bare repository with no working directory as 2mur has pointed out. I wonder if that is a requirement. I always thought that if you clone a repository you can just start using it as a remote without additional hassle.


> Which turns out isn't right.

Sorry, you lost me; what's not right?

> You can set up a bare repository with no working directory

A bare repository has no working tree per se.

> I wonder if that is a requirement. I always thought that if you clone a repository you can just start using it as a remote without additional hassle

You can use bare and non-bare repositories as remote, but you should use only bare repositories (unless you know what you are doing). Here's a good explanation: http://bare-vs-nonbare.gitrecipes.de/


It depends on whether you have used other version control before.

If you jump straight to using git in production , you'll experience a lot of "git anxiety" every time you run a command that you're not quite sure what it will do.

There's always the git book. http://git-scm.com/book

Realistically git isn't something that you can just pick up on an ad-hoc basis.

Best way is just to make a folder, fill it with some random text files and type "git init". Then you can practise, just make changes to files. Try branching and merging, try rebasing some commits to a different branch, try cherry picking from one branch to another. Make sure you get some practise in merging conflicting changes because that comes up annoyingly often in the real world (so pick a merge tool that you are comfortable using since git will plug in to a few but the default one is not the most friendly).

You can learn it pretty well from one machine, but it might help to setup a github and use another machine (or VM) to practise the "distributed" part (understanding origin/master etc).

Don't necessarily worry about learning everything from the get go, I have used it for about a year and there's still plenty of features I haven't ever had to use but make sure you understand what you think you understand.

When you are learning you'll find stuff will happen which wasn't what you expected, at this point you can start googling and will probably end up on a stack overflow page that will answer your question succinctly.

There will be times when you think you have broken everything, you probably haven't. At this point git log and git reset are your friends.


I gave a two hour talk on git, starting from the ground up: https://speakerdeck.com/rmccue/introduction-to-git-plus-usin...

Still working on getting the videos up, but the slides should give a little bit of an introduction, albeit slightly confusing without the speaking.


http://rogerdudler.github.com/git-guide/

And use

Github for a repo, if you want private, get out your CC and pay them a very small amount.

For printable

http://rogerdudler.github.com/git-guide/files/git_cheat_shee...


Or just use bitbucket for free private repos up to teams of 5.


http://gitimmersion.com/ - This is a lab taking you through everything. I found it very helpful when introducing Git to my team mates who had little to no experience with source control.

It's a bit light on concepts but is fairly practical.


Use SVN. There's lack of "svn for people who have no idea how to use svn" tutorials, and when they're published, they tend to be single-paragraph.

I'm not sure git is a perfect solution for any project. Unless you intend to pull each other's modifications, the idea of a central "trunk" and custom "branches" is easy to explain to anybody in 5 minutes.


Isn't pulling each others modifications kind of the point of VCS?


No, most of the time you want to pull from trunk, which everybody agrees on is the most current version, not feature A from Alice, and feature B from Bob and see where you end up.


>> Zero a branch to do something radically different

There's another way to do this:

    git checkout --orphan new-branch


Last time I tried this, I was getting a lot of hassle when switching between branches that have no common ancestor; .gitignored files disappearing completely (very annoying when you keep a local config file around etc.), files left over and "not staged for commit" instead of being removed, etc. Maybe the "empty root commit" trick would help, I haven't tested that they.


You are going to have those issues anyway. Since there is no common ancestor you will have to commit a .gitignore from scratch to that new branch. Files left over is another common issue, if they are not committed then when you switch branches they are going to stick around, just like with any other branch switch...



Curated eh?


You have to admit it's a lot better than the recent "12 git tips written by a finite number of monkeys with typewriters and a finite amount of time".

They're actually pretty good tips, "curated" or ... not - however that would work.


> Making ‘git diff’ wrap long lines ... My git diff would not wrap lines and leave a lot of information hidden from view in my terminal.

Git is so ... stone age!


I think for git diff, this "no wrapping" is a feature not a bug. Git is made for code in general and Linux (as in, the kernel codebase) in particular. If your lines are long enough or your terminal is narrow enough that you need lines to be wrapped, then it may be that your lines are getting too long. The Linux coding style mandates limiting lines to 80 characters and breaking up lines which are longer than this limit. This may seem arbitrary and may not be a great idea in a language which isn't C, but for Linux, this limit allows levels of indentation which are too deep to be more easily seen because when code gets into too many levels of indentation it will become very obvious because several statements end up split across lines. Beyond that, Linus prefers to chop lines over wrapping, and it seems he just put his personal preference into his tool because it isn't that big a deal.


> Search for a string in all revisions of entire git history

The example they show can be shortened by using xargs:

    git rev-list --all | xargs -n1 git --no-pager grep -F "search string"
And here it is as an alias that you can add to your .gitconfig:

    grepall = "!f() { git rev-list --all | xargs -n1 git --no-pager grep -F \"$1\"; }; f"


I was not aware of the `git config` option for wrapping long files, worth the price of admission just for that for me.

Edit: there's a `git diff` tip in there, but this comes up a few times so if anyone needs to do patches from git for svn repos do `git patch --prefix=none $DIFF1 $DIFF2 > some.patch` share and apply with `patch -p0 < some.patch`.


Rather than setting git's pager to less with options, just export the "LESS" environment variable with options you want less to use everywhere.


I don't see that Git has a "patch" command, but svn does -- is that what you meant here? Where you have "git patch", did you maybe mean:

    git diff --no-prefix $OLD $NEW
...where $OLD and $NEW are the two commits you want to diff.


Yeah, my bad!


Or just:

  git diff REV1 REV2 >some.patch
  patch -p1 <some.patch


I found when I was doing that it would occasionally cause problems with applying in svn properly, but YMMV. I've seen some really over the top solutions to it as well.


These seem like the kind of things you'd end up using once in a blue moon. No point in memorizing them.

Am I alone in using git extensions? I've been very happy with it and its ability to display git's "state" right in the window. I usually have one monitor with VS and one with GitExtentions


>> Making a more recent branch the new master

Why don't just use "git reset --hard <sha-of-the-better-branch>"?

At least if the better-branch and master commit in common is the last commit in master so no history rewrite will happen when pushing.


Git is full of nice tricks, this is a good collection. One it didn't mention that I use a lot, is manipulating history with human-readable dates:

  git whatchanged --since="yesterday"
  git revert master@{"1 month ago"}


I saw the words "curated tips" included on the title and promptly closed the page.


I ask the same thing as blhack, I would like a tutorial. I only know how to use the basics, pushing to github and heroku, and I've been using it for a long time.





Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: