Setting up git for projects

What is git and why do you need it? Git is standard for distributed version control system versions used by many developers nowadays. The big plus is that you can track the code changes, revert to the previous versions, contribute and cooperate with other developers.
Git is secure enough to trust, all relationships between directories, versions, commits are secured using hashing algorithm SHA1 which is cool, compared to other versions of version control systems, where files could be altered and compromised. Another big advantage is open source, with the huge community support and plenty of learning materials, including documentation, websites and various articles.

First, install the git on our Ubuntu:
- Updating the package list: $sudo apt-get update
- Installing the git on Ubuntu: $sudo apt-get install git
- Next, make sure the git was installed: $git –version (outputs git version 2.7+)

Git was installed, now it’s time to configure it:
$git config –global user.name “Your Name”
$git config –global user.email “[email protected]

- Verify your configuration changes: $git config –list (outputs your name, email address)
- To make changes of your configuration file: $git config

Working with the existed project and files
Earlier we installed the git on our Linux distribution. Now it’s time to work with git locally.
Download the demo project from here and create a folder with files, the output will be the following:
$user@user:~/Projects/website

In the directory we will see the list of files, for that you can use ls command $user@user:~/Projects/website: ls
Now we are in the folder of our project where all of the files are listed. We need to initialize git within that folder, for that we can use $git init
We will get initialized project folder with ./git prefix at the end of the command line, for example /home/user/Projects/website/.git/

You can also use $git init [project name] but [project name] is optional, just make sure you are in the working folder, $pwd just to identify where you are. You can find all git files, including hidden ones by typing $ls -a

Projects from zero
Ok, how about starting the project from scratch? Let’s do it!
I am navigating to my project directory and using the command $git init my-project to initialize the folder with git. For now, the folder is empty and doesn’t have any files, so let’s create one.
You can use command $your-text-editor README.md, where $your-text-editor will provide the available text editor, in my case it will sublime.

Submitting the file to Staging Area
So, I am going to use $subl README.md – and it will invoke the file extension with .md. In the file we can say pretty much anything we want, so I’ll include:
// #Some comments This is the simple text line in the readme file. //

Alright, after we can use command/ctrl + W to save the changes in the markdown file. Now, if we say $ls1, you will see that README.md is now available in the folder. But, we didn’t add the file to the staging area yet, we can ensure that by running the $git status command. It will inform us, that there is one untracked file that we just created – README.md So, let’s go ahead and put this file into staging area by running command $git add YOURFILE, in my case it’s going to be $git add README.md
Great! The terminal says that there are some changes to be committed. Let’s do our first commit by running $git commit. You can also add -m “Your Text” to add the text/status after the commit has been released. Cool, now there are no files to be committed, the working tree is clean, we can come back to the git and continue working with files.

Making changes on committed files
Let’s say you need to change the file you already submitted, how are you going to do that? By modifying the README file we will get modified status from git, in order to “apply” the changes to the file in the git working directory, simply use $git add README.md You can ensure that the file was modified by $git status
Now, let’s submit the commit using $git commit -m “Yaaay, file is committed”. How about making the process a little bit faster? Assuming that the file was already committed before, we can just use $git commit -am “Yaaay, file is committed!”. So what it does is adding the file -a, and sending the message after the commit was applied. Cool stuff!

The files that have been modified, more than 1:
In case there are several files that need to be added or committed, we can use “.” as simple as that: $git add . or $git commit

If we would like to remove the files from the staging:
$git reset HEAD yourfile.extension

Backout Working Directory Changes:
$git checkout — file-name
You can use this command in case you would like to back out any changes that were committed to the file and replace it with the old version.

Removing and deleting the files:
Great, now let’s think that we want to remove, or delete the file in the directory, how would we do that? There are different commands which are most used: $git rm file.extension / $git commit -m “I am bored of the file, removing it!”.
Here, we are removing the file and with -rm and submit the commit using commit -m”Our message”. Simple, right? But how are we going to deal with the file that was removed out of git eye? Easy – just add the recursive update: $git add -u. For example, $rm myfile.extension / $git add -u.. Git will update and understand that the file was removed by updating the index of the working directory.

Creating SSH folder and generating keys
Earlier we discussed how we can work locally with the files and manipulate them within the folder. Let’s talk about remote tools, such as Github. We will start from creating the folder for SSH:
$ mkdir .ssh
$ ssh-keygen -t rsa -C “[email protected]

After that, the terminal will ask you to set the default location to save the files, the default works fine for us, hit enter and set the passphrase, highly recommended! Now, if we say $ls -al, we will see all of the list of the files within this directory, including the hidden files. We are looking for files with id_rsa and id_rsa.pub
The file with pub extension is the public key for the connection with Github. Let’s open it with any of your favorite code editors, in my case it’s going to be sublime: $subl id_rsa.pub. Great! This is the key that we are going to use on Github side, just copy it and login/register into Github.
Go to settings, and paste the ssh key. After that, come back to the terminal and connect to github using the following command: $ssh -T [email protected]

Remote repository reference
Next, set up the repository to that was already created, select ssh option and copy the terminal command that was offered by Github: $git remote add remote-name remote-repository-location, in my case it’s $git remote add origin [email protected]:pandoraxcc/git-demo.git

Sending changes to remote (Github)
Here, the remote command allows us to associate the remote repository and usually the first/primary remote will be called origin. We can see the list of git’s remotes by using this command: $git remote -v , where -v stands for verbose, displaying full lists of URLS of the remote repository. For the first push on remote, we will need to use -u parameter: $git push -u remote-name branch-name
Here, push sends all local changes on branch to the remote, listed here as remote-name. In my case, master is the branch, and remote is origin, the command will look like this: $git push -u remote origin master

Receiving changes from remote
To receive all remote changes to our local branch, simply use this command: $git pull remote-name branch-name, in my case $git pull origin master
That’s it! Now, we can freely interact with local and remote environments! Cool! There is a cool book about git for free, try it out!