Untracked: 剛新加的文件,尚未歸入git管理範圍git
UnModified: 已經committed的文件github
Modified: 已經committed的文件,經過vi等修改後,就變成Modifiedapp
Staged: git add 後的文件3d
Untracked->Staged: 經過git add 來完成日誌
UnModified->Modified: 修改文件內容來完成,好比vi命令code
Modified->Staged: 經過git add 來完成生命週期
UnModified->Untracked: 經過git rm 來完成ip
Staged->UnModified: 經過git commit 來完成rem
#git clone git@github.com:sotrip/gittest.git Cloning into 'gittest'... warning: You appear to have cloned an empty repository. Checking connectivity... done.
#vi 1.txt 裏面內容以下: the first line
#git status On branch master //表示咱們目前在master分支上 Initial commit Untracked files: //有哪些文件是Untracked狀態,有1.txt (use "git add ..." to include in what will be committed) 1.txt nothing added to commit but untracked files present (use "git add" to track)
#git add 1.txt 成功後,沒有輸出 #git status 再次查看 On branch master Initial commit Changes to be committed: //表示1.txt已是staged了,能夠被提交了 (use "git rm --cached ..." to unstage) //若是不想提交了,能夠用git rm --cached 1.txt new file: 1.txt
文件已是staged了,但想要退回原來的狀態it
#git rm --cached 1.txt rm '1.txt' #git status // 再來看又回來2.3這一步了 On branch master Initial commit Untracked files: (use "git add ..." to include in what will be committed) 1.txt #git add 1.txt // 咱們仍是再加上 #git status On branch master Initial commit Changes to be committed: // 1.txt 又改成staged狀態 準備提交 (use "git rm --cached ..." to unstage) new file: 1.txt
#git commit -m "first commit" //-m後面是咱們這一次提交的註釋 [master (root-commit) e6b0e7d] first commit 1 file changed, 1 insertion(+) create mode 100644 1.txt
#git push origin master Warning: Permanently added the RSA host key for IP address '*.*.*.*' to the list of known hosts. Counting objects: 3, done. Writing objects: 100% (3/3), 214 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To git@github.com:sotrip/gittest.git * [new branch] master -> master
命令歸納
git diff //查看 Modified的文件,作了哪些修改 git diff --staged // 查看 Staged的文件,作了哪些修改
#vi 1.txt 在後面增長一行,變成以下 the first line the second line #git status On branch master Your branch is up-to-date with 'origin/master'. Changes not staged for commit: //這個表示1.txt已經變爲Modified了,not staged (use "git add ..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: 1.txt no changes added to commit (use "git add" and/or "git commit -a") #git diff 查看Modified的文件,修改了哪些地方 diff --git a/1.txt b/1.txt index 137b7fd..067030b 100644 --- a/1.txt +++ b/1.txt @@ -1 +1,2 @@ the first line +the second line #git add 1.txt //把1.txt加入到staged中 #git status On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD ..." to unstage) modified: 1.txt #git diff 這個時候不會輸出任何東西,由於沒有Modified的文件了 #git diff --staged //查看staged的文件和上一次commit有哪些修改 diff --git a/1.txt b/1.txt index 137b7fd..067030b 100644 --- a/1.txt +++ b/1.txt @@ -1 +1,2 @@ the first line +the second line
命令歸納 #git reset HEAD 1.txt //文件已經Staged的了,用這個來回滾到Modified狀態,可是內容不會回滾 #git checkout 1.txt //若是文件是Modified,不想作修改了,恢復原樣,使用這個
#git status On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD ..." to unstage) modified: 1.txt #git diff --staged diff --git a/1.txt b/1.txt index 137b7fd..067030b 100644 --- a/1.txt +++ b/1.txt @@ -1 +1,2 @@ the first line +the second line #git reset HEAD 1.txt //把1.txt 的狀態由Staged變爲Staged, 可是1.txt的內容不會變 Unstaged changes after reset: M 1.txt #git status On branch master Your branch is up-to-date with 'origin/master'. Changes not staged for commit: //能夠看出1.txt 由Staged變爲Modified (use "git add ..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: 1.txt no changes added to commit (use "git add" and/or "git commit -a") #cat 1.txt //查看內容,發現 1.txt的內容並無回滾 the first line the second line #git checkout 1.txt //回滾 #git status On branch master Your branch is up-to-date with 'origin/master'. nothing to commit, working directory clean #cat 1.txt //內容已經回滾 the first line
命令歸納 #git revert HEAD //回滾上一次提交 #git revert HEAD^ //回滾上上次提交 #git revert #commit no# //回滾某一次提交 #git revert -m 1 #commit no# //回滾某一次merge提交
增長了2.txt 並提交了,如今想回滾 #vi 2.txt 在裏面增長內容 #git add 2.txt 把文件由Untracked 變爲 Staged #git commit -m "2.txt commit" 提交 #git log 查看提交日誌 commit 710c5e84bd02e5d041b537b8732b9e80fee257a1 //這個是咱們2.txt的提交 Author: jingbo Date: Thu Apr 7 22:10:00 2016 +0800 2.txt commit commit e6b0e7d844154d5473a37baed2ef56807dca16b3 Author: jingbo Date: Wed Apr 6 22:42:44 2016 +0800 first commit #git show 710c5e84bd02e5d041b537b8732b9e80fee257a1 能夠查看此次修改的內容 #git revert 710c5e84bd02e5d041b537b8732b9e80fee257a1 //回滾提交 [master d3ab103] Revert "2.txt commit" 1 file changed, 1 deletion(-) delete mode 100644 2.txt #git revert -m 1 9d55502f0e05bef943d6b4e7cf57aafe08e42d63 //保留master,去除別的分支提交過來的
#git branch //查看目前有哪些分支 * master //只有一個分支,"*"表示當前是在master分支上
#git branch first-branch //打出第一個分支,名字是first-branch #git branch first-branch //分支已經有了 * master //"*"表示當前是在master分支上 #git checkout first-branch Switched to branch 'first-branch' #git branch * first-branch //已經成功切換到本身打的分支上了 master
#vi 2.txt #cat 2.txt //增長的內容以下 edit in first-branch #git add 2.txt #git commit -m "2.txt commit in first-branch" //在分支上提交 [first-branch 9abd8f2] 2.txt commit in first-branch 1 file changed, 2 insertions(+) create mode 100644 2.txt
# git push origin first-branch Counting objects: 7, done. Delta compression using up to 4 threads. Compressing objects: 100% (5/5), done. Writing objects: 100% (7/7), 692 bytes | 0 bytes/s, done. Total 7 (delta 0), reused 0 (delta 0) To git@github.com:sotrip/gittest.git * [new branch] first-branch -> first-branch
#git diff master first-branch // 比較master與first-branch diff --git a/2.txt b/2.txt new file mode 100644 index 0000000..b09edf1 --- /dev/null +++ b/2.txt // 表示first-branch上多了一個2.txt @@ -0,0 +1,2 @@ +edit in first-branch+
#git checkout master #git merge first-branch //把first-branch的內容合併到master上 Updating d3ab103..9abd8f2 Fast-forward 2.txt | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 2.txt #ls 1.txt 2.txt # cat 2.txt edit in first-branch #git log commit 9abd8f2d8fe7c08ca246464552dae25397694582 Author: jingbo Date: Thu Apr 7 22:26:26 2016 +0800 2.txt commit in first-branch //在first-branch上提交的內容也顯示在日誌中 ### **6.7 從遠程拉一個分支** 有兩個辦法,第一種是: ```git #git fecth origin #git checkout first-branch Branch first-branch set up to track remote branch first-branch from origin. Switched to a new branch 'first-branch'
第二個辦法:
#git checkout -t origin/first-branch Branch first-branch set up to track remote branch first-branch from origin. Switched to a new branch 'first-branch'
tag通常維護一個只讀的版本,再也不進行修改
#git tag -a v1.0 -m "v1.0 ready for publish" //建立一個tag ,名字是"v1.0" #git tag //查看tag v1.0 #git push origin v1.0 //推送tag 到github上 Counting objects: 1, done. Writing objects: 100% (1/1), 162 bytes | 0 bytes/s, done. Total 1 (delta 0), reused 0 (delta 0) To git@github.com:sotrip/gittest.git * [new tag] v1.0 -> v1.0 #git checkout v1.0 切換到這個tag 上
注意 最好不要在tag進行修改東西,就把tag維護成一個只讀的版本
#git rm 2.txt 刪除2.txt 這個文件 #git remote -v 能夠查看遠程的git的地址