-
Notifications
You must be signed in to change notification settings - Fork 0
Getting Started With Git
If you are on Ubuntu, you can just run the following commands to get the git package from the Ubuntu Package Repository
$ sudo apt-get update
$ sudo apt-get install gitVisit git-scm. Download. Install.
==============
To verify if git has installed correctly
$ git --versionThis should display the git version.
Cool. Now that we have git on our systems, let's dive into some simple commands to get started!
Ok. Let's talk about commits for a second. Git works on the concept of snapshots. If the history of file changes is a video then a snapshot is a screenshot taken anywhere from that video. Committing is the act of creating this snapshot. That's it! =)
To start things off, let's create a folder to hold our files, shall we?
$ cd ~
$ mkdir project_folder
$ cd project_folderExecuting that will create a folder called project_folder in our home directory and move us into that folder.
A repository is a place that holds all those snapshots we create of our files. To initialise this folder to be a repository we run the following
$ git initOnce you run that, you'll notice that the folder now contains a .git folder. We won't go into a lot of detail on what it contains, but just think of it as housing all those snapshots in it. =)
And that's that...your folder is now a Git Repository. Woohoo!
Let's create a file in your folder now and add some stuff to it.
$ touch file1.txt
$ cat >> file1.txt
Hello WorldThis creates a file called file1.txt in your project directory and adds the line Hello World to it.
Now let's see what Git is seeing of our project folder.
$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
file1.txt
nothing added to commit but untracked files present (use "git add" to track)As you can see, Git shows that we have an untracked file. An untracked file is a file that the VCS has never seen before in your folder, ie. file1.txt.
Also, Git is known for it's extremely helpful prompts. Here we see nothing added to commit but untracked files present (use "git add" to track). So let's do that. Let's move our new file into the staging phase that we talked about before. (If you skipped that, you can find it here
To do that, run the following
$ git add file1.txtNow run git status
$ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: file1.txtCool. We've now moved the file into staging. Now all that's left is to package this into a snapshot by committing it. =D
$ git commit This should take us to a place that looks like this:
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
#
# Initial commit
#
# Changes to be committed:
# new file: file1.txt
#This is the terminal editor, asking us to enter a message to explain what this snapshot is so that someone looking at the snapshot can understand and have some context. =)
Just add a message right above all this text....like as follows:
Added a really helpful file!
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
#
# Initial commit
#
# Changes to be committed:
# new file: file1.txt
#Ok. I'm sure you're wondering why you can't type anything at all...and the editor is going all over the place. that's because the default editor for the terminal is vim. To figure out how to use vim, visit this link here.
And that's that! You've now created your first snapshot of your code! To see it,
$ git log
commit 63a21a93491c66d3295e212c02113a14112c8f66
Author: Abinav Seelan <abinav.n.seelan@gmail.com>
Date: Wed Aug 31 17:44:26 2016 +0530
Added a really helpful file!git log shows the list of snapshots that are being maintained in the .git folder. You can see your recent commit there along with the message that you added in the previous step! =)
You must be wondering what that 63a21a93491c66d3295e212c02113a14112c8f66 is right? That's called the SHA. It's a tag the .git folder adds to your snapshot. So whenever you do something with git and reference that SHA, the .git folder will know what you're talking about.
Let's see what happens and how git sees our folder when we modify a file.
Open up file1.txt in your favourite text editor and add some more lines. We're going to use the command line here for this, but you're free to use notepad if you want. =)
$ cat >> file1.txt
This is line two
This is line 3Now, as we'd done previously, let's see what git sees.
$ 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: file1.txt
no changes added to commit (use "git add" and/or "git commit -a")Notice that there's something different here. While in the previous exercise we saw that there were some untracked files, we see that git now tells us that file1.txt has been modified.
This is because the file is now known by git as it's seen the file before in a previous snapshot (also called tracking). It now prompts us saying that file1.txt has been updated and if we want to create another snapshot, we can do it by adding the file first.
$ git add file1.txt
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: file1.txtLet's commit this to create a new snapshot! Let's use some shorthand stuff here. Our commit message is going to be short, so we can use the -m argument to add the message directly without having to go into vim to add the message
$ git commit -m "This is the second commit."
[master 5539da8] This is the second commit.
1 file changed, 3 insertions(+), 1 deletion(-)Using the -m argument we can easy add small commit messages. Notice that now git says that 1 file changed, 3 insertions(+), 1 deletion(-)...which is on-point. Pretty cool eh?
Now let's see all our commits
$ git log
commit 5539da899619e74ab78b5912c776ae90bad31ed3
Author: Abinav Seelan <abinav.n.seelan@gmail.com>
Date: Wed Aug 31 18:05:01 2016 +0530
This is the second commit.
commit 63a21a93491c66d3295e212c02113a14112c8f66
Author: Abinav Seelan <abinav.n.seelan@gmail.com>
Date: Wed Aug 31 17:44:26 2016 +0530
Added a really helpful file!Want to see what's in each snapshot?
$ git show 63a21a93491c66d3295e212c02113a14112c8f66
commit 63a21a93491c66d3295e212c02113a14112c8f66
Author: Abinav Seelan <abinav.n.seelan@gmail.com>
Date: Wed Aug 31 17:44:26 2016 +0530
Added a really helpful file!
diff --git a/file1.txt b/file1.txt
new file mode 100644
index 0000000..b6ab9c0
--- /dev/null
+++ b/file1.txt
@@ -0,0 +1 @@
+Hello WorldWe have the commit SHA, the commit message, author, date and we also have the lines of code that we've added in that snapshot.
$ git show 5539da899619e74ab78b5912c776ae90bad31ed3
commit 5539da899619e74ab78b5912c776ae90bad31ed3
Author: Abinav Seelan <abinav.n.seelan@gmail.com>
Date: Wed Aug 31 18:05:01 2016 +0530
This is the second commit.
diff --git a/file1.txt b/file1.txt
index b6ab9c0..9b6400a 100644
--- a/file1.txt
+++ b/file1.txt
@@ -1 +1,3 @@
+This is line 2
+This is line 3Viewing the second commit gives us some more insight as to what snapshots hold. Lines starting with a - mean that that line was removed and ones starting with a + mean that that line was added. As we can see here, the lines This is line 2 and This is line 3 were added in this snapshot!
- What is Version Control?
- [What is Git?] (https://github.com/campvanilla/introduction-to-github/wiki/What-is-Git%3F)
- [Getting Started with Git] (https://github.com/campvanilla/introduction-to-github/wiki/Getting-Started-with-Git)
- Installation
- First Commit
- The Next Step
- Fixing Mistakes Using VCS
- Plugging Git To Github