最近公司的項(xiàng)目遷移到的新的服務(wù)器上店诗,并且更換了新的倉儲管理系統(tǒng)撒穷。在使用git的時候,發(fā)現(xiàn)一些終端命令在使用時板惑,有一種提筆忘字的感覺橄镜,于是決定整理個Cheat Sheet,方便使用的時候查看冯乘。
1洽胶,git 的安裝
對于Mac,gif的安裝有兩種方式:第一種是安裝homebrew裆馒,然后通過homebrew安裝git,安裝homebrew:終端輸入:
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
第二種方法更簡單姊氓,也是推薦的方法,就是直接從AppStore安裝Xcode喷好,Xcode集成了Git翔横,不過默認(rèn)沒有安裝,你需要運(yùn)行Xcode梗搅,選擇菜單“Xcode”->“Preferences”禾唁,在彈出窗口中找到“Downloads”,選擇“Command Line Tools”无切,點(diǎn)“Install”就可以完成安裝了荡短。
對于Windows,可以從Git官網(wǎng)直接,然后按默認(rèn)選項(xiàng)安裝即可。官網(wǎng)地址:https://git-scm.com/downloads哆键。
2掘托,創(chuàng)建
clone遠(yuǎn)程倉庫
$ git clone ssh://user@domain.com/repo.git
如果本地本地已經(jīng)有g(shù)it倉儲,可以將本地倉儲和遠(yuǎn)程倉儲關(guān)聯(lián)起來籍嘹,執(zhí)行下面命令之前需要先切換到本地目錄git倉儲目錄下
$ git remote add origin ssh://user@domain.com/repo.git
本地創(chuàng)建新
$ git init
3, 本地更改
查看本地更改
$ git status
查看更改內(nèi)容
$ git diff
注:查看具體的文件的更改內(nèi)容 $ git diff + <file>
將本地更改添加提交
$ git add
注:可以一次提交多個文件 $ git add + <file> <file>闪盔, 提交本地所有更改 $ git add .
提交本地更改
$ git commit -m "wrote a changes state"
4弯院,提交記錄和版本控制
查看提交記錄
$ git log
返回上一版本
$ git reset --hard HEAD^
注:HEAD表示當(dāng)前版本,HEAD上一版本泪掀,HEAD^上上一版本听绳, HEAD~數(shù)字,上多少個版本族淮。
返回指定版本
$ git reset --hard + 版本的id
查看輸入指令記錄
$ git reflog
5, 分支管理和標(biāo)簽
新建分支
$ git branch dev
切換分支
$ git checkout dev
注:$ git checkout -b dev 創(chuàng)建dev分支辫红,然后切換到dev分支
查看分支
$ git branch
刪除分支
$ git branch -d dev
打標(biāo)簽
$ git tag <tagname>
查看標(biāo)簽
$ git tag
查看標(biāo)簽信息
$ git show <tagname>
將本地添加標(biāo)簽推送到遠(yuǎn)程
$ git push origin <tagname>
注:一次性推送全部尚未推送到遠(yuǎn)程的本地標(biāo)簽,$ git push origin --tags
刪除標(biāo)簽
$ git tag -d <tagname>
合并分支
$ git merge dev
消除提交歷史的分叉
$ git rebase
6,配置
設(shè)置用戶信息
$ git config --global user.name "Your Name"
$ git config --global user.email "email@example.com"
簡化替換
$ git config --global alias.st status
注:st就表示status
查看配置文件
$ cat .git/config
7祝辣, 其他
將本地分支和遠(yuǎn)程分支建立關(guān)聯(lián)
git branch --set-upstream-to <branch-name> origin/<branch-name>
撤銷暫存區(qū)的修改
$ git reset HEAD <file>
相關(guān)的忽略文件.gitignore的配置可以參考:https://github.com/github/gitignore