The Crescent

The colour orange is colliding with the walls, museums are turning into food courts. Tomatoes, Potatoes, and glue. A glue to stick and raise the slogan “JUST STOP OIL”. Although I disagree with the…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Recommendations For A Git Beginner

A Gentle Approach To Evolving Your Git Skills

I started using Git several years ago. The sophisticated way in which Git swaps files back and forth “under the hood” (inside the mysterious .git folder) when changing branches still amazes me to this day. In my first job years ago, none of my colleagues used any formal version control systems. Every day, I would make a "just-in-case" backup of my code that was in a good working state before making further changes. Even after I started using Git, there was an initial period of anxiety when I made such backups because I didn't know all the ways of undoing changes with Git and feared a mishap.

I wish I had known about the approaches and opportunities for code management that Git provides beyond the high-level explanations about its importance. While there are a staggering number of tutorials on Git, it is a sophisticated tool that is impossible to learn all at once while also trying to focus on the application you’re developing.

This is not a complete tutorial on Git but a sequence of stages to gently evolve your knowledge and use of Git. Had I known these stages beforehand, I would have stumbled less along my journey with Git. In this article, I’ll outline how to evolve your usage of Git starting with a one-person project.

In any complex software project, writing code involves adding files, making changes to files, undoing changes, sometimes redoing changes, comparing files between branches, and making sure you don’t lose any work along the way.

If you’re worried that you need to completely understand how commits get stacked up, how merges and rebasing works, and the nuances of manipulating the HEAD pointer, well, you don't need to know these things to get started using Git.

The first step is to start using Git for projects where you’re the only author. Besides code, you could use Git for backing up your text-based notes or documentation. (I use Git for my entire personal blog.) At this stage, think of Git as a backup mechanism. All you need to know is how to initialize a new Git repository, add and commit files to it, and push it to the remote.

At the next stage, start using the commits in a meaningful way. Get a list of commit messages by typing git log. For me, this is a useful reminder about where I had left off on a project that I may not have worked on for a while.

As you work on your code, the ability to see the differences between the last commit and your current uncommitted changes will be invaluable for debugging. Typing git diff will show the changes in every file.

To see changes in a specific file, type:

To see the list of files that have changed (but not the changes themselves), use:

You can also use git show <commit-hash> to see the changes made in any given commit.

I also use the graphical tool called gitk to step through the changes made at each commit using gitk <filepath>. I don’t use gitk for much else.

Sometimes I’d mess up a file to the point where abandoning all the changes seemed wiser than to continue debugging the file. In such a situation, as long as you have not yet committed the file, it’s easy to undo all the changes in the file with:

This approach is more reliable than depending on your code editor’s undo capability. The more sophisticated and disciplined I became with git, the occurrence of such bad messes became fewer and far between.

For undoing a commit, there are a plethora of ways depending on how you want to manipulate git history (which you shouldn't worry about at this stage). A simple and effective approach for undoing a commit is:

With the above method, you avoid performing forced updates to the remote. (Forced updates have undesirable consequences if more than one person is using a branch.)

Always use a new branch to add a feature to a working codebase. Be disciplined about this even if you’re the only author. You’d keep your current working copy of the code in the main branch. (I’m using the branch name main rather than master as the default branch similar to GitHub). And then create a branch off of main to add a new feature:

Once you’ve tested your new feature, merge the feature branch back into main with these steps:

In a one-person project, you’d never have merge conflicts. Hence this will always work even when using different computers to access your repository. It’s usually a good idea to keep the feature branch around for a short while. Delete the branch locally and on the remote when you’re ready with:

Once you start using branches, more sophisticated ways of undoing changes become available. When things are completely out of control and you have lots of changes already committed, you can abandon the branch (and no one will ever know!):

And start back again with a new feature branch off main. Sometimes, it's good to save the branch you want to abandon for a little while as you might be able to salvage a file or two and add it to your new feature branch. This can be done with:

The above retrieves the file good-file from the branch-being-abandoned into your new feature branch.

Then there is git reset to manipulate the HEAD pointer which you can explore further until your head hurts. But here's a basic use case that will come in handy. Let's say you've made changes to multiple files, performed a commit, and pushed it to the remote. And now you're sorry you did all that and want to abandon this last commit. Do this:

If you need to compare the main and feature-xyz branches:

The above will show the differences in every file between these branches. And the output can be voluminous if there are lots of files with changes. I often want a list of files that have changed, which I obtain with:

After that, I can view changes in a specific file between the branches with:

There are many great graphical tools and editor extensions that help with using Git in various ways. I mentioned gitk earlier. Tools try to abstract away the complexity of Git while introducing ambiguity about what they are doing under the hood. My preference is to do almost all Git related actions on the command line. Otherwise, I think it's difficult to truly understand some of the fundamentals.

I use graphical Git tools to support visualization. A couple of key examples:

I hope the gentle stages suggested in this article help those at the early stages of their Git journey and those contemplating starting one.

Add a comment

Related posts:

This Teachers Day Amazed your teacher with online cake.

Teachers Day will be held on the 5th of September, Where we present our gratitude towards our teacher in order to respect them And while honoring them we tend to give some gifts in terms of heartfelt…

From Prototype to Work of Art

You have a prototype working with simple objects and it feels just as you envisioned. Now it is time to add some art and get the look you want. I will show you how to take a 3D object and turn it…

Making Good Metrics

We all know we need metrics, but to be useful, the metrics need to be comprehensive, consistent, and reliable. Above all, they need to tell us something we need to know in order to make a decision…