day 1
- init a git repository:
git init
- add file/files to the repository
git add
we can add multiple files usingadd
- submit these added files
git commit -m
Example
$ git add file1.txt
$ git add file2.txt file3.txt
$ git commit -m "add 3 files."
day2
Using git status
to check the current status of the repository
$ 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: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
the output told us that the file readme.txt had been modified, but the modification is not added to the repository.
Use git diff
to see the modification in details
$ git diff readme.txt
diff --git a/readme.txt b/readme.txt
index 46d49bf..9247db6 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1,2 @@
-Git is a version control system.
+Git is a distributed version control system.
Git is free software.
After knowing the difference of the file, we can add the modified version to the repository.
$ git add readme.txt
After this add operation, we run git status
to check the current status
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)
modified: readme.txt
The status told us that version to be committed includes readme.txt file. Next, we can commit the modification.
$ git commit -m "add distributed"
[master e475afc] add distributed
1 file changed, 1 insertion(+), 1 deletion(-)
We use git status
to check the status:
$ git status
On branch master
nothing to commit, working tree clean
day3
Use git log
to check all of the historical records of the repository.
$ git log
commit 1094adb7b9b3807259d8cb349e7df1d4d6477073 (HEAD -> master)
Author: Michael Liao
Date: Fri May 18 21:06:15 2018 +0800
append GPL
commit e475afc93c209a690c39c13a46716e8fa000c366
Author: Michael Liao
Date: Fri May 18 21:03:36 2018 +0800
add distributed
commit eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0
Author: Michael Liao
Date: Fri May 18 20:59:18 2018 +0800
wrote a readme file
We can print it with better-condensed look:
$ git log --pretty=oneline
1094adb7b9b3807259d8cb349e7df1d4d6477073 (HEAD -> master) append GPL
e475afc93c209a690c39c13a46716e8fa000c366 add distributed
eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0 wrote a readme file
About rollback
First, git needs to now what is the current version. Git uses HEAD
to represent the current version. The previous version is HEAD^
. And the previous of the previous one is HEAD^^
. The previous 100 one is HEAD~100
.
Now, we want to roll back the current version to the previous one. We can use the git reset
operation.
$ git reset --hard HEAD^
HEAD is now at e475afc add distributed
Now, the current version is the previous version.
We now check the log of the repository.
$ git log
commit e475afc93c209a690c39c13a46716e8fa000c366 (HEAD -> master)
Author: Michael Liao
Date: Fri May 18 21:03:36 2018 +0800
add distributed
commit eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0
Author: Michael Liao
Date: Fri May 18 20:59:18 2018 +0800
wrote a readme file
We cannot see the previous latest version.
We can always regret doing the previous rollback operation.
We can use git reflog
to show the record of each of the git operation and find the id of the 'regret version'.
$ git reflog
e475afc HEAD@{1}: reset: moving to HEAD^
1094adb (HEAD -> master) HEAD@{2}: commit: append GPL
e475afc HEAD@{3}: commit: add distributed
eaadf4e HEAD@{4}: commit (initial): wrote a readme file
We find the commit of 'append GPL' is 1094adb
. Then we use git reset
to roll back to the regret version.
$ git reset --hard 1094a
HEAD is now at 83b0afe append GPL
day 4
- Case 1: If you change some file accidentally and want to discard the modification, use
git checkout -- file
- Case 2: If you change some file and also applied the
add
operation and then you want to discard the changes, there are two steps: 1) usegit reset HEAD
to go back to Case 1; 2) use the solution of Case 1. - Case 3: If you have already committed the unwanted modification to the repository, use the rollback operation to cancel the commit.
day5
Delete a file
We first add a file test.txt
to the repository and then commit.
$ git add test.txt
$ git commit -m "add test.txt"
[master b84166e] add test.txt
1 file changed, 1 insertion(+)
create mode 100644 test.txt
We can directly delete the file using rm
operation
$ rm test.txt
The working directory is different from the repository. We use git status
to check the current git status
$ git status
On branch master
Changes not staged for commit:
(use "git add/rm ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
deleted: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
There are two cases that you would like to do:
- You definitely want to delete the file from the repository. We can use
git rm
to remove the file and then commit
$ git rm test.txt
rm 'test.txt'
$ git commit -m "remove test.txt"
[master d46f35e] remove test.txt
1 file changed, 1 deletion(-)
delete mode 100644 test.txt
- You accidentally remove the file. Since the file is still in the repository, we can simply use the
checkout
operation
$ git checkout -- test.txt
The checkout
operation is to replace the files in the directory with the ones in the repository.
day6
Add remote repository (We first have the local repository, and want to connect the local one with the remote one.)
- If we want to bind the local repository to the remote repository, we can use
git remote add origin git@server-name:path/repo-name.git
- After that, we use
git push -u origin master
to push all of the content of the master branch. - If necessary, we always use
git push origin master
to push the update of the branch.
day7
Clone the repository from the remote one
$ git clone [email protected]:michaelliao/gitskills.git
Cloning into 'gitskills'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 3
Receiving objects: 100% (3/3), done.
We must know the address of the remote repository.