學(xué)習(xí)資料:廖雪峰的Git教程 http://www.liaoxuefeng.com/
1.安裝git for windows
下載 msysgit是Windows版的Git狱掂,從https://git-for-windows.github.io下載(國內(nèi)鏡像)
打開 Git Bash 表示安裝成功
git config --global user.name “Your Name”
git config --global user.email "Your email“
git config --global
全局設(shè)置
2.創(chuàng)建版本庫
生成空目錄
mkdir repositoryname
新建目錄
cd repositoryname
打開目錄
pwd
顯示當(dāng)前目錄
將目錄git init
git init
將當(dāng)前目錄創(chuàng)建為倉庫
ls -ah
顯示.git目錄
3.版本控制
向git暫存區(qū)中添加文件
git add
添加文件到暫存區(qū)(Stage)(一次只能添加一個文件 可反復(fù)使用)
git commit --m"提交描述"
提交文件提交到分支(可提交多個文件)
git status
查看倉庫當(dāng)前修改狀態(tài)
git diff
查看修改內(nèi)容
版本號與HEAD指針
git log
查看修改信息
git log --pretty=oneline
查看簡化修改信息
commit id
版本號
HEAD
當(dāng)前版本
HEAD^
上個版本
HEAD~100
向上一百個版本
git reset
回退命令
git reset--hard HEAD
回退到上個版本
git reset --hard commit_id
回退到對應(yīng)id版本
git reflog
用戶命令歷史記錄
撤銷修改
git checkout --file
放棄工作區(qū)的修改
git reset HEAD file
放棄暫存區(qū)的修改
刪除文件
rm file
刪除文件(需git commit
提交一次
)
git checkout --file
放棄工作去修改 (相當(dāng)于還原刪除文件)
4.遠程倉庫
本地和github倉庫交互
1.創(chuàng)建SSH Key
在用戶主目錄下查看.ssh目錄弯洗。若沒有雏节,打開GIt Bash(for windows)
$ssh-keygen -t rsa -C "yourGithub@example.com"
創(chuàng)建SSH Key
然后查看.ssh目錄,檢查id_rsa
和id_rsa.pub
id_rsa
私鑰
id_rsa.pub
公鑰
2.登陸GIthub 創(chuàng)建"SSH Key"
Title 自定義
Key 填入id_rsa.pub
3.添加遠程倉庫
git remote add orgin git@github.com:yourGithub/yourRepoName.git
關(guān)聯(lián)遠程倉庫git push -u origin master
將當(dāng)前master
分支推送到遠程庫上(第一次)git push origin master
將當(dāng)前master
分支推送到遠程庫上4.從遠程庫克隆到本地
git clone git@github.com:yourGitHub/yourRepoName.git
克隆本地庫(最好是空目錄)cd yourRepoName.git
打開新克隆下來的目錄ls
查看文件Git支持多種協(xié)議邑跪,包括https次坡,但通過ssh支持的原生git協(xié)議速度最快。
5.分支管理
創(chuàng)建画畅、合并和刪除分支
$git checkout -b yourBranch
創(chuàng)建并轉(zhuǎn)到新分支
$git branch yourBranch
創(chuàng)建yourBranch
分支
$git checkout yourBranch
跳轉(zhuǎn)到yourBranch
分支
git branch
列出所有分支
git merge yourBranch
將yourBranch
分支和并到你當(dāng)前分支
git branch -d yourBranch
刪除yourBranch
分支