Introduction To Git

About Git: https://git-scm.com/book/en/v2/Getting-Started-Git-Basics

This post goes over the basics of how to manage a local git repository.

First we need to setup the local git repository, to do that we create a directory and then initialize git inside of it.

user@linux-ryzk:~/> cd ~/Documents/demo/git-tutorial/
user@linux-ryzk:~/Documents/demo/git-tutorial> git init
        Initialized empty Git repository in /home/user/Documents/demo/git-tutorial/.git/

Now lets check to make sure everything is working as expected, we should see that we are on branch master, with only the initial commit and no files to change.

user@linux-ryzk:~/Documents/demo/git-tutorial> git status
    On branch master

    Initial commit

    nothing to commit (create/copy files and use "git add" to track)

To demonstrate the power of VCS, we can mess with some basic text files. Lets create a file called example.txt and put some text inside of it:

user@linux-ryzk:~/Documents/demo/git-tutorial> cat example.txt
    This is some sample data

Now if we check git status we should see that git has noticed the creation of the file.

user@linux-ryzk:~/Documents/demo/git-tutorial> git status
    On branch master

    Initial commit

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

            example.txt

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

To get git to start tracking a file you need to use the git add command like below. Then check status again to make sure the file is now being tracked.

user@linux-ryzk:~/Documents/demo/git-tutorial> git add example.txt
user@linux-ryzk:~/Documents/demo/git-tutorial> git status
user@linux-ryzk:~/Documents/demo/git-tutorial> git status
    On branch master

    Initial commit

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

            new file:   example.txt

Now we can try to commit the file to the git repo, files that get commited will have their changes tracked. Note that if you do not commit your changes the repo will not have a snapshot of the changes. Using the add command is not enough!

user@linux-ryzk:~/Documents/demo/git-tutorial> git commit -m "This was my first commit!"

        *** Please tell me who you are.

        Run

        git config --global user.email "you@example.com"
        git config --global user.name "Your Name"

        to set your account's default identity.
        Omit --global to set the identity only in this repository.

        fatal: unable to auto-detect email address (got 'user@linux-ryzk.(none)')

Note that git complains we didnt tell it who we are. This is because git tracks who made a certain commit, which will prove to be useful when we use the git blame utility to figure out who made certain changes!

So lets setup our account and then try to commit again…

user@linux-ryzk:~/Documents/demo/git-tutorial> git config --global user.email "ushamim@linux.com"
user@linux-ryzk:~/Documents/demo/git-tutorial> git config --global user.name "Uzair Shamim"
user@linux-ryzk:~/Documents/demo/git-tutorial> git commit -m "This was my first commit!"
    [master (root-commit) 7879099] This was my first commit!
    1 file changed, 1 insertion(+)
    create mode 100644 example.txt

We can use the git log to track all the changes ever made to this repo. If we check now we see there is just the commit we made.

user@linux-ryzk:~/Documents/demo/git-tutorial> git log
    commit 7879099254ac26ed30b6ef2f1424d919032aeb9d
    Author: Uzair Shamim <ushamim@linux.com>
    Date:   Wed Jan 20 20:35:29 2016 -0500

        This was my first commit!

Now lets edit the example file, add it to the tracked files and then commit it again.

user@linux-ryzk:~/Documents/demo/git-tutorial> cat example.txt
    This is some sample data. I made a mistike.
user@linux-ryzk:~/Documents/demo/git-tutorial> git status
    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:   example.txt

    no changes added to commit (use "git add" and/or "git commit -a")
user@linux-ryzk:~/Documents/demo/git-tutorial> git commit -m "This was my second commit!"
    [master 1eb830b] This was my second commit!
    1 file changed, 1 insertion(+), 1 deletion(-)

When we check the logs we can see we now have two different commits. Progress!

user@linux-ryzk:~/Documents/demo/git-tutorial> git log
    commit 1eb830bb5f6e84955d6170fc8c16fe984960c048
    Author: Uzair Shamim <ushamim@linux.com>
    Date:   Wed Jan 20 20:38:21 2016 -0500

        This was my second commit!

    commit 7879099254ac26ed30b6ef2f1424d919032aeb9d
    Author: Uzair Shamim <ushamim@linux.com>
    Date:   Wed Jan 20 20:35:29 2016 -0500

        This was my first commit!

Now that we have more than one commit, we can use the git diff utility to show us what changes were made to the repo between them. Note that the commit ID from the log is used for this, and the order you put the commits in affects the result (scroll down for an example).

user@linux-ryzk:~/Documents/demo/git-tutorial> git diff 1eb830bb5f6e84955d6170fc8c16fe984960c048 7879099254ac26ed30b6ef2f1424d919032aeb9d
    diff --git a/example.txt b/example.txt
    index e0373f3..caf30c1 100644
    --- a/example.txt
    +++ b/example.txt
    @@ -1 +1 @@
    -This is some sample data. I made a mistike.
    +This is some sample data

Lets create another file called example2.txt and put some text in it. Then we can add it and push a third commit to our repo.

user@linux-ryzk:~/Documents/demo/git-tutorial> git add example2.txt
user@linux-ryzk:~/Documents/demo/git-tutorial> git commit -m "This was my third commit!"
    [master 6c57eaa] This was my third commit!
    1 file changed, 1 insertion(+)
    create mode 100644 example2.txt

If we check our logs and diff we will see it now tells us two files were changed! This is very handy when you want to figure out what exact changes were made to your setup at a certain time. Note how in the example below the order of the commits affects what diff shows as added/removed.

user@linux-ryzk:~/Documents/demo/git-tutorial> git diff 6c57eaa2c8c6352d10b6153e27e4ce2d22fb7e1e 7879099254ac26ed30b6ef2f1424d919032aeb9d
    diff --git a/example.txt b/example.txt
    index e0373f3..caf30c1 100644
    --- a/example.txt
    +++ b/example.txt
    @@ -1 +1 @@
    -This is some sample data. I made a mistike.
    +This is some sample data
    diff --git a/example2.txt b/example2.txt
    deleted file mode 100644
    index d4a2d15..0000000
    --- a/example2.txt
    +++ /dev/null
    @@ -1 +0,0 @@
    -This is another file!
user@linux-ryzk:~/Documents/demo/git-tutorial> git diff 7879099254ac26ed30b6ef2f1424d919032aeb9d 6c57eaa2c8c6352d10b6153e27e4ce2d22fb7e1e
    diff --git a/example.txt b/example.txt
    index caf30c1..e0373f3 100644
    --- a/example.txt
    +++ b/example.txt
    @@ -1 +1 @@
    -This is some sample data
    +This is some sample data. I made a mistike.
    diff --git a/example2.txt b/example2.txt
    new file mode 100644
    index 0000000..d4a2d15
    --- /dev/null
    +++ b/example2.txt
    @@ -0,0 +1 @@
    +This is another file!
Advertisements
This entry was posted in Git, Uncategorized and tagged , , . Bookmark the permalink.

One Response to Introduction To Git

  1. Pingback: Links 29/1/2016: Controversy at the Linux Foundation, Tor Browser 5.5 | Techrights

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s