一,原理css
首先,咱們要明白Git是什麼,它是一個管理工具或軟件,用來管理什麼的呢?固然是在軟件開發過程當中管理軟件或者文件的不一樣版本的工具,一些做家也能夠用這個管理本身創做的文本文件,由Linus開發的,也是Linux系統開發所用到的分佈式版本控制軟件。一說到分佈式,其實就是每一個電腦都有這麼個版本庫,而摒棄了傳統的中心文件系統服務,因此每一個人都能保存、還原以前的版本。在航天二院實習的時候,mentor讓我改一個軟件的Bug,因爲國企沒有外網,因此不能用github,院裏面也沒有搭建本身的Git文件服務器,因此每次改Bug以前都要備份一個以前的版本,以日期命名,並且後面還容易忘記那個版本有哪些功能模塊,就咱們兩我的開發一個軟件都把版本搞錯了好幾回,真心頭痛。linux
二,Git經常使用命令git
git的經常使用命令有:init、add、rm、mv、commit、push、pull、clone、log、checkout、megre、status、branch、diff、config、remote、fetch、reset、tag、show、stash、grep、rebase、gc等。github
1,本地操做shell
Git命令 | 解釋 | 撒旦法備註 |
git init | 初始化Git環境 | 先cd到要管理的文件夾中 |
git add *.txt / . | 添加文件到暫存區 | . 是將全部文件都添加到暫存區 |
git status | 查看文件的狀態 | |
git commit -m 'First commit' | 提交更新到版本庫 | -m 是message的縮寫,即每次添加更新的備註 |
git log | 查看提交更新的記錄 | |
git rm *.txt | 刪除文件 | |
git mv a.txt b.txt | 修改文件名 | 將a.txt的文件名改成b.txt |
2,遠程操做segmentfault
Git命令 | 解釋 | 撒旦法備註 |
git config --global user.name "your name" | 設置username,由於github每次commit都會記錄他們 | github註冊時的用戶名 |
git config --global user.email "your_email@youremail.com" | 設置email | github註冊時的郵箱地址 |
git remote add origin https://...git | 查看文件的狀態與Github創建遠程鏈接 | 如今github網站上創建倉庫 |
git push -u origin master | 提交推送到Github的倉庫中 | master分支能夠是其餘分支 |
在遠程操做Github以前要進行ssh key的配置,網上有不少教程能夠參考。大概分三步:bash
在本地建立ssh key,$ ssh-keygen -t rsa -C "your_email@youremail.com" (github網站上註冊的郵箱),回車後會在當前用戶.ssh文件夾下生成id_rsa、id_rsa.pub、known_hosts三個文件服務器
打開id_rsa.pub,複製裏面的key, 回到github網站,進入Account Settings,左邊選擇SSH Keys,Add SSH Key, Title隨便填,粘貼key。ssh
驗證是否成功,在本地git bash下輸入,$ ssh -T git@github.com ,回車就會看到:You’ve successfully authenticated, but GitHub does not provide shell access 。這就表示已成功連上github。分佈式
參考連接:
http://www.bootcss.com/p/git-guide/
Git命令腦圖(圖片來自Github)
3,Git分支管理模型Pic
三,Demo
1,初始化一個git倉庫,新建一個code.txt文件,提交到git版本庫,如今git開始管理code.txt文件了
ckjbug@ckjbug MINGW64 ~/Desktop $ mkdir gituse ckjbug@ckjbug MINGW64 ~/Desktop $ cd gituse ckjbug@ckjbug MINGW64 ~/Desktop/gituse $ git init Initialized empty Git repository in C:/Users/Enz/Desktop/gituse/.git/ ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git status On branch master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) code.txt nothing added to commit but untracked files present (use "git add" to track) ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git add code.txt ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git status On branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: code.txt ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git commit -m 'First commit' [master (root-commit) 0f67f2c] First commit 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 code.txt ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git status On branch master nothing to commit, working tree clean
2,修改文件,在code.txt添加一行代碼,而後用checkout回到修改前(即沒有添加代碼前的版本,這裏以前寫入的代碼沒有了,空文本),這只是checkout的一種用法,更可能是後面學習的git的分支轉換。
ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git status On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: code.txt no changes added to commit (use "git add" and/or "git commit -a") ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git checkout -- code.txt ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ notepad code.txt
3,仍是上面這個功能,回到文本修改前的狀態,可是咱們修改文件後已經添加到暫存區了,這時使用git checkout code.txt已經沒用了,能夠用git reset回到沒有添加到暫存區的狀態(最初始的狀態)
ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ clear ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git status On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: code.txt no changes added to commit (use "git add" and/or "git commit -a") ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git add code.txt ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git checkout code.txt ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git reset Unstaged changes after reset: M code.txt ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git status On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: code.txt no changes added to commit (use "git add" and/or "git commit -a") ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git checkout -- code.txt
4,添加、切換、刪除分支(添加develop開發者的工做分支)
ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git branch * master ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git branch develop ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git branch develop * master ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git checkout develop Switched to branch 'develop' M code.txt ckjbug@ckjbug MINGW64 ~/Desktop/gituse (develop) $ git branch * develop master ckjbug@ckjbug MINGW64 ~/Desktop/gituse (develop) $ git checkout master Switched to branch 'master' M code.txt ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git branch develop * master ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git branch -d develop Deleted branch develop (was 0f67f2c). ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git branch * master
5,新建一個開發者develop分支,而後添加develop.txt文本,添加到暫存庫,提交,最後轉換到master分支,發現develop.txt文本不見了,這就是版本管理,每一個分支控制一個版本,當咱們轉回到develop分支下時這個devel.txt文件又出現了。若是咱們在develop分支下修改了master分支的code.txt,當咱們轉換爲master分支上時,不會出現develop分支下的修改。
ckjbug@ckjbug MINGW64 ~/Desktop/gituse $ git init Initialized empty Git repository in C:/Users/Enz/Desktop/gituse/.git/ ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git status On branch master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) code.txt nothing added to commit but untracked files present (use "git add" to track) ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git add . ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git status On branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: code.txt ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git commit -m 'First commit' [master (root-commit) 2571887] First commit 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 code.txt ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git status On branch master nothing to commit, working tree clean ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git branch * master ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git branch develop ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git checkout develop //以後添加一個develop.txt文件 Switched to branch 'develop' ckjbug@ckjbug MINGW64 ~/Desktop/gituse (develop) $ git branch * develop master ckjbug@ckjbug MINGW64 ~/Desktop/gituse (develop) $ git status On branch develop Untracked files: (use "git add <file>..." to include in what will be committed) develop.txt nothing added to commit but untracked files present (use "git add" to track) ckjbug@ckjbug MINGW64 ~/Desktop/gituse (develop) $ git add develop.txt ckjbug@ckjbug MINGW64 ~/Desktop/gituse (develop) $ git commit -m 'add a develop_txt on branch develop' [develop 2fd0d60] add a develop_txt on branch develop 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 develop.txt ckjbug@ckjbug MINGW64 ~/Desktop/gituse (develop) $ git branch * develop master ckjbug@ckjbug MINGW64 ~/Desktop/gituse (develop) $ git checkout master Switched to branch 'master' ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git checkout develop //在develop分支下修改master分支下的code.txt文件 Switched to branch 'develop' ckjbug@ckjbug MINGW64 ~/Desktop/gituse (develop) $ git status On branch develop Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: code.txt no changes added to commit (use "git add" and/or "git commit -a") ckjbug@ckjbug MINGW64 ~/Desktop/gituse (develop) $ git add code.txt ckjbug@ckjbug MINGW64 ~/Desktop/gituse (develop) $ git commit -m '在develop分支下修改了master分支的code.txt文件' [develop 903c8e0] 在develop分支下修改了master分支的code.txt文件 1 file changed, 1 insertion(+) ckjbug@ckjbug MINGW64 ~/Desktop/gituse (develop) $ git status On branch develop nothing to commit, working tree clean ckjbug@ckjbug MINGW64 ~/Desktop/gituse (develop) $ git checkout master //此時code.txt修改的代碼不在了 Switched to branch 'master'
6,融合分支,在master分支上時,將develop分支merge到master分支上。這時develop.txt文件出現了。即融合到了master分支中。
ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git merge develop Updating 2571887..903c8e0 Fast-forward code.txt | 1 + develop.txt | 0 2 files changed, 1 insertion(+) create mode 100644 develop.txt
7,Merge時衝突的處理,加入兩個不一樣分支同時修改了主分支下code.txt中的第三行代碼,兩個分支在融合時會衝突。
8,遠程推送push、拉取pull項目代碼,在實際項目中,每每已經存在了遠程的項目,當你參與到項目的開發當中時,
http://www.javashuo.com/article/p-mgrqqypj-ev.html
相關連接: