Git basics on command line

Git is a free and open source distributed version control system developed by Linus Torvalds back in 2005, in order to maintain his first big project, the Linux kernel. You can find many different GUI environments for controlling Git, or libraries for different editors like the Magit for Emacs.

The basic idea behind git is shown in figure below.

git-cycles

git init

We can initialize a git repository in our working directory, either locally using git init or by creating a new repository in our github account, and typing in a terminal git clone git@github.com:<user-name>/<repo-name>.git. I will create a new git local repository on my working directory.

git init && ls -al .git/
Initialized empty Git repository in home/aucotsi/myblog/aucotsi.git/      
total 40              
drwxr-xr-x 7 aucotsi aucotsi 4096 Νοέ 9 03:44 .
drwxr-xr-x 7 aucotsi aucotsi 4096 Νοέ 9 03:44 ..
drwxr-xr-x 2 aucotsi aucotsi 4096 Νοέ 9 03:44 branches
-rw-r–r-- 1 aucotsi aucotsi 92 Νοέ 9 03:44 config
-rw-r–r-- 1 aucotsi aucotsi 73 Νοέ 9 03:44 description
-rw-r–r-- 1 aucotsi aucotsi 23 Νοέ 9 03:44 HEAD
drwxr-xr-x 2 aucotsi aucotsi 4096 Νοέ 9 03:44 hooks
drwxr-xr-x 2 aucotsi aucotsi 4096 Νοέ 9 03:44 info
drwxr-xr-x 4 aucotsi aucotsi 4096 Νοέ 9 03:44 objects
drwxr-xr-x 4 aucotsi aucotsi 4096 Νοέ 9 03:44 refs

gitignore

Now I will create a .gitignore file to inform git which files and folders I do not want to watch using this repo.

echo -e '*.org\n*~\nimg/\nmedia/\nsrc/\ntmp-audacity' > .gitignore && cat .gitignore
*.org
*~
img/
media/
src/
tmp-audacity

git status (Working Directory)

Using git status I can see the status of my Working Directory.

git status
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	.gitignore

nothing added to commit but untracked files present (use "git add" to track)

None of the files/folders in my working directory are being tracked by git because I have exclude them in .gitignore. I have to add only the later in my repository.

git add (Staging Area)

In order to move .gitignore to the Staging Area I have to type git add .gitignore or git add . where dot symbol is regex.

git add .gitignore && git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   .gitignore

Now .gitignore is on Staging Area.

git commit (.git directory)

In order to checkout my directory I have to make my first commit.

git commit -m 'initial commit and adding .gitignore' && git status
[master (root-commit) e96bbb2] initial commit and adding .gitignore
 1 file changed, 6 insertions(+)
 create mode 100644 .gitignore
On branch master
nothing to commit, working tree clean
NOTE
If you decide to merge the steps git add and git commit using the -a -m switches you might loose or add unwanted information that you have added last minute. The same file can be at the same time on staging area and unstaged as well.

Now I have finished my git cycle. After every commit, git can remember every edit that I have done.

Rollback to previous version of a file

I will add a new line to .gitignore and I will rollback to my initial commit.

echo 'a new line' >> .gitignore && cat .gitignore && git status
*.org
*~
img/
media/
src/
tmp-audacity
a new line
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   .gitignore

no changes added to commit (use "git add" and/or "git commit -a")

Now I will stage and commit at once, just for the demo.

git commit -a -m 'stage & commit at once' && git status
[master 5009d0a] stage & commit at once
 1 file changed, 1 insertion(+)
On branch master
nothing to commit, working tree clean
  • git log

    git log -5 will print the history of the last five commits. Now I have only two commits so I will type git log.

    git log && cat .gitignore # check the contents of my file
    
    commit 5009d0a0c6d26ed2558b55af893d34fa7595c501
    Author: gewhere <myemail[at]mail[dot]com>
    Date:   Wed Nov 9 04:46:18 2016 +0200
    
        stage & commit at once
    
    commit e96bbb2dd86092297ddc447b9b25ab981481b0cf
    Author: gewhere <myemail[at]mail[dot]com>
    Date:   Wed Nov 9 04:13:09 2016 +0200
    
        initial commit and adding .gitignore
    *.org
    *~
    img/
    media/
    src/
    tmp-audacity
    a new line
    
  • git checkout

    I can rollback to my first commit using git checkout <file-id-of-the-commit> <file-name>.

    git checkout e96bbb2dd86092297ddc447b9b25ab981481b0cf .gitignore && cat .gitignore
    
    *.org
    *~
    img/
    media/
    src/
    tmp-audacity
    

    I have now successfully retrieve my previous commit.

Latest posts