Git tutorial and basics
|DIFFICULTY||Basic - 1 | Medium - 2 | Advanced - 3|
|TIME REQUIRED||15 min|
|RELATED PRODUCTS||Linux-based VPS or dedicated servers
Linux-based shared hosting
Git is a version control system that enables developers to better manage their repositories of code during web development projects. Git is especially adept at branching, staging and distribution of code, as well as ensuring the integrity of a code base.
One of the most powerful and somewhat unique features Git provides is the ability to work with multiple, completely separate branches of your code without affecting other branches. Development can occur within a unique branch outside of the master branch and be later reintegrated.
Another fairly unique feature to Git is the intermediary "staging" area where files (or even chunks of code) can be stored before commitment into the repository. This enables multiple edits within the same file to be staged (and therefore committed) on separate occasions if necessary, without the need to commit the entire file after every change is made.
Although not unique to Git, as a distributed version control system Git allows virtually any workflow that may best serve you or your team or clients. Git has no built-in requirement for central repositories, making it particularly suited to open source projects and other workflows that benefit from a widely spread developer network.
Every object that passes through a Git repository passes through a checksum verification process, both when files are entered in or taken out. This ensures that no part of Git repository can be altered without changing the commit ID, providing assurance that a commit always remains identical to when it was entered.
Let's get started.
Installation on Ubuntu/Debian
$ apt-get install git
Installation on Fedora/CentOS
$ yum install git
While the detailed information can be found within the official Git documentation, getting started with your first Git repository is very simple and only requires a few minutes to begin.Once Git is installed, begin by navigating into an empty directory that will be your root directory for your project. For this example our project will live in the
root@Main:~/dev/foo# mkdir ~/dev/foo
root@Main:~/dev/foo# cd ~/dev/foo
Once inside your project's root directory, enter git init to generate a new Git repository:
root@Main:~/dev/foo# git init
Initialized empty Git repository in /root/dev/foo/.git/
Once you've added a file to your project directory, you need to tell Git which files should be entered into the repository and tracked by Git. Here we're adding a data.xml file to the project directory, then using the
git add [file] command to tell Git that we want
data.xml in the system:
root@Main:~/dev/foo# touch data.xml
root@Main:~/dev/foo# git add data.xml
One of the most common tools in Git is the git status command, which provides information on the overall commitment status of your project. In this case after adding our data.xml file to Git, we see that the file is waiting to be committed:
root@Main:~/dev/foo# git status
On branch master Initial commit Changes to be committed: (use "git rm --cached
..." to unstage) new file: data.xml
After adding files to the repository the next step is to create a commit, which can be performed easily with the
git commit command:
root@Main:~/dev/foo# git commit
If this is your first time using Git on this machine you may encounter a warning upon attempting to commit regarding setting your git config user.email and user.name values. If necessary, follow the instructions on screen to set these and retry the commit command.
Once the git commit command is issued, you will typically be presented with a text editor screen in which to edit the commit message. Enter an appropriate message and save to finalize your commit:
Added the data.xml file. # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # On branch master # # Initial commit # # Changes to be committed: # new file: data.xml # [master (root-commit) ce19fd5] Added the data.xml file. 1 file changed, 1 insertion(+) create mode 100644 data.xml
Alternatively, you can add the -m flag to your git commit command, which allows you to enter a commit message directly in-line rather than through the additional commit message file:
root@Main:~/dev/foo# git commit -m "Added the data.xml file."
[master (root-commit) ce19fd5] Added the data.xml file. 1 file changed, 1 insertion(+) create mode 100644 data.xml
While this tutorial should give you a solid grasp on what Git is and how you can potentially use it to improve your version control workflow, we've just scratched the surface of what Git can do and how you can take full advantage of its capabilities.