git全局配置及文件改變狀態詳解

本文git爲1.9.6版本:git


1、git全局配置bash

2、git初始化本地倉庫服務器

3、git文件狀態詳解yii

4、git文件撤銷、恢復操做
ide


git提交流程層次以下:測試

wKioL1QZmZLSIoqTAADqrDRk-Xk515.jpg

git repository 視爲線上code集中管理服務器;也就是咱們的code最後的位置;ui

git staging area 視爲咱們本地的code集中管理服務器,可認爲code的中間的暫留區(以相對repository來講);
spa

git working directory 視爲咱們本地編輯的code,也就是代碼編輯處;3d

1:全局配置版本控制

leo@LEO-PC /d/User/leo/Desktop/git (master)
$ git config --global user.name "lansgg"

leo@LEO-PC /d/User/leo/Desktop/git (master)
$ git config --global user.email "coffee_lanshan@sina.com"


這裏配置 --global選項其實就是在修改家目錄下的.getconfig文件

如個人:

C:\Users\leo\.getconfig

內容:

[user]
    name = lansgg
    email = coffee_lanshan@sina.com
[color]
    ui = true
[core]
    autocrlf = true
    excludesfile = C:\\Users\\leo\\Documents\\gitignore_global.txt

當咱們修改此文件,好比將name=test (內容已經修改)

$ git config --get user.name

wKioL1QZnOnACof9AACZsKXdmaQ003.jpg

這裏的用戶名及郵件地址都是在提交代碼的時候進行標識的,顯示提交人的信息;

2:初始化本地git倉庫

本地新建一個目錄;執行git init ;

$ mkdir git
$ git init

執行後的變化就是多了一個.git目錄;

leo@LEO-PC /d/User/leo/Desktop/git (master)
$ ls -a .git
.   COMMIT_EDITMSG  config       hooks  info  objects
..  HEAD            description  index  logs  refs

leo@LEO-PC /d/User/leo/Desktop/git (master)
$

.git 目錄,其中存放的是咱們所提交的文檔索引內容,Git 可基於文檔索引內容對其所管理的文檔進行內容追蹤,從而實現文檔的版本控制。.git目錄位於工做目錄內。

index(索引):將工做目錄下全部文件(包含子目錄)生成快照,存放到一個臨時的存儲區域,Git 稱該區域爲索引。

三、git文件狀態詳解

3.一、如今新建咱們的code;

leo@LEO-PC /d/User/leo/Desktop/git (master)
$ touch README.txt

leo@LEO-PC /d/User/leo/Desktop/git (master)
$ cat hello.rb
puts "hello world!"

咱們要將he.rb 、README 提交到本地git repository;

3.二、首先查看文件的狀態

$ git status

wKioL1QZorOR6ByQAAEIxOsfZpE246.jpg

$ git status -s

wKioL1Qaw9fgQYswAACCrMV_pNQ840.jpg

根據輸出信息能夠看出,README.txt hello.rb 兩個文件處於master branch(主分支),這兩個文件處於未追蹤文件,也就當咱們執行git commit(用於提交到圖中的倉庫)命令的時候不會將他們提交到git repository;

上圖 -s 表示簡要信息;( ?? )兩個問號也有很重要的意義;

第一個 ? 主要表示staging area 和 repository 兩個區域間的文件變化,通常會有兩個字母來表示(A、M <綠色>);A  表示此文件已是在追蹤文件,M 表示此文件已經在staging area區域修改,尚未提交到repository。

第二個 ? 主要表示working directory 和 staging area 兩個區域間的文件變化,M <紅色> 表示此文件已經working directory區域修改,尚未提交到staging area


下面開始演示:git add 表示將文件進行追蹤;

$ git add README.txt
$ git add hello.rb

再次查看文件狀態

$ git status -s

wKiom1QayN2j1SdNAAEsVHZICPU028.jpg

能夠看到兩個文件已經處於 A 狀態 (追蹤)如今這兩個文件處於staging area區域; changes to be committed 能夠進行提交了;輸出信息提示,能夠使用git reset HEAD <file> 將文件恢復於未追蹤狀態;

wKioL1Qaye2i-LqkAACvhbbQbng968.jpg

恢復到已經追蹤的狀態,進行提交測試;

$ git commit -m "first commit"     #-m 表示提交此代碼時進行描述;咱們這裏描述爲「first commit」

wKioL1QaypjSCg2ZAAHNRhyk3D0616.jpg

能夠看到wording directory clean 已經將此兩個文件提交到repository;(從staging area區域),並查看文件狀態時,不在輸出任何東西

3.2 修改本地code,再查看文件狀態

$ echo 'puts "hello world!" '>> hello.rb
$ git status -s


wKioL1Qay8aDvNfpAAGFhOaNxr8545.jpg

第二個 ? 的地方出現了M <紅色> 表示此文件已經在working directory區域修改;輸出信息提示,hello.rb文件已經modified ; 命令  git add && git checkout && git commit -a  (圖示都有) 下面講;

如何將此文件提交到repository

$ git add hello.rb
$ git commit -m "second commit" hello.rb

wKiom1Qa0_fAt58sAAJaCVn8ZMo997.jpg

或者使用:

$ git commit -a -m "second commit"

此命令將跳過git add 這一步~

四、撤銷、恢復

4.一、修改本地working directory 的code ;而後撤銷修改,也就說從staging area區域取出此code覆蓋當前working directory的code;

測試以下:

$ echo 'puts "hello world!"' >> hello.rb     #修改本地code
$ git status -s                              #查看文件的狀態,有差別
$ git checkout hello.rb                      #從staging area區域取出此code
$ git status -s                              #再次查看該文件的狀態,無差別,而且代碼恢復到了以前的代碼

wKiom1Qa1diDPH6LAAEaK8irxy4604.jpg

4.二、修改本地working directory的code,而且進行追蹤(add 到 staging area區域);而後咱們想撤銷本地code的修改,那咱們能夠從repository倉庫拉出此code覆蓋到staging area,而後再從staging area區域取出覆蓋到working directory區域;測試以下:

$ echo 'puts "hello world!"' >> hello.rb
$ git status -s
$ git add hello.rb
$ git status -s
$ git reset hello.rb
$ git status -s
$ git checkout hello.rb
$ git status -s                     # 每一步都進行了文件狀態差別的核對;

wKiom1Qa1z7R340dAAMUN3vNTWM915.jpg

咱們也能夠不用這麼麻煩,能夠直接從 repository 區域拉取出來直接覆蓋到 working directory 區域:

$ echo 'puts "hello world!"' >> hello.rb
$ git status -s
$ git add hello.rb
$ git status -s
$ git checkout HEAD hello.rb
$ git status -s

wKiom1Qa5W7yiiGfAAK9_1gP39o830.jpg

能夠看到已經從git repository 拉取此文件並進行了覆蓋~

相關文章
相關標籤/搜索