By Ran Bar-Zik | 6/4/2018 | General |Beginners

First Steps in Git - Basic Commands

First Steps in Git - Basic Commands

In the last article we learned about installing Git and basic configurations. We also saw how Git works the same in Windows and Linux, and as such the commands are the same across platforms.

 

Let’s open a folder on our computer where we’ll run our exercises. Let’s use: temp_project

 

In order to get Git to track this folder, we need to make a new repository, or repo for short. Go to the folder and use the command git init to initialize a repo. If everything was OK, you should see this:

$ git init
Initialized empty Git repository in /home/ran/temp_project/.git/

From this point on the folder is being tracked by Git. Note that there is no need for a remote server or to install a local server. All the work at this point is done locally. In the future we’ll learn about working with a server.

 

Now we’ll create a file named index.php in which we’ll put none other than— hello.world. After it’s been created, we’ll use the command git status to see if there are changes between the local version and what’s saved on our local Git. This is what we’ll see:

$ git status
On branch master
Initial commit
Untracked files:
 (use "git add ..." to include in what will be committed)
index.php

Now it’s time to talk about the workflow in Git. As opposed to SVN, every time we push code there are two stages. The first is the commit that we use to push local code alone. This push is really code begin pushed to the server. We’re working with the local commit at the moment. So we can see, via the git status, that we have one untracked file, meaning that Git is not tracking it. The file name is index.php. Let’s ask Git to put it to the repo. We do this with the git add command, which takes the file name (or pattern, but that another story for later). After we type git add index.php, and then git status we can see that in the initial commit we have a new file called index.php:

$ git add index.php 
$ git status
On branch master
Initial commit
Changes to be committed:
 (use "git rm --cached ..." to unstage)
new file:   index.php

I can add to and update my commit—update index.php, to add new files (don’t forget to use git add after you create the files). After I finish and my commit is ready, I’ll use the commit command to put all my work into the local repo. The command is simple: git commit, then -m with the commit message in quotation marks. Here’s how it looks:

$ git commit -m "Initial commit"
[master (root-commit) afe684e] Initial commit
1 file changed, 3 insertions(+)
create mode 100644 index.php

Take note of the space and the double quotation marks. From this point, I’m pretty much finished my local work. I can continue to work and any changes I make will go into a new commit. If I edit index.php and run git status again, I’ll see the following output:

$ git status
On branch master
Changes not staged for commit:
 (use "git add ..." to update what will be committed)
 (use "git checkout -- ..." to discard changes in working directory)
modified:   index.php
no changes added to commit (use "git add" and/or "git commit -a")

If I want to do another commit, then I need to run git add again with the file name (or git add -all for all the files if there are multiple) and then git commit with the message for the log.

So how does it all work? Create a Git repo -> create files -> choose what to add to the commit with commit add -> commit -> continue working and repeat.

 

If I have a few commits, I have a log I can take a quick look at:

$ git log
commit 228a8c3190429c61c8fde0c77a068ae25d2fed29
Author: Ran Bar-Zik 
Date:   Sat Nov 21 16:42:07 2015 +0200
   Change wording
commit afe684e746f36320d5aac9db039807051fe8877e
Author: Ran Bar-Zik 
Date:   Sat Nov 21 16:34:21 2015 +0200
   Initial commit

See that long number there? 228a8c3190429c61c8fde0c77a068ae25d2fed29 for instance? This is called the hash and it’s the unique identifier of the commit. Just like in SVN or any other version control system. I can view the differences between commits with diff:

$ git diff 228a8c3190429c61c8fde0c77a068ae25d2fed29 afe684e746f36320d5aac9db039807051fe8877e

You don’t have to use the entire hash—in most cases you can use just the last 5 characters. For instance:

$ git diff 228a8 afe68
diff --git a/index.php b/index.php
index b495a86..40f1dd2 100644
--- a/index.php
+++ b/index.php
@@ -1,3 +1,3 @@
<?php

-print "Hello World!";
+print "hello world!";

If you’ve worked with SVN or another version control system, you should be familiar with the syntax of diff, and even a few GUIs that work with Git just like they do with SVN.

 

At this point, it’s still not very different from working with SVN—besides that it’s easy to work locally in Git as opposed to SVN where we needed a server. With Git we can work locally, push commits and run diff. But the great thing about Git is how its works with a server or remote servers. So in the next article, we’ll learn about working with a remote server.

 

Previous article: Intro to Git

Next article: remote servers in GitHub

 

About the author: Ran Bar-Zik is an experienced web developer whose personal blog, Internet Israel, features articles and guides on Node.js, MongoDB, Git, SASS, jQuery, HTML 5, MySQL, and more. Translation of the original article by Aaron Raizen.

By Ran Bar-Zik | 6/4/2018 | General

{{CommentsModel.TotalCount}} Comments

Your Comment

{{CommentsModel.Message}}

Recent Stories

Top DiscoverSDK Experts

User photo
3355
Ashton Torrence
Web and Windows developer
GUI | Web and 11 more
View Profile
User photo
3220
Mendy Bennett
Experienced with Ad network & Ad servers.
Mobile | Ad Networks and 1 more
View Profile
User photo
3060
Karen Fitzgerald
7 years in Cross-Platform development.
Mobile | Cross Platform Frameworks
View Profile
Show All
X

Compare Products

Select up to three two products to compare by clicking on the compare icon () of each product.

{{compareToolModel.Error}}

Now comparing:

{{product.ProductName | createSubstring:25}} X
Compare Now