My typical Git workflow.
Git is a powerful version control system and one of my favorite tools for a long time.
My idea here is not explaining the basic concepts about Git but just to show how I typically use it in my daily work. What I will illustrate here is also known as the branching workflow.
So, let’s start creating a hypothetical project to list, for example, the products of a shop.
Just to have a start point, let’s create a new app.rb file, init our repository and commit it to the master.
I will do here some very simple Ruby code just as example.
Let’s edit our app.rb:
then save it, and add to our repository:
Look to the message I used on my first commit here.
"Initializing the main app file to handle our requests using the ruby framework Sinatra version 1.0.1"
It’s a meaningful commit message and can tell a lot about what I’m doing at this point on my project.
There’re some things I like to think about and answer when I’m writing a commit message:
- What I’m doing.
- Why I’m doing it.
- What’s next.
Try to avoid messages on your commit like
adding a new app.rb file. It’s tell nothing about your project history and will not help your teammates.
To create my branch, I use my initials as the prefix to naming it.
Since we’re now at the new
tf-new-products-feature branch, then let’s add more some code to our app.rb
If you want to run the code above, just install the gems
gem install sinatra sinatra-contrib --no-ri --no-rdocand run
ruby app.jsonon your terminal.
After saving our file, we’re ready to commit the code by running a git commit like:
And it’s should works fine.
Now my feature is completely isolated from the master branch, and I can switch over my master and others branches to work on some others tasks.
If you want, you can check it on your terminal:
Sometimes I also want to keep an unfinished code where I’m still working and mark it as a “WIP” on my commit message. Also, it’s good to describe where I stopped and what I need to do next, like for example “WIP: created the endpoint to the new products. Needs to integrate to some DB.”
Let’s see it in practice editing again the app.rb file.
If I run a
git log --oneline now, I will see my commits history
If you have some remote repository, like on Github, you can send the branch to your remote repository by running:
It’s also a good idea to ask for feedback to the others teammates if you need some help on your
WIP branch using a PR on Github. I did it many times and was always nice to get some other point of view and suggestions.
So, after got everything done, probably I won’t need to preserve the
WIP commit on my history, and to discard or edit the commit message I will use the rebase in the interactive mode to squash or modify the commit message.
Let’s try for example to squash and also discard that commit’s log message.
It’s should open to you something like:
To do what we need here, we just need to change the second line to
and then save and close the editor
If everything goes fine, then we should see something like:
And now, in our history, we will have only the previous message with that
WIP discarded from our log history.
and on the app.rb, our work done before are also there:
So, after everything done, probably we can merge our work into the master.
And then, delete our branch locally and also remotely.