git initgit
git add file.txt 添加github
git commit -m "XXX" 提交app
git statusui
git diffserver
git log 查看提交歷史開發
git log --pretty=oneline 歷史一行一行顯示rem
git reset --hard head^(回退到上一個版本)文檔
git reset --hard 版本號前幾位 it
git reflog 查看命令歷史io
git checkout -- readme.txt 丟棄工做區的修改
git reset HEAD file 不但改亂了⼯工做區某個⽂文件的內容,還添加到了暫存區時,想丟棄修改,分兩步 一、reset 二、checkout
刪除文件:
rm 文件名
git rm 文件名
git commit -m "rm 文件名"
rm 文件名
git checkout -- test.txt
$ git push origin master 本地master分⽀支的最新修改推送⾄至GitHub
git remote add origin git@server-name:path/repo-name.git ; 關聯一個遠程庫
git push -u origin master 第一次推送master分⽀支的全部內容;
從遠程庫克隆
git clone git@github.com:michaelliao/gitskills.git
Cloning into 'gitskills'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
$ cd gitskills
$ ls
README.md
$ git checkout -b dev 建立dev分支,而後切換到dev分⽀支:
git checkout 命令加上 -b 參數表⽰示建立並切換,至關於如下兩條命令:
$ git branch dev 查看分支
$ git checkout dev 切換分支
$ git branch 查看當前分支
* dev
master
git branch 命令會列出全部分⽀支,當前分⽀支前⾯面會標⼀一個*號。
查看分支:git branch
建立分支:git branch name
切換分支:git checkout name
建立+切換分支: git checkout -b name
合併某分支到當前分支: git merge name
刪除分支: git branch -d name
Git用<<<<<<<,=======,>>>>>>>標記出不一樣分支的內容
git log --graph --pretty=oneline --abbrev-commit 查看分支的合併狀況
合併分支時,若是可能,Git會⽤用「Fast forward」模式,刪除分支後,會丟掉分支信息。
若是要強制禁用「Fast forward」模式,Git就會在merge時生成一個新的commit,這
樣,從分支歷史上就能夠看出分⽀支信息。
git merge --no-ff -m "merge with no-ff" dev --no-ff參數,表示禁用「Fast forward」
由於本次合併要建立一個新的commit,因此加上-m參數,把commit描述寫進去。
git stash 把當前工做現場「儲藏」起來,等之後恢復現場後繼續工做
git checkout -b issue-編號 增長bug的分支
git merge --no-ff -m "merged bug fix 101" issue-101 bug合併到主幹
git branch -d issue-編號 刪除bug分支
git stash list 查剛纔的工做現場
git stash apply 恢復現場 git stash apply stash@{0}
git stash drop 刪除stash內容
git stash pop 恢復現場的同時,刪除stash內容
開發一個新feature,最好新建一個分支;
若是要丟棄一個沒有被合併過的分支,能夠經過 git branch -D name 強行刪除。
git remote 查看遠程庫的信息
git remote -v 查看遠程庫,顯示更詳細的信息
多人協做
查看遠程庫信息,使用 git remote -v ;
• 本地新建的分支若是不推送到遠程,對其餘人就是不可見的;
• 從本地推送分支,使用 git push origin branch-name ,若是推送失敗,先用git pull抓
取遠程的新提交;
• 在本地建立和遠程分支對應的分支,使用 git checkout -b branch-name origin/branch-
name ,本地和遠程分支的名稱最好一致;
• 創建本地分支和遠程分支的關聯,使用 git branch --set-upstream branch-name
origin/branch-name ;
• 從遠程抓取分支,使用 git pull ,若是有衝突,要先處理衝突。
標籤
git tag v1.0 建立標籤
git tag 查看標籤
git log --pretty=oneline --abbrev-commit 找到歷史提交的commit id
git tag 版本號 commit id 對某次的提交打標籤
git show 版本號 查看標籤信息
還能夠建立帶有說明的標籤,⽤用-a指定標籤名,-m指定說明⽂文字:
$ git tag -a v0.1 -m "version 0.1 released" 3628164
還能夠經過-s⽤用私鑰簽名⼀一個標籤:
$ git tag -s v0.2 -m "signed version 0.2 released" fec145a
簽名採⽤用PGP簽名,所以,必須⾸首先安裝gpg(GnuPG),若是沒有找到gpg,或者沒有
gpg密鑰對,就會報錯:
gpg: signing failed: secret key not available
error: gpg failed to sign the data
error: unable to sign the tag
若是報錯,請參考GnuPG幫助⽂文檔配置Key。
git tag -d 版本號 刪除標籤
git push origin 版本號 推送某個標籤到遠程
git push origin --tags 一次性推送所有還沒有推送到遠程的本地標籤
若是標籤已經推送到遠程,要刪除遠程標籤就麻煩一點,先從本地刪除:
$ git tag -d v0.9
而後,從遠程刪除。刪除命令也是push,可是格式以下:
$ git push origin :refs/tags/v0.9
git的其餘設置
$ git config --global color.ui true 讓Git顯示顏色,會讓命令輸出看起來更醒目:
配置別名
$ git config --global alias.st status