Getting Started with Git

If you have successfully installed Git on your system, we can now add some excitement to the lessons. Now let's create a folder first. No matter what we call it, I will call it "test-project". If you want, you can create this folder on the command line as follows;

mkdir test-project

Then let's go inside the folder. The command line is entered using the cd command.

cd test-project

Now we are inside the folder, this folder is our project folder. First of all, let's make this folder a git repo by running the following command.

git init

Now we are ready to test by creating a file in our project. For example, let's create our first file, index.php, as follows;

touch index.php

Then, either with the help of the editor or with the help of the editor tools on the command line, let's write something in it and save it. And let's run the following command on the command line and check its status.

git status

This returns a result like the one below. git status is the command that we get information about our repo, we often use it for control purposes. For example, it says that we have a file that is not tracked here, because we haven't added it to git yet. It's normal :) In fact, it says how to do this in the returned result, let's take a look right away.

MacBooks-MacBook-Pro-2:test-project phpexample$ git status
On branch master

No commits yet

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


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

As you can see, I need to add the files I want to follow to git as git add . Let's add our index.php file now.

git add index.php

When we say git status again and look at it, we see a different result.

MacBooks-MacBook-Pro-2:test-project phpexample$ git status
On branch master

No commits yet

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

new file: index.php 

Now Git keeps track of changes in my file. And as you can see it says "no commits yet". So the changes haven't been "commit" yet. Then let's do our first commit.

git commit -m 'index.php file created'

Here, after -m, we specify a message to the relevant commit so that they can read what changes we have made when looking forward or when our teammates look at it. Therefore, let's always set our commit messages within the framework of logic. It will always be more accurate to express the change made directly.

If you have just installed Git, it will ask you to introduce yourself during your first commit. Therefore, you should edit and run the following commands for once.

git config --global "PHPEXAMPLE"
git config --global ""

As a result, when we commit, we should get a result like this;

MacBooks-MacBook-Pro-2:test-project phpexample$ git commit -m 'index.php file created'
[master (root-commit) dad999a] index.php file created
1 file changed, 3 insertions(+)
create mode 100644 index.php 

If all is well, congratulations you have made your first commit. I would like to say a few things about commits, when there is any change in your file, you have to re-commit the final version. However, it will be a healthier use to commit the transactions one by one, not more than one transaction while committing. Also, as I mentioned, your commit messages should only be about what's been done about the current transaction.

Now, after your last commit, you made some edits to your file or files, but it didn't go as you wanted and the codes became soup. In this case, if you want to revert to your last commit, you can use the git checkout command. For example, make meaningless changes in index.php and then run the following command;

git checkout -- index.php

Yes, magic happened and your file is back to the last commit. If you have committed more than one commit and want to revert to any commit, first check your commits by saying git log.

MacBooks-MacBook-Pro-2:test-project phpexample$ git log
commit 72554b030c4be5a306e84002c37e67754d68ac4c (HEAD -> master)
Author: phpexample
Date: Tue May 28 13:07:30 2019 +0300

index.php editing

commit dad999a3a4d316347402ff75ea9ec449a67347b2
Author: phpexample
Date: Tue May 28 12:58:12 2019 +0300

index.php file created

Now we have 2 commits, we will use the commit id to see the differences between our current file and the first commit. as follows;

git diff dad999a3a4d316347402ff75ea9ec449a67347b2

This gave me a result like this.

MacBooks-MacBook-Pro-2:test-project phpexample$ git diff dad999a3a4d316347402ff75ea9ec449a67347b2
diff --git a/index.php b/index.php
index 8cdd3ec..908fea0 100644
--- a/index.php
+++ b/index.php
@@ -1,3 +1,5 @@

echo "index file";
+echo "test"; 

Now let's say I want to revert to the last commit again, then I will do something like this again using the commit id;

git checkout dad999a3a4d316347402ff75ea9ec449a67347b2

Yep, we've got a quick introduction to a Git world. Let's see what we will see next :)


There are no comments, make the firs comment