git使用筆記

chopin發表於2017-12-10

git基礎知識

工作區(Working Directory)
暫存區(stage)
版本庫(Repository)
中央伺服器
git add(stage) -> 暫存區 -> git commit -> 本地版本庫 -> git push 伺服器
Git的版本庫裡存了很多東西,其中最重要的就是稱為stage(或者叫index)的暫存區,還有Git為我們自動建立的第一個分支master,
以及指向master的一個指標叫HEAD

.gitignore檔案

清除快取,使.gitignore生效
git rm -r --cached .

git上傳本地專案(檔案)到版本庫

git init
git add . 或者 git add name.file
git commit -m "first commit" 或者 git commit -m "first commit" name.file
git remote add origin https://gitee.com/*.git
git push -u origin master 或者 git push (只會提交暫存區的檔案)

修改程式碼並上傳

新增檔案的時候,我們用git add命令,修改程式碼也是用這個命令git add,是不是覺得很奇怪。

git push免輸使用者名稱和密碼

windows

在windows下第一次輸入使用者名稱和密碼後,會自動記錄使用者名稱和密碼,下次提交的時候不需要重複輸入。

linux/mac os

在使用者~目錄下

$ cd
$ vi .git-credentials
https://username:password@github.com 
如果你用的是gitee的話
https://username:password@gitee.com
$ git config --global credential.helper store
$ cat .gitconfig
[credential]
    helper = store

git fetch和get pull命令

git fetch和get pull都是將伺服器程式碼更新到本地

git fetch origin master
git log -p master..origin/master
git merge origin/master

另外一種方式

git fetch origin master:tmp
git diff tmp 
git merge tmp

git pull:從遠端獲取最新版本併合併到本地,相當於git fetch和get merge命令。

git命令列表

git reset
git status

版本(檔案)撤銷提交

版本切換

首先,在Git中,用HEAD表示當前版本,上一個版本就是HEAD^,上上一個版本就是HEAD^^,往上100個版本寫成HEAD~100
Git允許我們在歷史版本中切換,使用命令git reset可以回退到任意一個歷史版本中。
git reset有三個選項,–hard、–mixed、–soft

//僅僅只是撤銷已提交的版本庫,不會修改暫存區和工作區
git reset –soft 版本庫ID
//僅僅只是撤銷已提交的版本庫和暫存區,不會修改工作區
git reset –mixed 版本庫ID
//徹底將工作區、暫存區和版本庫記錄恢復到指定的版本庫
git reset –hard 版本庫ID

$ git log
commit 611dd5e339d3748e8b54620a203988***
Author: xxx <xxx@163.com>
Date:   Sat Nov 11 18:08:28 2017 +0800

    first commit
$ git reset --hard commit_id //將所有的檔案都恢復到id版本
//恢復到上一個版本
$ git reset --hard HEAD^ 
// 恢復到前兩個版本
$ git reset --hard HEAD~2

用git log可以檢視提交歷史,以便確定要回退到哪個版本。
用git reflog檢視命令歷史,以便確定要回到未來的哪個版本.

檔案切換

從工作區撤銷,其實就是刪除後重新檢出

rm -f hello.c
git checkout [commit_id] hello.c

從暫存區撤銷
git reset HEAD 檔名

技巧

刪除版本庫檔案,不刪除本地檔案

首先把檔案新增到.gitignore裡面忽略掉,然後提交使.gitignore生效。
執行命令,刪除版本庫中檔案但不刪除本地檔案

git rm --cached .DS_Store //--cached不刪除本地,只刪除暫存區的檔案
git commit -m "remove ds_store" //提交
git push //將修改提交帶版本庫,然後檢視版本庫就看到檔案刪除了
git rm -r --cached xxx.iml  //-r 如果存在子檔案則遞迴刪除
(git add xxx.iml)      //若.gitignore檔案中已經忽略了xxx.iml則可以不用執行此句
git commit -m "delete file" 
git push

用版本庫檔案-覆蓋本地檔案

git checkout -- test.c
git checkout -- `*.c`
git checkout branch-name //切換分支

git重新命名資料夾

git mv -f old_filename new_filename
git add -u new_filename
git commit -m "rename"
git push

git add的幾個選項

git add -u:將檔案的修改、刪除,新增到暫存區。
git add .:將檔案的新建、修改,新增到暫存區。
git add -A:將檔案的新建、修改、刪除,新增到暫存區。

問題

LF,CRLF問題

warning: LF will be replaced by CRLF in projects.iml.
The file will have its original line endings in your working directory.

在linux和mac os中換行符表示為LF,但是在windows中衛CRLF。
git config --global core.autocrlf true

git config –global core.autocrlf true
Configure Git on Windows to properly handle line endings
解釋:core.autocrlf是git中負責處理line endings的變數,可以設定三個值–true,inout,false.
設定成三個值會有什麼效果呢?
【1】If core.autocrlf is set to true, that means that any time you add a file to the git repo that git thinks is a text file, it will turn all CRLF line endings to just LF before it stores it in the commit.。
設定為true,新增檔案到git倉庫時,git將其視為文字檔案。他將把crlf變成lf。
【2】If core.autocrlf is set to false, no line-ending conversion is ever performed, so text files are checked in as-is. This usually works ok。
設定為false時,line-endings將不做轉換操作。文字檔案保持原來的樣子

git diff

git diff是對比本地倉庫(local repository)和遠端倉庫(remote repository)之間的差異,不包括未提交的和stage內的。

//修改任意本地檔案,然後執行下面命令
$ git diff master origin/master //控制檯無任何資訊
$ git add .
$ git diff master origin/master
$ git commit -m "update"
$ git diff master origin/master
$ diff --git a/excel-upload/src/test/java/org/xiaog/config/UploadConfigTest.java b/excel-upload/src/test/java/org/xiaog/config/UploadConfigTest.java
index a6067c5..7ba72d7 100644
--- a/excel-upload/src/test/java/org/xiaog/config/UploadConfigTest.java
+++ b/excel-upload/src/test/java/org/xiaog/config/UploadConfigTest.java
....

伺服器建立git資源庫

git init --bare
建立一個裸倉庫,只管理版本資訊,不會有work tree,也就是不會有程式碼檔案,也不會有.git資料夾,這個裸倉庫只有.git資料夾裡面的內容。

名詞

  1. FETCH_HEAD
    是一個版本連結,記錄在本地的一個檔案中,指向著目前已經從遠端倉庫取下來的分支的末端版本
  2. origin和matser
    通過命令檢視origin git remote -v 可以看到origin的資訊。分別是push和fetch的git地址,且指向的是同一個git伺服器地址。
    所以,origin是遠端倉庫的名字,而且是自動命名的。
    origin/master 代表是遠端倉庫origin的分支master.
    git pushgit push origin master:master 預設情況下,這兩個命令其實是一樣的,都是將本地倉庫的程式碼提交到遠端倉庫的master分支。第一個master是本地後面的值遠端倉庫master

相關文章