Git 101 — Part 1

A practical introduction to git basic concepts.

Lorenzo Felletti
Analytics Vidhya

--

Photo by Yancy Min on Unsplash

Git is a DVCS: Distributed Version Control System.

Basically, with git you and your team can cooperate to create, modify, extend, maintain code working in parallel on the same codebase, without worrying about “breaking” the code.

When you want to develop a project using git you create a repository — a directory containing your project’s code and a .git subdirectory used by git itself to keep track of everything.

Note: repositories are often called repo for brevity.

The command to initialize a folder is:

git init 

to initialize the current folder, or

git init <directory_name>

to initialize a specific directory.

Keep Track Of a File

Git doesn’t automatically keep track of files in the repo, you have to tell him that you want it. To do this, use the command:

git add <filename>
git add <directory>

Note: adding a directory to git doesn’t — again — mean git will add the files contained in the directory, you have to do it later or use a command like:

git add <directory>/*

to automatically add all the files contained (in that moment) in the directory to that followed by git.

To add all the files you’ve created use:

git add .

Commits

The fundamental unit-of-change of git is the commit. A commit is a snapshot of your repo at a specific point in time. To be precise, with a commit you’re not forced to “save” all the changes you’ve made. When committing, you select the files you want to be committed.
To make a commit use the command:

git commit <files blank-separated> -m 'what changed'

The -m option is a shortcut to add a short description (message) of what are the changes present in the commit. By default — and it is a good practice — you must provide a message to the commit, but you can omit it using:

git commit <files> --allow-empty-message -m ''

A useful shortcut to commit all the files that have changed:

git commit -a -m 'message'

Part 1 Conclusion

I think this is enough for the first part of this series, and for you to start experimenting with your projects locally. Don’t worry if, at first, you feel disoriented, or you don’t understand the mechanism behind it well enough, or experience other kinds of problems, it is normal. With time and experience, you will improve.

In the following articles, we will go more in-depth with the commit and git mechanism, we will introduce the basics of branching and merging, and later also how to work in a distributed environment using GitHub.

Thanks for reading!

--

--