What is Git?
Simply put, Git is a free and open-source code management system, i.e. distributed version control system. It allows a group of people to work together, all using the same files. These files are stored in a Git repository, usually in the same directory as the project itself, in a subdirectory called .git.
If you don’t understand what “version control” is, read on.
What is a Version Control System?
Remember instances where you’re editing a word document and the next moment it hangs? All your data is lost. Nobody likes the feeling of having unsaved data vanish into thin air. Just like pushing a “save” button when work is done, version control does this for developers.
Hence, a version control system is basically a software that helps developers track changes to their code.
There are many tools that do this. Examples include Github, Subversion (SVN), and Mercurial. Since I use Github, I’ll only be talking about it in this post. Unlike Git, which is strictly a command-line tool, Github provides a graphical interface.
What do I do with a git repository?
A git repository usually contains:
1. A set of commit objects
2. Heads (which reference commit objects)
Commit objects contain a set of files reflecting the state of your project at a given time. They are usually identified by a SHA1 name, a 40-character string that uniquely identifies the commit object. Think of it as a unique fingerprint.
Commit objects may exist as parent commit objects, which are commits that were edited to produce the subsequent state of the project. Note that every project will have one commit object that has no parent. It’s the very first commit in your project that you created. Hence, there was no predecessor.
A head is a reference to a commit object. You can think of the head as the name given to the current branch. When you switch branches, the head will reference another branch / commit object. By default, there is a head in every repository called master, and there can be multiple heads in a single repository.
*Point to note: a “head” (lowercase) refers to any one of the named heads in the repository; “HEAD” (uppercase) refers exclusively to the currently active head.
Creating your first git repository
You’ll need to create a directory for your project, then go to the directory and create your first git repository.
Congrats! You have successfully created your first .git directory. Now it’s time to add some files into the directory and commit them.
git add .
git commit -a -m [message]
To check what has been committed and what files have been changed, I usually do this:
git diff [file]
Frequently used commands
Before you start using Git, it’s helpful to get familiar with some common commands.
add Add file contents to the index
bisect Find by binary search the change that introduced a bug
branch List, create, or delete branches
checkout Checkout a branch or paths to the working tree
clone Clone a repository into a new directory
commit Record changes to the repository
diff Show changes between commits, commit and working tree, etc
fetch Download objects and refs from another repository
grep Print lines matching a pattern
init Create an empty git repository or reinitialize an existing one
log Show commit logs
merge Join two or more development histories together
mv Move or rename a file, a directory, or a symlink
pull Fetch from and merge with another repository or a local branch
push Update remote refs along with associated objects
rebase Forward-port local commits to the updated upstream head
reset Reset current HEAD to the specified state
rm Remove files from the working tree and from the index
show Show various types of objects
status Show the working tree status
tag Create, list, delete or verify a tag object signed with GPG
I use Gitflow
At Talenox, we use this Gitflow model. It’s been working well for us all these while, and I highly recommend you to try it out. You can read more about it by clicking the link below the image.