- Published on
When to make a new Git commit
When starting out as a software engineer using Git, like clerical work, you're taught the rules of using Version Control, most especially Git. One of the most common rules is
Commit often ...
But you are rarely told:
- what changes to commit independently, and
- when to commit your local changes upstream
Throughout this article, I will list scenarios under which you should commit your local code changes.
- New project
Suppose we are working on an application to greet us whenever we want it to (self love, duh). We gather the tools needed to execute this mega project, i.e the language and framework to use, our favourite IDE or editor plus a cup of coffee or a bottle of water.
In my case, I'll be using Go, VS Code and a bottle of water.
Here, we go!
Create a directory for our project
cd ~ && cd Projects && mkdir greetings \
&& cd greetings && touch greetings.go greetings_test.go
package main
import "fmt"
func GreetMe() string {
return "Hello to me!"
}
func main() {
fmt.Println(Hello())
}
Then, we proceed to write a test for our GreetMe
function in the greetings_test.go
file
package main
import "testing"
func TestGreetMe(t *testing.T) {
got := GreetMe()
want := "Hello to me!"
if got != want {
t.Errorf("got %q want %q", got want)
}
}
Then we perform the usual steps
git add .
git commit -m "initial commit"
git push -u origin master
Yeey! We now have our fully working app ready for deployment.
- Refactors
But then, we realize that our application isn't saying out our names. Let's make some changes to our code. Go to our greetings.go
file.
package main
import "fmt"
func GreetMe(name string) string {
return "Hello, " + name
}
func main() {
name := "James"
fmt.Println(GreetMe(name))
}
Then, let's change our test case, too.
package main
import "testing"
func TestGreetMe(t *testing.T) {
got := GreetMe("James")
want := "Hello, James"
if got != want {
t.Errorf("got %q want %q", got want)
}
}
We can finally make another commit to our remote. In this case, we shall push to a feature branch so that we can make a pull request.
git add .
git commit -m "feat: include name in output"
git push --setupstream origin master
Let me know what your thoughts are about this article hi[at]luigimorel.com. Cheers!