Git - The Bare Bones

Early in my "learn to code" journey, I discovered the work of Tania Rascia. I found her fresh and inspiring. Her clean and clear explanation style helped me gain insight into important concepts.

This article today is inspired by some of her work. I'm going to write a little about git.

Git is a version control system, mostly aimed at software development, but you can use it for anything where you want to track changes and undo any foot wounds inflicted on yourself. Git was built by Linus Torvalds in 2005. You can read a short history of it here. I'm not going to cover GitHub in this article. Git is the system for version control, whereas GitHub is an online hosted repository for hosting and sharing code "pushed" to it from a local project on your computer.

Alright, let's git into it and give you the bare bones...haha, see what I did there? I know...terrible...

What is Git?

As I said above, Git is a version control system. Teams benefit most from using it. A group can collaborate on a code base, and by building on "branches", individuals can work together without stepping on each others toes. Git does the heavy lifting of tracking changes and history of coding projects. While teams benefit most, anyone can add it to their coding workflow.

Installing

I'll refer you to Tania's article on Git, linked in the references.

Initial Steps

Let's start a Rust program, initially with no support from Git, then we'll add it. In fairness, the cargo build tool configures a project for you automatically with git support, but frequently I like to ignore that and git going (yes, I need to stop) after the initial setup is complete. Do this wherever you want to work on your code:

cargo new --bin --vcs none project-with-git

A new Rust project that contains a skeleton "Hello World" executable program will be created for you. The --vcs none flag tells Cargo not to do any git setup.

Initialization

Change into the directory of the project you just created, cd project-with-git and type: git init. This will initialize a repository "repo" for you and create a .git directory which holds the configuration and instructions for how the git will manage the repo.

What to Ignore

Next, type: touch .gitignore. Open the project in your editor of choice, for me that's VS Code, and you should see the freshly created .gitignore along with the other project files. Git will track everything in your project if you let it. That's great mostly, but you don't need to track anything in the /target directory, which contains build artifacts and other internals generated by the Rust compiler. Edit your .gitignore to look like:

# gitignore for project-with-get

/target

The syntax of a .gitignore is pretty straightforward:

  • anything preceded by # is a comment
  • exclude directories by doing </directory_name>
  • you can use wildcards, i.e. Shuttle*.toml

Adding Files to Track

To add the files you do want to track, type: git add . This will add everything not excluded by .gitignore into git's list of what to officially keep track of.

Committing

Once you've added the files, changes aren't tracked until you actually "commit" them. To do that type: git commit -m "Initial commit" This will create your first commit into the history with the message "Initial Commit". Well done! You've offically begun tracking the history of this project. You're project lives locally on your computer at this point, it's not connected to the internet and you are the only one who can work on it.

Branching

After the above, youi'll be on the "main" branch of your project. Let's say you've worked awhile, but change your mind and want to explore another direction, without losing what you just started. You can make a branch.

To create a branch, type: git branch <new-branch-name>. This will create a branch in which you can work on a new feature, a bug fix, or just explore a different direction, but not lose your original work. Branches have to be "checked out" when you make them, by doing: git checkout <new-branch-name>.

Follow the sames steps as above to commit your branch into the tracking sytem. You can go back to main by: git checkout main. This will switch you back to your main branch. If you look, you'll see that any changes you just made are not in this main branch.

Merging

Let's say you end up liking the approach you took in the fresh branch you just created. When it comes time to unite the two branches do this: git merge <new-branch-name> this will "merge" the new code into the old and unite it under the main branch. You can then push out to production and the new feature or bug fix will be incorporated.

Pretty nifty hey?!

Conclusion

There's really no reason not to use git. It makes your software development life much easier. If you make a mistake or what to start over, by leveraging the branching system you can simply "switch back" to an earlier point in time. It's really a no-brainer.

In this short piece I've given you the basics to get started. From here, you can practice on your own and explore the links I've provided below to learn more. Remember, cargo initializes a git repo for you, you don't need to go through the steps above every time.

References