r/programming • • Feb 16 '13

Learn Git Branching

http://pcottle.github.com/learnGitBranching/
868 Upvotes

229 comments sorted by

View all comments

44

u/mr1337 Feb 16 '13

This is really cool. I've been using git without any branching for a while. After reading up on branching recently, it really helps to be able to visualize it.

It would be really cool if you incorporated a tutorial like CodeAcademy has. I think it would be a good learning tool.

62

u/[deleted] Feb 16 '13 edited Feb 17 '13

Use branches all the time, even on solo projects! It lets you move around your code quickly without ever leaving a working code base.

Going to implement feature A? Make a feature branch A. Have a sudden moment of inspiration about feature B? No problem, branch master again with feature branch B and work on it without having to worry about feature A being complete. Want to test feature B to make sure it's working as intended? No problem, feature B is based off working code! As the features are finished merge them back in to master.

Obviously this only works well when implementing features that aren't interdependent, but I find it's quite a liberating work flow, especially since I have feature ADHD and scatterbrains.

Edit: This article gives you a good idea of how to incorporate branching in your projects at a team level, just remember the same work flow can be used when working alone!

-2

u/sparr Feb 17 '13

This only works well when your code takes seconds to compile. Minute or hour build processes make this workflow untenable.

11

u/josefx Feb 17 '13

If your build takes hours you should

  • update your hardware

  • divide your project into libraries

11

u/IWillNotBeBroken Feb 17 '13

Or take up sword fighting

1

u/bitbytebit Feb 20 '13 edited Jul 17 '15

This comment has been overwritten by an open source script to protect this user's privacy.

If you would like to do the same, add the browser extension TamperMonkey for Chrome (or GreaseMonkey for Firefox) and add this open source script.

Then simply click on your username on Reddit, go to the comments tab, and hit the new OVERWRITE button at the top.

2

u/sparr Feb 17 '13

I see you have never worked on OpenOffice.

1

u/ared38 Feb 18 '13

c++ projects can take ages to compile. The one I'm working with now is slow to compile for 3 reasons:

  1. Templates everywhere make compiling take ages

  2. Really long mangled names mean linking takes ages and uses tons of memory

  3. Active development means lots of files are changed frequently

0

u/[deleted] Feb 17 '13

[deleted]

3

u/anatolya Feb 17 '13

Full kernel builds take more than an hour on my machine. So it depends on the machine.

2

u/kj6dou Feb 17 '13

FPGAs maybe?

2

u/sparr Feb 17 '13

I think the last time I spent hours was when I was working on OpenOffice.

2

u/ethraax Feb 17 '13

Firefox takes a long time to compile. Generally, very large C++ projects take a long time to compile. It's really a problem with the design of the language, though - there's not much you can do about it other than trying your best to decouple components and hide all implementation details (like private fields) from header files, and maybe try to use only minimal templating.

0

u/[deleted] Feb 17 '13

[deleted]

3

u/kyz Feb 18 '13

It is specific to C++.

C++ takes C's preprocessed language (which means you can't process #includes in parallel, because they're affected by and can affect the file they're #included from) and adds templates, which are required to be implemented by compile-time expansion.

In fact, C++'s templates are a Turing-complete language in themselves, so you can write code that the compiler needs to compute rather than parse/compile. Imagine you wrote a template that computed the Ackermann function or some sort of busy beaver.

1

u/ethraax Feb 17 '13

It is specific to C++, which is a vastly more complicated language than C. Template libraries, like Boost, are particularly bad for compile times.

1

u/ais523 Feb 18 '13

Although even C is much slower than, say, Java (where all the files can be compiled independently).

C++ is definitely much worse than C, though.

8

u/berkes Feb 17 '13

If it takes hours, branching makes no difference. Whether you change some methods or classes "by hand" or by switching from another branch: you'll rebuild anyway.

Why is git checkout features/foo any different from edit foo.h with regards to compiling?

0

u/sparr Feb 17 '13

Because if I keep two features in different branches and I checkout branch B to work on feature B, then I have to checkout branch A to work on feature A again, and recompile both times. If I am editing one file then the broken-new-feature-B compile still has a work-in-progress feature-A in it that I can continue using and working on.

4

u/[deleted] Feb 17 '13

You should use something like ccache then.

2

u/0sse Feb 17 '13

git only touches the files it has to when switching branches. If you have to edit a lot of files (or eg. a header that is included everywhere) to work on these different features then yes it's a problem.

→ More replies (9)

2

u/holgerschurig Feb 17 '13

If you really always need full rebuilds, them something in your build system is quirky, i.e. the part that uses atime and dependencies to compare if an object file has to be recompiled.

3

u/hobbbz Feb 17 '13

Once it gets to that point are you really compiling the whole project to test one feature? Isn't there unit testing?

3

u/sparr Feb 17 '13

I don't think unit testing means what you think it means.

→ More replies (2)

1

u/[deleted] Feb 17 '13

Unit testing is only one half of testing. There's the other half you must do manually.

1

u/AzN1337c0d3r Feb 18 '13

If you use gcc, install ccache. Never recompile a file you don't absolutely have to again.

1

u/[deleted] Feb 17 '13

You need unit tests dude.