Git是什么?
Git 屬于分散型版本管理系統(tǒng),是為版本管理而設(shè)計(jì)的軟件。
遠(yuǎn)程倉(cāng)庫(kù)
創(chuàng)建公開密鑰認(rèn)證所需的ssh key
$ ssh-keygen -t rsa -C "your_email@example.com"
Generating public/private rsa key pair.
Enter file in which to save the key
(/Users/your_user_directory/.ssh/id_rsa): 按回車鍵
Enter passphrase (empty for no passphrase): 輸入密碼
Enter same passphrase again: 再次輸入密碼
id_rsa 文件是私有密鑰,id_rsa.pub 是公開密鑰。
添加公開密鑰
登錄github,點(diǎn)擊右上角頭像,選擇Settings,再點(diǎn)擊SSH and GPG keys,設(shè)置SSH keys。點(diǎn)擊New SSH key 把id_rsa.pub 文件里的內(nèi)容添加進(jìn)去。完成以上設(shè)置后,就可以用手中的私人密鑰與GitHub 進(jìn)行認(rèn)證和通信了。讓我們來(lái)實(shí)際試一試。
$ ssh -T git@github.com
The authenticity of host 'github.com (207.97.227.239)' can't be established.
RSA key fingerprint is fingerprint值 .
Are you sure you want to continue connecting (yes/no)? 輸入yes
Hi hirocastest! You've successfully authenticated, but GitHub does not
provide shell access.
配置
git config --global user.name xxx #方便產(chǎn)品經(jīng)理找(懟)你
git config --global user.email yyy #方便產(chǎn)品經(jīng)理找(懟)你
git config --global push.default simple
git config --global core.quotepath false #防止文件名變成數(shù)字
git config --global core.editor "vim" #使用vim編輯提交信息
基本操作
git init——初始化倉(cāng)庫(kù)
要使用Git 進(jìn)行版本管理,必須先初始化倉(cāng)庫(kù)。
mkdir git-demo
cd git-demo
git init
Initialized empty Git repository in /Users/hirocaster/github/github-book
/git-tutorial/.git/
git status——查看倉(cāng)庫(kù)的狀態(tài)
git status命令用于顯示Git 倉(cāng)庫(kù)的狀態(tài)。
$ git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)
git add——向暫存區(qū)中添加文件
$ git add README.md
$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: README.md
#
git commit——保存?zhèn)}庫(kù)的歷史記錄
git commit命令可以將當(dāng)前暫存區(qū)中的文件實(shí)際保存到倉(cāng)庫(kù)的歷史記錄中。
$ git commit -m "first"
[master (root-commit) 9f129ba] first
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 README.md
-m 參數(shù)后的"First commit"稱作提交信息,是對(duì)這個(gè)提交的概述。
git log——查看提交日志
git log命令可以查看以往倉(cāng)庫(kù)中提交的日志。
$ git log
commit 33c1c74e376fd66d0747a8093c4c73b7e9d6427a
Author: wang7211401 <wang7211401@163.com>
Date: Thu Jul 6 16:04:15 2017 +0800
first
git diff——查看更改前后的差別
git diff命令可以查看工作樹、暫存區(qū)、最新提交之間的差別。
git remote add——添加遠(yuǎn)程倉(cāng)庫(kù)
在GitHub 上創(chuàng)建的倉(cāng)庫(kù)路徑為“git@github.com:用戶名/git-tutorial.git”。現(xiàn)在我們用git remote add命令將它設(shè)置成本地倉(cāng)庫(kù)的遠(yuǎn)程倉(cāng)庫(kù)。
$ git remote add origin git@github.com:github-book/git-tutorial.git
git push——推送至遠(yuǎn)程倉(cāng)庫(kù)
推送至master 分支
$ git push -u origin master
Counting objects: 20, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (10/10), done.
Writing objects: 100% (20/20), 1.60 KiB, done.
Total 20 (delta 3), reused 0 (delta 0)
To git@github.com:github-book/git-tutorial.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
git pull——獲取最新的遠(yuǎn)程倉(cāng)庫(kù)分支
git clone——獲取遠(yuǎn)程倉(cāng)庫(kù)
git branch——顯示分支一覽表
git branch命令可以將分支名列表顯示,同時(shí)可以確認(rèn)當(dāng)前所在分支。
$ git branch
* master
git checkout -b——?jiǎng)?chuàng)建、切換分支
$ git checkout -b feature-A
Switched to a new branch 'feature-A'
git merge——合并分支
$ git checkout master
Switched to branch 'master'
$ git merge --no-ff feature-A
git log --graph——以圖表形式查看分支
git reset——回溯歷史版本
$ git reset --hard 33c1c7
git reflog ——查看當(dāng)前倉(cāng)庫(kù)執(zhí)行過(guò)的操作的日志。
$ git reflog
33c1c74 HEAD@{0}: checkout: moving from feature-A to master
33c1c74 HEAD@{1}: checkout: moving from master to feature-A
33c1c74 HEAD@{2}: commit (initial): first
git rebase -i——壓縮歷史
git stash——用于保存和恢復(fù)工作進(jìn)度
原則
1.git push 之前必須 git pull
2.git pull 之前必須 git commit
3.git commit 之前有時(shí)必須 git add