git使用《一》 常用本地操作

2021-08-11 05:54:21 字數 4081 閱讀 9700

編寫軟體,時常免不了修修改改,修改過後的**不一定比前面好,甚至產生新問題,或者有時無意間修改了某行**,導致出錯,這種情況都是很常見的,如果此時沒有版本管理,如果是小軟體可能沒什麼影響,如果**量很大,就是個很頭疼的問題,git的出現正是為了解決這個問題的,對於碼農來說,簡直是神器,下面簡單記錄下。

基本操作:

1.倉庫初始化:

直接進入資料夾,輸入git init

2.新增檔案:

乙個檔案(比如x檔案):git add x

多個檔案(比如x,y檔案):git add x y

整個資料夾檔案:git add *

3.提交檔案:

一行注釋:git commit -m "you comment"

多行注釋:git commit -m ",然後按下回車,輸入內容,然後繼續輸入內容,輸入完之後輸入"

4.檢視狀態:

git status

5.檢視日誌:

檢視版本日誌:git log

檢視所有操作日誌:git reflog

6.檢視異同:

僅僅檢視:git diff

製作patch:git diff > diff.patch

7.版本回滾與切換:

先執行git log確定要回滾到的版本,然後使用git reset --hard進行回滾;如果回滾後突然又想回到之前的版本,怎麼辦呢?這個時候只需要再次檢視日誌,不過需要使用git reflog,然後再次git reset --hard就可以恢復到之前的版本

8.配置資訊

git config --global  --list// 檢視配置資訊

git config --global user.name "myname"// 配置使用者名稱

git config --global user.email  "[email protected]" // 配置郵箱

操作例項:

ubuntu@ubuntu:tst$ git init        # 初始化

initialized empty git repository in /samba/tmp/tst/.git/

ubuntu@ubuntu:tst$ vi tst.txt

ubuntu@ubuntu:tst$ git status # 檢視狀態

on branch master

initial commit

untracked files:

(use "git add ..." to include in what will be committed)

tst.txt

nothing added to commit but untracked files present (use "git add" to track)

ubuntu@ubuntu:tst$ git add tst.txt # 新增檔案

ubuntu@ubuntu:tst$ git commit -m " # 提交檔案

> first commit

> "

[master (root-commit) 767ba26] first commit

file changed, 1 insertion(+)

create mode 100644 tst.txt

ubuntu@ubuntu:tst$ git log # 檢視日誌

commit 767ba26cc39de73ab2848680058a339341599fe8

author: your name date: thu aug 24 22:11:56 2017 +0800

first commit

ubuntu@ubuntu:tst$ vi tst.txt

ubuntu@ubuntu:tst$ git diff # 檢視修改

diff --git a/tst.txt b/tst.txt

index d00491f..48082f7 100644

--- a/tst.txt

+++ b/tst.txt

@@ -1 +1 @@

-1+12

ubuntu@ubuntu:tst$ git status # 再次檢視狀態

on branch master

changes not staged for commit:

(use "git add ..." to update what will be committed)

(use "git checkout -- ..." to discard changes in working directory)

modified: tst.txt

no changes added to commit (use "git add" and/or "git commit -a")

ubuntu@ubuntu:tst$ git add tst.txt # 再次新增修改後的檔案

ubuntu@ubuntu:tst$ git commit -m "second commit" # 提交修改

[master be932f8] second commit

file changed, 1 insertion(+), 1 deletion(-)

ubuntu@ubuntu:tst$ git log # 檢視日誌

commit be932f8fee8cfa266e0c2051905ff428d2f8bdb5

author: your name date: thu aug 24 22:16:21 2017 +0800

second commit

commit 767ba26cc39de73ab2848680058a339341599fe8

author: your name date: thu aug 24 22:11:56 2017 +0800

first commit

ubuntu@ubuntu:tst$ git reflog # 檢視所有操作日誌

be932f8 head@: commit: second commit

767ba26 head@: commit (initial): first commit

ubuntu@ubuntu:tst$ git reset --hard 767ba26 # 回滾到第一次提交

head is now at 767ba26 first commit

ubuntu@ubuntu:tst$ git log # 檢視日誌

commit 767ba26cc39de73ab2848680058a339341599fe8

author: your name date: thu aug 24 22:11:56 2017 +0800

first commit

ubuntu@ubuntu:tst$ git reflog # 檢視所有操作日誌

767ba26 head@: reset: moving to 767ba26

be932f8 head@: commit: second commit

767ba26 head@: commit (initial): first commit

ubuntu@ubuntu:tst$ git reset --hard be932f8 # 回滾到第二次提交

head is now at be932f8 second commit

ubuntu@ubuntu:tst$ git log

commit be932f8fee8cfa266e0c2051905ff428d2f8bdb5

author: your name date: thu aug 24 22:16:21 2017 +0800

second commit

commit 767ba26cc39de73ab2848680058a339341599fe8

author: your name date: thu aug 24 22:11:56 2017 +0800

first commit

ubuntu@ubuntu:tst$

git使用(常用操作)

安裝git 參考 建立倉庫 參考 配置git 參考 完成以上工作後,就可以開始使用git了 git的工作原理簡介 在開始之前需要掌握如下幾個概念 worspace 快取區 版本儲存空間 參考 分支,合併,衝突解決 參考 一些常用的命令 git status 檢視修改 git diff 檢視所有未新增...

Git本地操作

git菜鳥只能執行八步走戰略,沒有問題還好,一遇到問題就傻了 還是把八步走貼出來,方便跟我一樣的菜鳥 1 git status 2 git checkout 忽略修改的檔案 可省略 3 git status 4 git add all 5 git commit m 修改說明 6 git fetch ...

常用Git操作(一)

建立乙個資料夾用作本地git倉庫 repository mkdir repositorycd到該目錄,初始化該目錄,之後可用git管理 cd repository git init在該目錄可用 ls ah看到隱藏的資料夾.git 該資料夾用於管理倉庫 新增檔案到庫 在repository的本地目錄或...