git

[Git] Git Basics

Everything is local

Posted by Yu-Hsuan Yen on 2017-03-25

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

Basic Command


I’ll list the most common command down below.
You can find more information at git cheat sheet

1
2
3
4
5
6
$ git status    # Show the working tree status
$ git add # Add your file to the index
$ git diff # Show changes between commits
$ git commit # Record your change to the reposity
$ git log # Check your git history
$ git reset # Reset current HEAD to the specified state

Here is an example to learn how to use these command.
git lifecycle

Picture from: https://git-scm.com

Git status

Add a file at your repo , in this toturial I create git-test.py at my repo.
This will list all new or modified files to be commited, you can know which file you change or not be tracked.

1
2
3
4
5
6
7
8
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)

git-test.py

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

Git add

Then add the file you want to track.

1
2
3
4
5
6
7
$ git add git-test.py 	
$ git status # check new status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

new file: git-test.py

Git diff

See the difference between original status and the current status.

1
2
3
4
5
6
7
8
9
$ git diff
diff --git a/git-test.py b/git-test.py
index e69de29..b29b4cd 100644
--- a/git-test.py
+++ b/git-test.py
@@ -0,0 +1,2 @@
+#by Yu-Hsuan
+print ("This is git tutorial!!")
(END)

press Q if you want to quit the diff listing.

Git commit

All file are prepared, then you have to commit this change to the repo.

1
2
3
$ git commit -m "<Commit Message>"
[master 7fa4bc8] <Commit Message>
1 file changed, 2 insertions(+)

Git log

Check your commitment.

1
2
3
4
5
6
$ git log
commit 7fa4bc8901c35d67ab4216a0fe4ca33681af4a3d
Author: BeanTech <BeanTech@gmail.com>
Date: Sun Mar 26 HH:MM:SS 2017 +0800

<Commit Message>

Git reset

Reset current HEAD to the specified state.
the argument HEAD can change to HEAD^ or HEAD~<num>

1
2
3
$ git reset HEAD^       # reset HEAD to previous version
$ git reset HEAD~2 # reset HEAD back to past 2 version
$ git reset --hard HEAD # reset HEAD to the first status

Try it!!

I think the fastest way to learn git is to try it your self !

Let’s go to the other git tutorial:
Next - Git Branching
Prev - Start with Git

Full series of git tutorial