-
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!
Now go ahead...make some more commits!
How about we work with some actual code to show you the power of VCS in jumping back between snapshots of your code.
Let's create a simple C program that has 5 integers and we perform some arithmetic operations on them.
First we need to create a git directory
$ mkdir mathStuff
$ cd mathStuff
$ git init
Initialized empty Git repository in /Users/abinavseelan/mathStuff/.git/Cool. Let's create a the basic structure of our C file.
#include <stdio.h>
int main() {
}Now let's compile and run this. (Cause why not. =P)
$ gcc math.c
$ ./a.outAs long as you don't see any error messages, everything went through fine.
Now let's create a git commit with this. First let's see what we have to commit
$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.out
math.c
nothing added to commit but untracked files present (use "git add" to track)We see that the compiled file a.out is part of the files to be added. Now, a.out is something we don't need as every time we want to run this, we're going to compile and then execute. So how do we get git to ignore the file?
We can do this by adding a file called .gitignore to the project folder.
(Again, we're going to use the command line, but you can create this file using notepad if you want to)
$ touch .gitignore
$ cat >> .gitignore
a.outDoing this tells git to ignore a file called a.out. Let's see if that worked.
$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
math.c
nothing added to commit but untracked files present (use "git add" to track)And we that we've officially told git to ignore the file! =D
Time to commit these files.
$ git add .gitignore
$ git add math.c
$ git commit -m "Added initial files"
[master (root-commit) 8f0059f] Added initial files
2 files changed, 6 insertions(+)
create mode 100644 .gitignore
create mode 100644 math.cHere, we added the files one-by-one. Sometimes, if we know that we want to add all the files in our directory, we can just use the short-hand
$ git add .Anyway, let's get coding! Let's add some variables to the code.
#include <stdio.h>
int main () {
int a = 5;
int b = 6;
int c = 7;
int answer;
answer = a + b + c;
printf("The answer is: %d", answer);
}Time to compile and test.
$ gcc math.c
$ ./a.out
The answer is: 18Woot! Genius.
Now let's commit this to Git.
$ git add .
$ git commit -m "Created an add-er"
[master 35dd5de] Created an add-er
1 file changed, 6 insertions(+)Cool. To show you guys the power of VCS, we're going to purposely make a mistake to the code and commit it. (So don't think we're stupid. xD)
#include <stdio.h>
int main() {
int a = 5;
int b = 6;
int c = 7;
printf("The answer is: %d", answer);
}We removed the entire arithmetic operation! Whoopsie! And now we commit it to new snapshot. (Oh no!)
$ git add .
$ git commit -m "Added some new functionality
[master bd10104] Added some new functionality
1 file changed, 2 deletions(-)The list of snapshots is now
$ git log
commit bd101040bd72234b55f3c8ed4690875b9dc3597a
Author: Abinav Seelan <abinav.n.seelan@gmail.com>
Date: Wed Aug 31 21:37:59 2016 +0530
Added some new functionality
commit 35dd5de0f9f3141d9f56142c4f8ee355e5a937a4
Author: Abinav Seelan <abinav.n.seelan@gmail.com>
Date: Wed Aug 31 18:38:56 2016 +0530
Created an add-er
commit 8f0059f67e3af8c2f0a35716131a5b2a7bd2eafe
Author: Abinav Seelan <abinav.n.seelan@gmail.com>
Date: Wed Aug 31 18:29:30 2016 +0530
Added initial filesNow we run the code and this happens.
$ gcc math.c
math.c:8:30: error: use of undeclared identifier 'answer'
printf("The answer is: %d", answer);
^
1 error generated. Sh*t be going down! Now we're going use a VCS to fix this.
We know that the last snapshot of the code worked perfectly. Let's move back to that snapshot. TO THE TIMEMACHINE (Aka Git)
$ git checkout 35dd5de0f9f3141d9f56142c4f8ee355e5a937a4Now we've officially gone back in time! See for yourself. Try checking the git list of commits.
$ git log
commit 35dd5de0f9f3141d9f56142c4f8ee355e5a937a4
Author: Abinav Seelan <abinav.n.seelan@gmail.com>
Date: Wed Aug 31 18:38:56 2016 +0530
Created an add-er
commit 8f0059f67e3af8c2f0a35716131a5b2a7bd2eafe
Author: Abinav Seelan <abinav.n.seelan@gmail.com>
Date: Wed Aug 31 18:29:30 2016 +0530
Added initial filesAs we can see, we moved back to a different snapshot in time. Now, when you check the contents of the math.c file, you'll notice that it's all back to the state it was at that point in time!
$ cat math.c
#include <stdio.h>
int main() {
int a = 5;
int b = 6;
int c = 7;
int answer;
answer = a + b + c;
printf("The answer is: %d", answer);
}- 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