← Back to resources

Git Will Change Your Coding Life And You Only Need Two Commands

Git Development Best Practices
Brendan O'Neill

A practical guide to using git for code generation workflows - you only need to know how to commit and reset code, it's that simple.

If you aren’t using git, stop developing and invest 30 minutes in learning the basics today. I guarantee you’ll see a full ROI within a few days.

Git might sound complicated, but the thing is, to ‘glide code’ you only really need to know two commands: commit and reset your code.

*Well, technically it’s three commands, but you only need to learn how to do two things.

1. Committing Code

‘Committing’ code essentially saves your codebase at a certain point in time. So when you’re happy with your code, you create a commit with two really simple commands:

bash
git add . // Adds all changes to the 'staging' area
git commit -m "Message describing the commit"

2. Resetting Code

‘Resetting’ your code instantly drops all the code that you’ve written or generated but haven’t yet committed. In the long run, this command is going to save you countless hours of your time.

bash
git reset --hard

You might not believe me just yet, but you soon will. Here’s why:

  • Agents are prone to getting into error loops - when this happens, it makes it difficult to get back because as more wonky code is generated, it becomes part of the prompt. A lot of the time, from observing these loops you get a much better idea of what’s going on. Resetting and giving a more detailed prompt will do the trick much faster than trying to prompt your way backwards
  • You can experiment risk-free. You think a transition will look good, you prompt to add a transition, it’s not great, you test it out a bit more, you’ve updated 3 files now trying to get this working and you finally get it working but it’s not as good as you thought - git reset --hard, no need to backtrack. Minimal time wasted.

Using Git

As you use these simple steps in your development workflow, the use cases will become very clear, very quickly.

Here’s a hypothetical example - you’re building a TODO list app and you want users to be able to:

‘Set an item as high priority’.

So think through that - you want to:

  1. Add a ‘High Priority’ Button
  2. Update the item to show it is ‘High Priority’ when the user clicks the button
  3. Move ‘High Priority’ items to a new list at the top of the screen
Prompt

Add a button that is a flag icon on the right hand side of the TODO item so the user can set the item as a priority of “HIGH”

Adds button - you’re happy

bash
git add .
git commit -m "Add High Priority Button"
Prompt

Create a separate list shown at the top of the screen for ‘High Priority TODO items’

Adds list - you’re happy

bash
git add .
git commit -m "Add High Priority List at the top of the screen"

Looks great when it’s static and you know you’re happy. But now you want to add a nice transition.

Prompt

When a user sets an item to high priority, have a nice transition that moves it from the normal list to the ‘High Priority’ list.

You’re not happy, it’s a bit jumpy, but you think about it and you realise how you want it to work.

Prompt

No, that’s not really working, it’s a bit jumpy. Make the item slide out of the ‘normal priority’ list from left to right and then slide in to the ‘high priority’ list from left to right.

It changes some code, but follows a pattern from the previous prompt but it leaves you with something that is a weird mix of the original jumpy transition and the newly requested sliding one.

So…

bash
git reset --hard

This will reset your code back to your last commit. Now we’ve gotten rid of the code that has spiralled and gone a bit wonky and we’re back to our nice clean code that we’re happy with.

Open a new agent. Copy and paste the file for context and then prompt.

Prompt

When a user sets an item to high priority, make the item slide out of the ‘normal priority’ list from left to right and then slide in to the ‘high priority’ list from left to right.

Perfect. This has a much better chance of coming out as you planned. You experimented, got a better understnading of what exactly you want and it was super simple to get back into a position to give a more detailed prompt without having to regenrate the things you were already happy with; the button, the feedback, and the list!

So finally, commit your finished feature.

bash
git add .
git commit -m "Add transition for high priority item"

This might slow you down a little bit at first but overall you will save A LOT of time, especially when you start running into errors!

Takeaway

Get into the habit of committing code early and often.

As you get further into your code it becomes more and more important as the codebase gets bigger and the generated code gets more unstable and unreliable.

My Commits For The ‘Callout’ Component

If you see the ‘Callout’ component in this article it’s used for the ‘Prompt’ box and the ‘Takeaway’ box above and the ‘Next Steps’ box at the end.

I initially created the ‘Prompt’ and ‘Takeaway’ components at different times, so they were generated as separate components even though they were essentially identical - just with different colours and icons. Here are my commits showing how I brought them together into one single ‘Callout’ component.

TimeMessage
15:40Create Callout component
15:41Convert Prompt and Takeaway components to Callout component and delete components
15:43Add Next Steps as callout type

So this is the process, and might be a bit overkill for the article but every time I commited, it took about 5 seconds and it meant I could easily revert to a previous commit if I needed to.

Next Steps
  • Branching, using branches and merging
  • Using commits to think thorugh a feature

Articles to come!

How did you find this resource?

Share this resource

Join the newsletter

Get the latest resources and insights delivered directly to your inbox.