Monday, January 2, 2017

Learn Git within 7 Minutes and Play Around with It

Yesterday, I watched a VDO on somkiat.cc about Git for beginner and I found it is really interesting especially for those who new to Git (like me! 😅). The video is from CodingDojo.



Here is the infographic published on their page.


So let's play around with it! But first thing to do is to install Git client from here. Once installed then you are ready to play.



1. Let's find a folder you want to make a backup with Git or you can create a new one. This folder is called working directory. Here I created a folder "demo-git" and create new 3 text files in there.


Here are the content in those files:
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
view raw File 1.txt hosted with ❤ by GitHub
This is a test 1.
This is a test 2.
This is a test 3.
view raw File 2.txt hosted with ❤ by GitHub
10 INPUT "What is your name: "; U$
20 PRINT "Hello "; U$
30 INPUT "How many stars do you want: "; N
40 S$ = ""
50 FOR I = 1 TO N
60 S$ = S$ + "*"
70 NEXT I
80 PRINT S$
90 INPUT "Do you want more stars? "; A$
100 IF LEN(A$) = 0 THEN GOTO 90
110 A$ = LEFT$(A$, 1)
120 IF A$ = "Y" OR A$ = "y" THEN GOTO 30
130 PRINT "Goodbye "; U$
140 END
view raw File 3.txt hosted with ❤ by GitHub
2. Create local Git repository using command git init
C:\Users\Chairat\Git\demo-git>git init
Initialized empty Git repository in C:/Users/Chairat/Git/demo-git/.git/
3. Add files to staging area using command git add .
C:\Users\Chairat\Git\demo-git>git add .
4. Commit your files into your local Git repository by using command git commit -m "Initial Commit"
C:\Users\Chairat\Git\demo-git>git commit -m "Initial Commit"
[master (root-commit) 3bea459] Initial Commit
3 files changed, 18 insertions(+)
create mode 100644 File 1.txt
create mode 100644 File 2.txt
create mode 100644 File 3.txt
Now you have a backup of your files in your local Git repository.

5. Let's make change to the File 2.txt and File 3.txt and use command git status

C:\Users\Chairat\Git\demo-git>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: File 2.txt
modified: File 3.txt
no changes added to commit (use "git add" and/or "git commit -a")
This will show which files have been modified since the last commit.

6. Add modified files to staging area by command git add .

C:\Users\Chairat\Git\demo-git>git add .
7. Use command git status again

C:\Users\Chairat\Git\demo-git>git status
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)
modified: File 2.txt
modified: File 3.txt
Now you modified files are now in staging area and ready to commit.

8. Use command git commit -m "Updated File 2 and 3" to commit your changes to local Git repository.
C:\Users\Chairat\Git\demo-git>git commit -m "Updated File 2 and 3"
[master 43ac65a] Updated File 2 and 3
2 files changed, 2 insertions(+), 2 deletions(-)
9. If you use command git status again, it will say there's no more change to commit.
C:\Users\Chairat\Git\demo-git>git status
On branch master
nothing to commit, working tree clean
10. Use command git log to display commit log information
C:\Users\Chairat\Git\demo-git>git log
commit 43ac65aa37851b11ab5b3216f1c7cdd5a053a989
Author: pacroy
Date: Mon Jan 2 12:00:18 2017 +0700
Updated File 2 and 3
commit 3bea4599b90ab043af41f1c81ac293197d930f20
Author: pacroy
Date: Mon Jan 2 11:49:44 2017 +0700
Initial Commit
11. you can use command git show to display the changes from the last commit.

C:\Users\Chairat\Git\demo-git>git show
commit 43ac65aa37851b11ab5b3216f1c7cdd5a053a989
Author: pacroy
Date: Mon Jan 2 12:00:18 2017 +0700
Updated File 2 and 3
diff --git a/File 2.txt b/File 2.txt
index 82cd70f..5829b98 100644
--- a/File 2.txt
+++ b/File 2.txt
@@ -1,3 +1,3 @@
This is a test 1.
-This is a test 2.
+This is a test 2xx.
This is a test 3.
\ No newline at end of file
diff --git a/File 3.txt b/File 3.txt
index b0dea5f..6792d9f 100644
--- a/File 3.txt
+++ b/File 3.txt
@@ -1,5 +1,5 @@
10 INPUT "What is your name: "; U$
-20 PRINT "Hello "; U$
+20 PRINT "Hello World"; U$
30 INPUT "How many stars do you want: "; N
40 S$ = ""
50 FOR I = 1 TO N
12. You can revert changes to previous commit by using command git checkout <current commit name>~1

C:\Users\Chairat\Git\demo-git>git checkout 43ac65aa37851b11ab5b3216f1c7cdd5a053a989~1
Note: checking out '43ac65aa37851b11ab5b3216f1c7cdd5a053a989~1'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b
HEAD is now at 3bea459... Initial Commit
If you use command git status again you will see this:
C:\Users\Chairat\Git\demo-git>git status
HEAD detached at 3bea459
nothing to commit, working tree clean
13. Next, you want to push your local repository into your remote repository so someone else can work on your files. First thing is you need to create a new repository on GitHub (Sign up first if you don't have an account yet.).


14. Input your repository name and click Create repository


15. Copy your Git repository URL


16. Use command git remote add origin <repository URL> to add a new remote repository named "origin" to your local repository
C:\Users\Chairat\Git\demo-git>git remote add origin https://github.com/pacroy/demo-git.git
If you use command git remote now you should see "origin" in in the list

C:\Users\Chairat\Git\demo-git>git remote
origin
17. Let's get back to the latest commit by using command git checkout master
C:\Users\Chairat\Git\demo-git>git checkout master
Previous HEAD position was 3bea459... Initial Commit
Switched to branch 'master'
18. Use command git push --set-upstream origin master to push files to your remote Git repository
C:\Users\Chairat\Git\demo-git>git push --set-upstream origin master
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 370 bytes | 0 bytes/s, done.
Total 4 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local objects.
To https://github.com/pacroy/demo-git.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
If you use command git status you will that your local repository is up-to-date with your remote repository.
C:\Users\Chairat\Git\demo-git>git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean
19. Try to make some changes on GitHub.

20. Use command git fetch to get the latest data from remote repository
C:\Users\Chairat\Git\demo-git>git fetch
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 6 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (6/6), done.
From https://github.com/pacroy/demo-git
43ac65a..d8ba137 master -> origin/master
If you use command git status again now, you will see that your local repository is now behind your remote repository
C:\Users\Chairat\Git\demo-git>git status
On branch master
Your branch is behind 'origin/master' by 2 commits, and can be fast-forwarded.
(use "git pull" to update your local branch)
nothing to commit, working tree clean
21. Use command git pull to load all changes from remote repository to your local repository.

C:\Users\Chairat\Git\demo-git>git pull
Updating 43ac65a..d8ba137
Fast-forward
File 1.txt | 2 +-
File 2.txt | 4 ++--
2 files changed, 3 insertions(+), 3 deletions(-)
Now, your working directory and local repository should be at the same version as in remote repository. You can check that by using command git status

C:\Users\Chairat\Git\demo-git>git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean

Learn More

2 comments:

  1. Replaced console commands with Gist

    ReplyDelete
  2. Added http://learngitbranching.js.org/ as a Learn More link

    ReplyDelete