Git Basics for Linux
Originally designed by the Linux All-Father, Linus Torvalds, Git was intended as a place where GNU/Linux developers could submit, merge and track changes to the Linux kernel. As is the habit with so many Linuxy things, Git evolved into something quite beyond its original purpose. Today, thousands- from coding beginners to some of the world's largest companies- use Git as their preferred version control system.
Whether it's your intention to utilize Git for your own project, want to add to or fork an existing project, or just want to check out an independently-developed application, Git is a versatile and fun addition to your repertoire. Getting familiar with it, of course, starts with its installation:
Installation
For CentOS:
sudo yum install git
For Ubuntu:
sudo apt-get install git
The Set-up
Once you've installed git, you'll want to introduce yourself to Git so that it can attribute work and changes to the right person.
git config -- global user.name ["Your Username"]
git config -- global user.email [your_email@example.com]
Working with Git
If you wanted to work on an existing Git project, you'd pull all its files to your local machine using
the 'clone' command:
git clone [url]
To start a new project, you should first create its directory. For the example here, I'm going to use a
Hello World bash script, but the contents are irrelevant to the tutorial.
mkdir /home/HelloWorld
cd /home/HelloWorld
git init
Initialized empty Git repository in /home/HelloWorld/.git/
The local repository stores work done by the local user. When you modify and commit your work, Git essentially saves something akin to a restore point of the project at that moment. Even your committed changes don't overwrite earlier versions, so you're largely free of those “What have I done!” moments.
The next command is one you will probably use quite frequently:
git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)
Nothing to see so far, but once the helloworld.sh is created and saved to the directory...
git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add ..." to include in what will be committed)
#
# helloworld.sh
nothing added to commit but untracked files present (use "git add" to track)
Here we see the contents of the Git, but Git still needs to be told what you'd like to track:
git add [filename]
Once added, a file gets placed into the Git staging area:
git add helloworld.sh
git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached ..." to unstage)
#
# new file: helloworld.sh
#
For those with a fear of commitment, the Git staging area is a perfect place to pause and reflect. You can continue editing, adding or removing files, and that work will not factor into the bigger picture. The Git stage is the last stop before storing your work into the permanent record.
To illustrate how the staging area keeps tabs on your work, I'm going to edit the bash script and add a
readme to the project:
vim helloworld.sh
vim readme.txt
git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached ..." to unstage)
#
# new file: helloworld.sh
#
# Changes not staged for commit:
# (use "git add ..." to update what will be committed)
# (use "git checkout -- ..." to discard changes in working directory)
#
# modified: helloworld.sh
#
# Untracked files:
# (use "git add ..." to include in what will be committed)
#
# readme.txt
As you can see, modifying a file doesn't automatically overwrite the version waiting to be committed. If you wanted to, you could easily scrap the new helloworld.sh, since any modifications or newly created files have to be sent to the staging area using the git add command. Once everything is just the way you want it, files are added to the project's permanent record using the git commit command. Committing also requires a message to be included. Normally here one would comment on what the commit contains or addresses:
git commit -m "Hello World is ready"
[master (root-commit)] Hello World is ready
2 files changed, 3 insertions(+)
create mode 100644 helloworld.sh
create mode 100644 readme.txt
At this point, the repository holds all the files created or modified, and then committed by the local user. Anyone else working on the project can see these, along with the notes their evolution in the Git log. To make the screenshot of that a little more interesting, I'm going to make further modifications and commit those changes, then view the log using:
git log
commit 284f219591b00be1a52e48c113bdaf6c3be9b367
Author: Your Name
Date: Thu Nov 20 12:55:08 2014 -0500
Final revision
commit 470e40d3634fcd14400fd71faef7a5c0ab22f70a
Author: Your Name
Date: Thu Nov 20 12:51:29 2014 -0500
Hello World is ready
The next step would be to push the directory to a remote repository, where it can be publicly or privately shared. With so many Git repositories available (most notably GitHub), setting up your own dedicated Git server isn't really necessary- but it also isn't so difficult that you need to be intimidated by the idea. That said, you won't be learning how to do it in this article. Instead, we hope you've got the necessary basics to start putting Git's versatility to use in your next project.