Git is GPL version control system used primarily for distributed development of projects, even ones with large amounts of data. The important part here is Version Control System — it means that it’s a software that track changes in files and compare different versions, and do many other nice things, like going back to previous versions of a certain file. Every working directory in Git is a full-fledged repository which has a complete history. Therefore in git one can work without a network connection and sync his/her changes when the network is there.
Requirements
Following items are required for this tutorial:
- A server or VPS running Debian 8
- A root or sudo user account
Installing Git
Before you install Git, make sure that your system is updated by executing the following command:
1 2 |
sudo apt-get update sudo apt-get upgrade |
Now install Git with following apt-get command:
1 |
sudo apt-get install git-core |
This is the only command that is needed to install Git. The next step is to configure Git.
Configuring Git
Git implements version control using mainly two settings:
- A username
- The user’s email
This information will be used in every commit one make with Git so it can track who is making which commits. All these setting are stored in .gitconfig in your home folder.
So we need to add these two settings in our Git configuration file. This can be done with the help of the git config utility or we can edit directly .gitconfig file. Let’s first do it by git config utility. Here’s how:
Set your Git username:
1 |
git config --global user.name "Davinder Singh" |
Set your Git email:
1 |
git config --global user.email davinder.singh@example.com |
View all Git settings:
You can view these newly-configured settings (and all the previously existing ones, if any) using the –list parameter in the git config utility.
1 |
git config --list |
You will see your user settings:
1 2 |
user.name=Davinder Singh user.email=davinder.singh@example.com |
If we want to get our hands dirty with the Git configuration file, simply fire up nano and edit .gitconfig content:
1 2 3 4 5 |
nano ~/.gitconfig [user] name = Davinder Singh email = davinder.singh@example.com |
This is the basic configuration one need to get up and running with Git. Adding your username and email is not mandatory, but it is recommended.
Congratulations on your very own Git installation.
Here is a simple guide for getting started with git, no deep shit.
Create a new repository
Create a new directory, open it and perform a
1 |
git init |
to create a new git repository. The git inits command creates a new Git repository. It can be used to convert an existing, unversioned project to a Git repository or initialize a new empty repository. Most of the other Git commands are not available outside of an initialized repository, so this is usually the first command you’ll run in a new project. Executing git init creates a .git subdirectory in the project root, which contains all of the necessary metadata for the repo. Aside from the .git directory, an existing project remains unaltered.
Check out a repository
To create a working copy of a local repository by running the command
1 |
git clone /path/to/repository |
when using a remote server, the command will be
1 |
git clone username@host:/path/to/repository |
Workflow
Our local repository consists of three “trees” maintained by git. the first one is our Working Directory which holds the actual files. the second one is the Index which acts as a staging area and finally the HEAD which points to the last commit we’ve made.
Add and Commit
We can propose changes (add it to the Index) using
1 2 |
git add <filename> git add * |
This is the first step in the basic git workflow. To actually commit these changes we use git commit -m “Commit message” Now the file is committed to the HEAD, but not in the remote repository yet.
Pushing changes
Our changes are now in the HEAD of our local working copy. To send those changes to remote repository, execute
1 |
git push origin master |
Change master to whatever branch you want to push your changes to. If we have not cloned an existing repository and want to connect our repository to a remote server, we need to add it with
1 |
git remote add origin <server> |
Now we are able to push your changes to the selected remote server
Happy branching!