tags:

views:

68

answers:

3

I've been using Git as source code management tool. For now my workflow is simple:

  • think about a minimal new feature/refactoring
  • code it
  • review the diff with HEAD
  • commit

I would like to improve this workflow, by making it more flexible. The reason is that I've encountered the following situation a couple of times, which I think is a typical one:

After a commit, I'm starting to modify the working directory in order to add a new feature.

I code for a while, let the state of the working directory be A at this moment. The important thing here is that is state A the working directory is not ready to be committed, because the new feature is not completed yet.

At this point I realize that it would be better to perform a refactoring first. Admit it, sometimes the need of a refactoring becomes obvious in the process of code modification.

Now I need to start again from HEAD and do the refactoring first. But I don't want to lose the modification of code HEAD -> state A. So basically I need to:

  • do the refactoring in a copy of the working directory that is not altered yet after the last commit
  • commit it
  • merge the work on the new feature that I've started and stopped it when the working directory was in state A
  • complete the work on the new feature
  • commit it

I believe this is where branching can help me, but I'm having issues figuring the correct git commands. I'm confused by the fact that I need to create a branch when the working directory is altered after a commit and I don't know the correct way do deal with this.

What's the correct way to describe the above workflow in terms of Git commands?

+2  A: 

You want git stash. When your working directory is in an intermediate state that you are not ready to commit, run 'git stash' or 'git stash save "some text"'. This will take your working directory back to HEAD. Now make a change and commit. Then run 'git stash pop' and your previous intermediate work will be merged into the working directory. You can have multiple stashes; list them gith 'git stash list'. (The 'some text' will identify them. If you provide no message, the last commit log is used.)

See also 'git add --patch' and git add '--interactive'

William Pursell
A: 

git stash is what you're looking for, I believe.

Bernard
+2  A: 

When you start a new feature, create a new branch, and do work there. When you think you need to refactor code that you're not working on, switch back to master (or another dedicated branch) and do it. When you're done, merge the refactoring branch back into master. Then rebase your feature branch on top of the updated master.

sykora