Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm getting used in working with git at the moment. And I'm touching an issue that is unclear to me.

I feel compelled, if I think of commiting after a change and making a documenting description. I don't know exactly when I should commit, because that's very relative and there's always something under construction.

I tend to commit, when I fear to hose something. But then the summaries look like "Backup before trying this and that" or just "Backup".

What do you do? Is there a better way, or is my practise common?

share|improve this question

migrated from stackoverflow.com May 13 '12 at 9:16

This question came from our site for professional and enthusiast programmers.

8 Answers 8

up vote 7 down vote accepted

You can do whatever is socially acceptable with your fellow programmers on the project you are working on. There are no rules really.

Personally I commit very often to keep change-sets really small and make sure they only do one thing, and then may or may not rearrange / squash those before publishing to the team.

When you realize you have git rebase -i to hand, it becomes less important how often you commit initially.

In terms of comments, I think it can be important that your commits are well described so that you can manage the changes you are working on.

share|improve this answer
2  
+1 for rebase. It makes commits "your own business" and you can present them however you want when pushing to the main repo. –  Tamás Szelei May 13 '12 at 10:57

The wonderful thing about git is that for the most part it's like having a paper notebook, the notes in it aren't shared and for the most part are useful(ish) to you alone.

That means you're free to commit over anything, I'll commit things like "tweaked x by 3px", totally useless to the overall goals of a project but then I'll rebase it in so the commits are clear and in a nice block. Plus with how easy git makes local branching and merging it encourages you to commit often and just manage the change set you want to apply, versus for example SVN where a commit has to be thought about as generally it's going out to every other developer straight away.

share|improve this answer

You can take practical advice from big long-running open-source projects, like Chromium, there you send code for review first, so you can discuss your changes early and other engineers can point out your mistakes. When everyone agrees code is 'good enough', you can commit it.

This solves the backup problem (you have a separate branch on your machine, and code review server typically allows to download patch), and leads to better code in general.

share|improve this answer

First off, like others I'm assuming you're working on a local branch and that you have a master somewhere else.

I think a DVCS gives you a huge amount of freedom. I try to commit each time I make a semantic step forward and the code compiles. If I rename a variable, I commit. If I improve a comment, I commit. The advantage of a very fine-grained commit structure is that when you push to master there is a bundle of descriptive commits.

I tend to commit, when I fear to hose something. But then the summaries look like "Backup before trying this and that" or just "Backup".

Again, a DVCS gives you freedom. I wouldn't say "backup before trying flux capacitor", I'd say "written flux capacitor". If it turns out that this doesn't work, I'd back that particular change set out. You have the freedom to do this because you only have to worry that your changes break other people when they either pull from you or you push to the master branch.

share|improve this answer

Source control is a bit of a personal thing. Typically if you work with a group they will have their own procedures regarding when to commit and what your commit messages should look like. Typically you commit AFTER you complete some task, not before. Remember, you want to be able to access the most up-to-date code possible in case you lose your data.

Here are a few rules of thumb:

  1. You should always, to the best of your ability, leave the commited code in a runnable state. Do not submit anything unless you tested it.
  2. The commited code should be as up-to-date as possible without violating rule 1.
  3. You should try to keep commits as small as possible. If you fix three bugs, it's best to have three separate commits (one for each bug). That way you can rollback one small change at a time. It also makes it easier to find where you introduced mistakes
  4. Commit messages should be meaningful. Always write like someone else will read them (because you won't remember what you did a week after your commit)

Obviously, sometimes (esspecially when you're beginning a project) it's hard to follow these rules. Either the code isn't complete enough to be run or your changes overlap and it's too difficult to separate them. Use your own judgement.

share|improve this answer
    
Thanks, you mentioned some good points. Some things are just hard for me to compy with at the moment, because I have too less routine in this mental tasks and I find it hard to getting started with it. –  rynd May 12 '12 at 15:52

Commit when you've completed a cohesive "unit of work". That could be fixing a typo in a comment, adding a new module to a project, adding some logging statements, writing a new unit test and the code to make it pass, or anything else that's easily understandable as a discrete change to a project. The key is "easily". Commits should be narrowly focused and able to be fully described in the commit message. The commit message is sentences or paragraphs, not books. In general, I'd say if you go more than an hour of solid coding without committing, you're doing something wrong.

share|improve this answer
    
"fixing a typo in a comment" - So like in Wikipedia, somehow. :-) +++ I think I should train me to have the courage to revert. –  rynd May 12 '12 at 16:12

Git is wonderful and scary because of it's flexibility. While there is a lot of advice that will probably be given on this thread, I think the best advice that I can give is to keep two branches of your code:

1) master

2) stable

I commit like crazy on my master branch. There is no guarantee of any functionality on it. There is also a desire to have a version of your code that is stable... that's where the stable branch comes in. Every time that you have a stable version of your code, you can merge your changes back in to the stable branch.

Depending on how big the project is, I might have several branches from the master. I'll have one for each bit of functionality that I'm working on and I can merge the changes back in when they're stable.

So long story short. In git you don't have to choose between committing often and having stable commits. You can do both! I like to use gitk or git-extensions to view my branches. It helps me remember where I am and what I'm working on for each project.

share|improve this answer
    
That's a great approach! +++ Why do say merge to the stable branch and not commit? I thought the version you have got, when you debugged much, is a stable one which can directly be commited to the stable branch. +++ What I read according to descriptions from your answer is: Just don't get confused by different natures of descriptions. –  rynd May 12 '12 at 15:36
    
Branches in source control, just like in a tree, are separate. Your stable branch isn't actually like a flag you put up at a certian point in time were you say "This is the stable version!". It's a completely separate copy. That means when you want to update your stable branch you need to MERGE the changes from the master branch over, just like you'd need to merge your code changes with the code changes of your teammates (several copies merging into one). –  acattle May 12 '12 at 15:39
    
So the master branch is a construction area where you test sections of functionality? –  rynd May 12 '12 at 15:56
    
Would you agree to this, acattle? (If so, I'd say, you should carry over the code from stable to master from time to time.) –  rynd May 14 '12 at 15:19
    
The idea that you should only commit when things are stable comes from a centralized versioning system like svn. In svn, when you commit you are forcing your code on everyone else. In git, this is the "push" command. Likewise, in git you should only push stable code. –  qwerty9967 May 15 '12 at 12:55

Whenever I start a project, I make a bullet point list of the different components that need doing, and often that includes subcomponents. It doesn't have to be on paper, it can be in your mind (even if paper is easier to share than thoughts). I always commit when I complete a component that took "some effort" and is something worthy of showing someone, even if that is a fellow developer and not a client.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.