Git版本控制和連接GitHub
一、Git簡(jiǎn)介
1. Git概述
Git先進(jìn)的分布式 版本控制 系統(tǒng), GitHub是Git常用的代碼托管中心
Git = GitHub
安裝完成后,在任何空白位置上右擊 git bash here 輸入 git --version,有版本提示即成功.
git --verion
二、本地庫(kù)基本操作
1. 創(chuàng)建本地倉(cāng)庫(kù)repository
- 首先,在合適的位置上 創(chuàng)建空白目錄(倉(cāng)庫(kù)),
git命令有很多跟linux相通的 例如cd、ls等,內(nèi)置vim
MECHREVO@□□е□□□□ MINGW64 ~/Desktop
$ mkdir MyGitRepo # 創(chuàng)建目錄
MECHREVO@□□е□□□□ MINGW64 ~/Desktop
$ cd MyGitRepo/
MECHREVO@□□е□□□□ MINGW64 ~/Desktop/MyGitRepo
$
- 其次, 通過(guò) 初始化 倉(cāng)庫(kù),使得該目錄可以被Git管理
MECHREVO@□□е□□□□ MINGW64 ~/Desktop/MyGitRepo
$ git init
Initialized empty Git repository in C:/Users/Administrator/Desktop/MyGitRepo/.git/
MECHREVO@□□е□□□□ MINGW64 ~/Desktop/MyGitRepo (master)
$ ls -la # 查看當(dāng)前文件目錄情況
total 12
drwxr-xr-x 1 MECHREVO 197121 0 7月 22 12:54 ./
drwxr-xr-x 1 MECHREVO 197121 0 7月 22 12:49 ../
drwxr-xr-x 1 MECHREVO 197121 0 7月 22 12:54 .git/
.git是隱藏文件目錄,里面存放的是跟蹤管理版本庫(kù),一般不要隨意修改,否則會(huì)破壞Git倉(cāng)庫(kù)不能用
- 設(shè)置簽名, 區(qū)分 不同的開(kāi)發(fā)人員身份 ,這里設(shè)置的簽名和登錄遠(yuǎn)程庫(kù)(代碼托管中心)的賬號(hào)、密碼沒(méi)有任何關(guān)系. 簽名分 項(xiàng)目(倉(cāng)庫(kù))級(jí)別 適用當(dāng)前本地庫(kù)范圍 和 系統(tǒng)用戶級(jí)別 適用當(dāng)前操作系統(tǒng)的用戶都存在的時(shí)候,優(yōu)先項(xiàng)目級(jí)別. 至少又一個(gè).
# 項(xiàng)目級(jí)別/倉(cāng)庫(kù)級(jí)別
$ git config user.name jackmin
$ git config user.email xxxxxx@xxx.com
# 系統(tǒng)用戶級(jí)別
$ git config --global user.name jackmin
$ git config --global user.email xxxx@xxx.com
- 查看git狀態(tài)
MECHREVO@□□е□□□□ MINGW64 ~/Desktop/MyGitRepo (master)
$ git status On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
2. 補(bǔ)充版本庫(kù)(Repository)知識(shí)
- 工作區(qū)有一個(gè)隱藏目錄
.git,這個(gè)不算工作區(qū),而是Git的版本庫(kù)。 - Git的版本庫(kù)里存了很多東西,其中最重要的就是稱為stage(或者叫index)的暫存區(qū),還有Git為我們自動(dòng)創(chuàng)建的第一個(gè)分支
master,以及指向master的一個(gè)指針叫HEAD。

- 有分支和
HEAD的概念,我們把文件往Git版本庫(kù)里添加的時(shí)候,是分兩步執(zhí)行的:
? 第一步是用git add把文件添加進(jìn)去,實(shí)際上就是把文件修改添加到暫存區(qū);
? 第二步是用git commit提交更改,實(shí)際上就是把暫存區(qū)的所有內(nèi)容提交到當(dāng)前分支master。(這里的master不是github上的)
3. 管理修改
- 嘗試查看當(dāng)前的status
MECHREVO@□□е□□□□ MINGW64 ~/Desktop/MyGitRepo (master)
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
text.txt # 紅色
nothing added to commit but untracked files present (use "git add" to track)
text.txt 是紅色字體,表明當(dāng)前文件存在工作區(qū),但是**需要提交到暫存區(qū) **
- 通過(guò)提交到暫存區(qū)
git add <file>,文件名支持正則匹配那種 (如果存在多個(gè)后綴是.txt文件,用*.txt)
$ git add *.txt # 所有以.txt結(jié)尾的文本都會(huì)被加入到暫存區(qū)
warning: LF will be replaced by CRLF in text.txt.
The file will have its original line endings in your working directory
# 再次查看git狀態(tài)
$ git status On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: text.txt # 綠色
new file: text.txt 是 綠色字體 表示 已經(jīng)加到了暫存區(qū) , 需要提交到master
如果要從 暫存區(qū)撤回 , 執(zhí)行git rm --cached <file>也是可以用正則匹配的
$ git rm --cached <file>
- 執(zhí)行 git commit text.txt將暫存區(qū)文件提交到master,會(huì)默認(rèn)打開(kāi)vim編輯窗口要求在第一行輸入 commit message
$ git commit text.txt

通過(guò)這種方式必須要輸入,否則不予許commit;完畢再次查看當(dāng)前git狀態(tài)
$ git status
On branch master
nothing to commit, working tree clean
git diff HEAD -- readme.txt 命令可以查看工作區(qū)和版本庫(kù)里面最新版本的區(qū)別
- 打開(kāi)vim輸入覺(jué)得麻煩,可以直接輸入
git commit -m “here are commit message” <file>直接給定commit message然后提交
$ git commit -m "This is the second time to write the message" text.txt
On branch master
nothing to commit, working tree clean
- 如果修改了text.txt文件而沒(méi)有提交,則運(yùn)行
git status會(huì)提示 <font color =red>modified : text.txt</font> 紅色字體,被修改.需要再次add ,commit - 查看git 日志
git log
MECHREVO@□□е□□□□ MINGW64 ~/Desktop/MyGitRepo (master)
$ git log
commit 90f022e03b9137b51d6434caf10058d161b69524 (HEAD -> master)
Author: Jack Chen <1416825008@qq.com>
Date: Mon Jul 22 15:04:12 2019 +0800
This is the second time to write the message
commit 353ed8f1bc9e0d69081612645c91532b59ffe142
Author: Jack Chen <1416825008@qq.com>
Date: Mon Jul 22 14:28:31 2019 +0800
This is the first time to write the message
4. 版本回退
可以看到有兩次提交日志, 而head 指針指向了最近一次修改. 推薦使用 基于索引值的版本進(jìn)退,
每次修改會(huì)有記錄,如果僅僅撤銷上次commit message并修改可用:
$ git commit --amend -m "commit message"
- 把改變當(dāng)前工作目錄中文件恢復(fù)到指定的工作狀態(tài),默認(rèn)最新
$ git checkout "90f022e"
- 當(dāng)使用
git checkout --hard "90f022e"撤銷了這幾次commit時(shí),又想重做這幾次的commit
git reflog和git reset
$ git reflog
$ git reset "90f022e"
git log 列出的是生效的, 沒(méi)有丟棄的commit, git reflog查找到被git checkout --hard“拋棄”掉的那幾次commit的sha值通過(guò)git reset恢復(fù)到想要的commit上去。
- commit了幾次后發(fā)現(xiàn)錯(cuò)誤地commit到了master分支上了,你只是想commit到feature分支上。
git branch feature然后 git reset --hard origin/master 最后git checkout feature
- 圖片來(lái)源網(wǎng)絡(luò)
Git_3.png
慎用git rebase會(huì)變基,會(huì)產(chǎn)生代碼沖突(蠢人給 push了)
當(dāng)Bob需要從遠(yuǎn)程pull拉取feature時(shí)候,就會(huì)出現(xiàn)代碼沖突.
rebase就是bob->you的變化; merge還是兩個(gè)分支,只不過(guò)在merge后這個(gè)點(diǎn)交匯
rebase 提示錯(cuò)誤一個(gè)個(gè)解決,中途可以退出;merge則是全顯示出來(lái) -
圖片來(lái)源網(wǎng)絡(luò)
Git_4.png
git pull –rebase 的理解:
這個(gè)命令做了以下內(nèi)容:
a.把你 commit 到本地倉(cāng)庫(kù)的內(nèi)容,取出來(lái)放到暫存區(qū)(stash)(這時(shí)你的工作區(qū)是干凈的)
b.然后從遠(yuǎn)端拉取代碼到本地,由于工作區(qū)是干凈的,所以不會(huì)有沖突
c.從暫存區(qū)把你之前提交的內(nèi)容取出來(lái),跟拉下來(lái)的代碼合并 .
三、遠(yuǎn)程庫(kù)基本操作
1.添加遠(yuǎn)程庫(kù)
- 登陸
GitHub,然后,在右上角找到“Create a new repo”按鈕,創(chuàng)建一個(gè)新的倉(cāng)庫(kù) 或者關(guān)聯(lián)已經(jīng)存在的倉(cāng)庫(kù). - 進(jìn)入倉(cāng)庫(kù)目錄點(diǎn)擊 <font color =green>
clone or download</font> 選擇 clone with https 復(fù)制鏈接 例如:https://github.com/JackMin1314/Learning_Skill.git - 查看當(dāng)前庫(kù)的遠(yuǎn)程庫(kù)情況,發(fā)現(xiàn)沒(méi)有任何遠(yuǎn)程庫(kù)提示說(shuō)明
$ git remote -v
- 關(guān)聯(lián)遠(yuǎn)程倉(cāng)庫(kù)
$ git remote add origin https://github.com/JackMin1314/Learning_Skill.git
- 再次查看當(dāng)前遠(yuǎn)程庫(kù)情況,發(fā)現(xiàn)已經(jīng)添加了
$ git remote -v
origin https://github.com/JackMin1314/Learning_Skill.git (fetch)
origin https://github.com/JackMin1314/Learning_Skill.git (push)
2. 克隆遠(yuǎn)程庫(kù)到本地
- 用
git clone <https://...>可以將遠(yuǎn)程庫(kù)復(fù)制到本地.
$ git clone https://github.com/JackMin1314/Learning_Skill.git
MECHREVO@□□е□□□□ MINGW64 ~/Desktop/MyGitRepo ((90f022e...))
$ git clone https://github.com/JackMin1314/Learning_Skill.git
Cloning into 'Learning_Skill'...
remote: Enumerating objects: 24, done.
remote: Counting objects: 100% (24/24), done.
remote: Compressing objects: 100% (21/21), done.
remote: Total 24 (delta 1), reused 15 (delta 0), pack-reused 0
Unpacking objects: 100% (24/24), done.
# 再次查看當(dāng)前目錄,發(fā)現(xiàn)有Learning_Skill 文件夾,進(jìn)入后發(fā)現(xiàn)github上的文件都在里面
MECHREVO@□□е□□□□ MINGW64 ~/Desktop/MyGitRepo ((90f022e...))
$ ls
Learning_Skill/ text.txt
3.創(chuàng)建與合并分支
- 查看當(dāng)前分支和創(chuàng)建
git branch <branchname>刪除git branch -d <branchname>分支
$ git branch # 查看分支和當(dāng)前分支
* master
$ git branch myBranch # 添加分支myBranch
$ git branch -d myBranch # 刪除分支
創(chuàng)建成功后,再次查看分支
$ git branch # 查看分支和當(dāng)前分支
* master
myBranch
$ git checkout myBranch # 切換到指定分支
$ git branch
$ git branch # 查看分支和當(dāng)前分支
master
* myBranch
尚未推送或分享給別人的本地修改可以執(zhí)行變基操作來(lái)清理歷史(commit message)----git rebase
已推送至別處的提交不執(zhí)行變基操作
-
--pretty=oneline表示只顯示一行哈希值和提交說(shuō)明
git log --graph --pretty=oneline --abbrev-commit
4.提交代碼到遠(yuǎn)程庫(kù)
嘗試提交一下到遠(yuǎn)程庫(kù)
$ git push origin master
但是報(bào)錯(cuò)!!!
$ git push origin master
To https://github.com/JackMin1314/Learning_Skill.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/JackMin1314/Learning_Skill.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
根據(jù)提示需要先git pull....說(shuō)明本地庫(kù)不是最新的,嘗試git pull origin master,還是失敗
$ git pull --rebase origin master
From https://github.com/JackMin1314/Learning_Skill
* branch master -> FETCH_HEAD
* [new branch] master -> origin/master
First, rewinding head to replay your work on top of it...
Applying: This is the first time to write the message
Applying: This is the second time to write the message
Applying: Sort out some files
四、添刪遠(yuǎn)程庫(kù)和username、email等問(wèn)題
- 輸入
git config --h可以查看很多命令,例如查看全局配置文件(系統(tǒng)級(jí)別)中的信息
$ git config --global -l user.name=Jack Chen user.email=1416825008@qq.com
- 輸入
git config --global -l可以查看所有信息 - 查看文檔選擇--unset remove a variable: name [value-regex]
$ git config --global --unset user.email # 清空 user.email 和 user.username
$ git config --global --unset user.name
- 查看信息沒(méi)有任何顯示直接看username 和email按
git config --global --get user.name
$ git commit --amend -m head
*** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address
-
重新添加name,email用
user.email,user.name(這里設(shè)置全局級(jí)別的)
$ git config --global user.email "1416825008@qq.com"
$ git config --global user.name "jackmin"
$ git config --global -l
user.email=1416825008@qq.com
user.name=jackmin
下面重新開(kāi)始
# 克隆最新的遠(yuǎn)程庫(kù)
$ git clone https://github.com/JackMin1314/Learning_Skill.git
$ git pull https://github.com/JackMin1314/Learning_Skill.git
# 修改了幾次后提交,再push報(bào)錯(cuò)
$ git push origin master To https://github.com/JackMin1314/Learning_Skill.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/JackMin1314/Learning_Skill.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
# 嘗試用最新的遠(yuǎn)程庫(kù)和本地變基
$ git pull --rebase origin master From https://github.com/JackMin1314/Learning_Skill
* branch master -> FETCH_HEAD
First, rewinding head to replay your work on top of it...
# 查看當(dāng)前狀態(tài)沒(méi)問(wèn)題
$ git status
# 直接提交到遠(yuǎn)程
$ git pull origin master From https://github.com/JackMin1314/Learning_Skill
* branch master -> FETCH_HEAD
Already up to date.
$ git push origin master
Enumerating objects: 2, done.
Counting objects: 100% (2/2), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 447 bytes | 447.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To https://github.com/JackMin1314/Learning_Skill.git
f25daf1..c699f70 master -> master
成功.去github看到修改了對(duì)應(yīng)目錄.
轉(zhuǎn) 載 注 明 出 處 謝 謝!!! ^ _ ^

