git 常用指令

2022-05-19 00:48:13 字數 2916 閱讀 4023

檢視當前**狀態:git status

顯示內容:

changes to be commited 表示將要提交但是尚未提交的修改(已經add但是尚未commit),也就是在stage區域已經有了,但是還沒有commit的內容

changed but not updated 表示已經修改但是還沒有新增到暫存區的內容(尚未add,當然尚未commit)

檢視檔案改動 git diff

git diff 比較working directory和stage的差別

git diff --cached 比較stage和history的差別

git diff head 直接比較working directory 和history的區別

檔案重新命名: git mv

git mv helloworld.txt helloworld2.txt

//將helloworld.txt重新命名為helloworld2.txt

建立新分支:git branch test

//建立名為test的分支

切換分支:git checkout test

//切換到test分支工作, 與上面的命令經常一起使用

檢視分支:git branch

//檢視前版本庫(本地)的所有分支

合併分支:git checkout master

//切換到「要合併到」的分支,常見的就是test分支合併到master分支,所以先切換到master分支

git merge test

//合併test分支到master分支

注意: 在合併分支的時候有時候會出現衝突(conflict)的情況,建立的情況是master分支和test分支對同一檔案的同一處**的內容不一樣(例如:master分支在hello.cpp中第三行寫入的是"hello", 而test分支在hello.cpp的第三行中寫入的是「world」),這樣使得git不知道如何是好,這時候就需要你手工修改**了,我難道要記住master和test在同一位置的內容?當然不用了,git會在git merge的反饋資訊中說明,**在何部分存在衝突。

刪除分支: git branch -d test //

刪除test分支

分支重新命名:git branch -m test test2 //

將test分支重新命名為test2,這個命令不常用

查詢git歷史記錄

查詢所有的commit歷史:git log

查詢指定範圍的commit歷史: git log --since = "5 hours" //

檢視最近5小時的commit歷史記錄

git log --before = "5 hours" -2 //

檢視5小時之前最後2次的提交commit記錄

git log 18f822e..0bb3dfb

//檢視從18f822e(不包括18f822e)到0bb3dfb之間的提交記錄

git log 18f822e..head

//head表示當前所在分支的最新版本,即head指向當前所在分支的最後一次commit

git log head^^^ //

^表示父節點,head^就表示head的父節點,以此類推

git log head~3 //

~n 表示回溯n個節點,所以與上乙個表述等價

檢視版本之間的差異: git diff

//檢視working dir和stage(index)之間差異

git diff --cached //

檢視stage(index)和history(當前分支版本庫)之間的差異

git diff head

//檢視working dir 和 history之間的差異

問責檔案內容: git blame hello.cpp

//增補提交: git commic -c head -a --amend //

版本庫同步: git fetch

//取來(fetch)遠端版本庫到本地,但是並不與本地分支合併

git pull

//拖入(pull)遠端版本庫,與本地分支合併,相當於 git fetch + git merge

推送本地**到遠端版本庫: git remote add origin https:

//為遠端版本庫reponame取別名origin

git push origin master

//將本地master分支推入github上的origin

其他://

翻轉提交: git revert

復位: git reset

分支變基: git rebase

標籤: git tag

git子模組: git submodule

匯出版本庫: git archive

二分查詢: git bisect

Git 常用指令

首先需要建立乙個repo,這是cd到資料夾底下,然後init。git init 在檔案系統裡面的檔案如果不新增到git的repo裡面,不會自動被git辨識,需要手動新增,這也是初始化repo是必須要做的事。git add documentation txt git add git sh 刪除檔案,分...

git常用指令

git 常用命令 git init here 建立本地倉庫 repository 將會在資料夾下建立乙個 git 資料夾,git 資料夾裡儲存了所有的版本資訊 標記等內容 git remote add origin git github.com winter1991 helloworld.git 把...

git常用指令

git config global user.name robbin git config global user.email fankai gmail.com git config global color.ui true git config global alias.co checkout g...