(一)help查看函數(shù)manual
git help 函數(shù)名
(二)圖解Git命令—方便理解記憶
(二 )撤銷(xiāo)最后一條commit(s)
問(wèn)題描述:I committed the wrong files to Git.How can I undo that commit?
$ git commit -m "Something terribly misguided" (1)
$ git reset HEAD~ (2)
# <edit files as necessary> (3)
$ git add ... (4)
$ git commit -c ORIG_HEAD (5)
(三)放棄暫存區(qū)改變(unstaged changes)
問(wèn)題描述:How do I discard changes in my workingcopy that are not in the index?
For a specific file use:
$git checkout path/to/file/to/revert
For all unstaged files use:
$git checkout -- .(注意末尾的'.')
(四)commit前撤銷(xiāo)git? add修改
問(wèn)題描述:mistakenly added files using the command:git add myfile.txt. I have not yet run git commit. Is there a way to undo this, so these files won't be included in the commit?
$ git config --global alias.unadd 'reset HEAD --'
$ git rm -r --cached .? # -r參數(shù)代表遞歸
(五)git diff 和 git diff --cached 、git diff HEAD的比較
$ git diff? ? # 是工作區(qū)(work directory)和暫存區(qū)(stage)的比較
$ git diff --cached? ? # 是暫存區(qū)(stage)和倉(cāng)庫(kù)|分支(master)的比較
$ git diff HEAD # 可以查看工作區(qū)和版本庫(kù)(master)的差別
(六)git revert和git reset的區(qū)別?
git revert 也是撤銷(xiāo)命令及皂,區(qū)別在于reset是指向原地或者向前移動(dòng)指針甫男,git revert是創(chuàng)建一個(gè)commit來(lái)覆蓋當(dāng)前的commit,指針向后移動(dòng)验烧。
git revert 是撤銷(xiāo)某次操作查剖,此次操作之前的commit都會(huì)被保留,而git reset 是撤銷(xiāo)某次提交噪窘,但是此次之后的修改都會(huì)被退回到暫存區(qū)中笋庄。
具體一個(gè)例子效扫,假設(shè)有三個(gè)commit(commit1,commit2,commit3),使用 git status:
commit3: add test3.c
commit2: add test2.c
commit1: add test1.c
當(dāng)執(zhí)行g(shù)it revert HEAD~1時(shí)(撤銷(xiāo)倒數(shù)第二個(gè)操作)直砂,第二個(gè)操作即commit2這個(gè)操作被撤銷(xiāo)了菌仁,使用git log可以看到:
commit1:add test1.c
commit3:add test3.c
由于git revert不會(huì)回退到暫存區(qū)中,所以使用git status 沒(méi)有任何變化
如果換做執(zhí)行g(shù)it reset --soft(默認(rèn)) HEAD~1后静暂,運(yùn)行g(shù)it log可以看到
commit2: add test2.c
commit1: add test1.c
運(yùn)行g(shù)it status济丘,可以看到test3.c處于暫存區(qū)了,準(zhǔn)備提交洽蛀。
但如果換做執(zhí)行g(shù)it revert后摹迷,
顯示:HEAD is now at commit2,運(yùn)行g(shù)it log可以看到
commit2: add test2.c
commit1: add test1.c
運(yùn)行g(shù)it status郊供, 則沒(méi)有任何變化
所以峡碉,git revert與git reset最大的不同是,git revert 僅僅是撤銷(xiāo)某次提交驮审,而git reset會(huì)將撤銷(xiāo)點(diǎn)之后的操作
都回退到暫存區(qū)中鲫寄。
(七)git rm --cached <file>和git reset -- <file>區(qū)別
(七)git rm與git rm --cached
?????? 當(dāng)我們需要?jiǎng)h除暫存區(qū)(index)或分支(master)上的文件, 同時(shí)工作區(qū)也不需要這個(gè)文件了, 可以使用:
$ git rm “文件路徑”
?????? 當(dāng)我們需要?jiǎng)h除暫存區(qū)或分支上的文件, 但本地又需要使用, 只是不希望這個(gè)文件被版本控制, 可以使用:
$ git rm --cached “文件路徑”
(八)git status簡(jiǎn)介
?????? git status命令可以列出當(dāng)前目錄所有還沒(méi)有被git管理的文件和被git管理且被修改但還未提交(git commit)的文件。
結(jié)果解讀:
"Changes to be committed"中所列的內(nèi)容是在Index中的內(nèi)容疯淫,commit之后進(jìn)入Git Directory地来。
“Changed but not updated”中所列的內(nèi)容是在Working Directory中的內(nèi)容,add之后將進(jìn)入Index熙掺。
“Untracked files”中所列的內(nèi)容是尚未被Git跟蹤的內(nèi)容未斑,add之后進(jìn)入Index。
(九)歷史版本恢復(fù)
$ git log # 查看最近的提交的歷史版 (1)
$ git reset --hard HEAD^ # ^上一個(gè)版本币绩,^^上上一個(gè)版本颂碧,多的話用HEAD~10? (2)
$ git reflog # 如果reset(恢復(fù))到某個(gè)歷史版本后,這個(gè)版本之前的狀態(tài)就會(huì)查詢(xún)不出來(lái)(具體解釋見(jiàn)六)类浪。為了還可以恢復(fù)到未來(lái)某個(gè)版本载城,我們需要查詢(xún)最近所做的操作,可以通過(guò)git reflog 命令费就,得到commit_id之后再通過(guò)git reset --hard HEAD commit_id命令來(lái)達(dá)到效果(3)
(十)創(chuàng)建和切換分支?
$ git checkout -b "分支名" # 新建分支并切換到新分支(1)=(2)+(3)
$ git checkout "分支名" # 切換分支(2)
$ create a new brache "分支名" # 創(chuàng)建新分支(3)
$ git brach -d "分支名" # 刪除分支
$ git merge "分支名" # 表示將指定分支合并到當(dāng)前所在分支诉瓦,一定要理解這句話不然會(huì)合并分支錯(cuò)誤。
參考資料推薦:Git參考手冊(cè)?力细、Git睬澡、圖解Git 、Git教程?