本篇筆記參考廖雪峯的git教程,爲方便查看將命令部分提取並記錄下來。git
無心對原做的版權侵犯,如須要學習請到廖雪峯網站學習gitgithub
建立git倉庫緩存
# mkdir learngit && cd learngit # git init
添加一個文件 readme.txt 內容以下:app
Git is a version control system. Git is free software.
將修改的文件加入git緩存學習
# git add readme.txt
而後將緩存提交到本地倉庫網站
git commit -m "wrote a readme file" [master (root-commit) cb926e7] wrote a readme file 1 file changed, 2 insertions(+) create mode 100644 readme.txt
修改readme.txt文件spa
Git is a distributed version control system. Git is free software distributed under the GPL.
而後嘗試提交:指針
$ git add readme.txt $ git commit -m "append GPL" [master 3628164] append GPL 1 file changed, 1 insertion(+), 1 deletion(-)
修改readme.txt文件,改爲以下內容:code
Git is a distributed version control system. Git is free software.
運行 git status 查看git狀態orm
# git status # On branch master # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: readme.txt # no changes added to commit (use "git add" and/or "git commit -a")
能夠看到readme.txt被修改,查看修改的具體內容
# 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.
說明:
git diff #是工做區(work dict)和暫存區(Stage)的比較
git diff --cached #是暫存區(Stage)和分支(Master)的比較
提交修改到緩存中
# git add readme.txt
咱們再運行git status
看看當前倉庫的狀態
# git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: readme.txt #
依然是readme.txt被修改,此時能夠放心提交到倉庫
# git commit -m "add distributed" [master ea34578] add distributed 1 file changed, 1 insertion(+), 1 deletion(-)
提交後,咱們再用git status
命令看看倉庫的當前狀態:
# git status
# On branch master
nothing to commit (working directory clean)
如今有三個版本:
版本1:wrote a readme file
Git is a version control system. Git is free software.
版本2:add distributed
Git is a distributed version control system. Git is free software.
版本3:append GPL
Git is a distributed version control system. Git is free software distributed under the GPL.
使用 git log 查看歷史版本
$ git log commit 3628164fb26d48395383f8f31179f24e0882e1e0 Author: Michael Liao <askxuefeng@gmail.com> Date: Tue Aug 20 15:11:49 2013 +0800 append GPL commit ea34578d5496d7dd233c827ed32a8cd576c5ee85 Author: Michael Liao <askxuefeng@gmail.com> Date: Tue Aug 20 14:53:12 2013 +0800 add distributed commit cb926e7ea50ad11b8f9e909c05226233bf755030 Author: Michael Liao <askxuefeng@gmail.com> Date: Mon Aug 19 17:51:55 2013 +0800 wrote a readme file
加上--pretty=oneline
參數能夠只輸出摘要信息
$ git log --pretty=oneline 3628164fb26d48395383f8f31179f24e0882e1e0 append GPL ea34578d5496d7dd233c827ed32a8cd576c5ee85 add distributed cb926e7ea50ad11b8f9e909c05226233bf755030 wrote a readme file
像 3628164fb26d48395383f8f31179f24e0882e1e0 稱爲 commit ID ,用於表明某個版本,經過他能夠回退版本
假如如今想要回退到 add distributed 時的狀態:
$ git reset --hard ea34578d5496d7dd233c827ed32a8cd576c5ee85
若是你保證commit ID前5位惟一,也能夠簡寫:
$ git reset --hard ea3457
或者使用 HEAD^ 表明HEAD指針指向上一個版本:
git reset --hard HEAD^
說明:
版本回退後,即HEAD指針指向上個版本HEAD^
若是想指向上上個版本用:HEAD^^ ,以此類推
若是想指向上100個版本用: HEAD~100
如今已經成功回退到 add distributed 狀態了,此時查看log
$ git log commit ea34578d5496d7dd233c827ed32a8cd576c5ee85 Author: Michael Liao <askxuefeng@gmail.com> Date: Tue Aug 20 14:53:12 2013 +0800 add distributed commit cb926e7ea50ad11b8f9e909c05226233bf755030 Author: Michael Liao <askxuefeng@gmail.com> Date: Mon Aug 19 17:51:55 2013 +0800 wrote a readme file
最新的那個版本append GPL
已經看不到了!,而如今又想恢復到那個最新的版本方法以下:
方法一:若是終端沒關,向上翻記錄找到最新版本的coommit ID號
$ git reset --hard 3628164 HEAD is now at 3628164 append GPL
方法二:若是終端關了,查看git歷史操做尋找新版本的commit ID號
$ git reflog ea34578 HEAD@{0}: reset: moving to HEAD^ 3628164 HEAD@{1}: commit: append GPL ea34578 HEAD@{2}: commit: add distributed cb926e7 HEAD@{3}: commit (initial): wrote a readme file
方法三:若是歷史操做也被其餘操做覆蓋記錄了,查看歷史全部版本號
$ git log --graph --pretty=format:'%h -%d %s (%cr)' --abbrev-commit --reflog
如今既能夠向後回退,也能夠向前恢復,只要有COMMIT ID號便可,查看當前狀態號
$ git rev-parse HEAD
假如當前提交了多個文件a b c,如今只想恢復文件c的上次狀態,其餘文件a 和 b保持當前狀態
$ git checkout HEAD^ -- c
或者
$ git checkout HEAD^ c
解決fatal:remote error:You can't push to git://github.com/username/*.git問題的辦法
今天Git push的時候,發現沒有push權限
fatal:remote error:
You can't push to git://github.com/username/*.git Use git@github.com:username/*.git
解決方法:
git remote rm origin git remote add origin git@github.com:username/resname.git