下載與配置
- Windows下載地址
https://git-for-windows.github.io/ - 配置信息
git config --global user.name "Your Name"
git config --global user.email "email@example.com"
:配置信息保存在主目錄下的.gitconfig文件下 - 創(chuàng)建SSH Key
ssh-keygen -t rsa -C "renyangfar@qq.com"
- 查看配置信息
'git config --global --list'
創(chuàng)建與提交
- 創(chuàng)建新的倉(cāng)庫(kù):創(chuàng)建新的文件夾没陡,打開(kāi)慧域,執(zhí)行命令:
git init
- 查看狀態(tài):
git status
- 檢出倉(cāng)庫(kù):克隆一個(gè)遠(yuǎn)程倉(cāng)庫(kù)到本地
git clone /path/to/repository
如:git clone git@github.com:renyangfar/git.git
- 添加到暫存區(qū):
git add <filename>
:添加具體文件到暫存區(qū)
git add *
或者git add .
:添加所有未添加的文件到暫存區(qū) - 添加到本地倉(cāng)庫(kù):
git commit -m '代碼提交信息'
- 提交到遠(yuǎn)程倉(cāng)庫(kù):
git push origin master
:可以把master換成任意分支 - 強(qiáng)制push(不建議)
git push -u origin master -f
- 關(guān)聯(lián)到遠(yuǎn)程服務(wù)器
git remote add origin <server>
如:git remote add origin git@github.com:renyangfar/hello.git - 取消本地目錄下關(guān)聯(lián)的遠(yuǎn)程庫(kù):
git remote remove origin
更新與合并
- 更新本地倉(cāng)庫(kù)同遠(yuǎn)端倉(cāng)庫(kù)保持一致
git pull
:git pull = fetch + merge - 合并其他分支到當(dāng)前分支
git merge <branch>
log
- 查看歷史記錄:
git log
- 查看命令歷史
git reflog
分支
版本回退
- 回退到上一次提交
git reset hard HEAD^
- 回退到上上次提交
git reset --hard HEAD^^
- 回退到某次提交(根據(jù)git log查看commit id)
git reset --hard <commit id>
創(chuàng)建新分支
git checkout -b dev
相當(dāng)于:git branch dev git checkout dev
常用命令
- 幫助:
git help
或者git --help
- 查看當(dāng)前目錄: (Print Working Directory)
pwd
- 切換目錄:(Change Directory)
cd <directory>
- 查看當(dāng)前目錄下的內(nèi)容:(List)
ls
- 創(chuàng)建目錄 (Make Directory)
mkdir <directory>
- 創(chuàng)建文件崭倘,如 touch index.html
touch <file>
- 查看文件全部?jī)?nèi)容,如 cat index.html
cat <file>
- 刪除文件
rm <file>
- 移動(dòng)文件或重命名
mv <file>
- 復(fù)制文件
cp <file> <file>
快捷鍵
自動(dòng)補(bǔ)全:tab鍵芬失,連按兩次顷蟆,匹配所有命令
忽略已經(jīng)track的文件
git rm -r --cached .
git add
git commit -m 'xxx'
git push xxx