git 學習筆記

2021-07-23 20:37:16 字數 4806 閱讀 5973

最近從 svn 轉到 git。這兩個版本控制系統區別蠻大的,還是有點不適用。做個筆記,記錄 git 的各種常用操作。算是給自己做個備忘。

首先要設定自己的使用者名稱和郵箱名。

git config --global user.name "your name"

git config --global user.email "your email address"

然後最好再設定一下提交 comment 時用的文字編輯器。(預設是 vi)

git

config--

global

core

.editor

"notepad++

.exe"

#我習慣用

notepad++

,

讓 git 輸出的文字顯示不同的顏色。

git config --global color.status auto  

git config --global color.diff auto

git config --global color.branch auto

git config --global color.interactive auto

檢視各種配置情況:

git

config--

list

這項操作其實和 git 無關,屬於是 ssh 的配置了。不過 git 會用到 ssh 來傳輸資料。所以還是需要配置一下。

ssh-keygen -t rsa -c 「您的郵箱位址」

cd ~/.ssh # 這時會生成這個目錄,公鑰就放在這個目錄裡。

ls # 這時會看到 這兩個檔案:id_rsa id_rsa.pub,id_rsa.pub 就是公鑰

生成乙個**倉庫有兩種方式,自己建乙個或者從遠端倉庫中轉殖乙個。

git init # 在當前目錄中生成乙個**倉庫

git clone url # 轉殖乙個**倉庫到本地

git clone url path # 轉殖乙個**倉庫到本地,放在 path 目錄中。

git add path # 新增檔案或目錄到**倉庫

git rm path # 從**倉庫和工作目錄中刪除檔案或目錄

git rm --cached path # 從**倉庫刪除檔案或目錄,本地還保留

git mv path destination # 檔案改名

git checkout [rev] file

# 簽出某乙個版本的檔案。如果不加 rev 則簽出最新的。

git status # 顯示當前工作目錄的狀態

git add path # 將檔案暫存

git commit # 將暫存的檔案提交到版本庫中。

git commit -a

# 將所有修改後的檔案都提交到版本庫中。

git commit --amend # 撤銷最近一次提交操作。

git reset head # 撤銷對 file 的暫存。

git checkout -- # 將 file 恢復到版本庫中的樣子。(丟棄對 file 的修改)

git checkout -- # 將所有檔案恢復到版本庫中的樣子。

git clean # 將工作目錄中非版本控制的檔案刪除

git diff # 當前檔案和暫存區域快照之間的差異。

git diff --cached # 暫存起來的檔案和上次提交時的快照之間的差異。

git diff --staged # 暫存起來的檔案和上次提交時的快照之間的差異,與 git diff --cached 相同。

有些檔案無需納入 git 的管理,也不希望它們總出現在未跟蹤檔案列表。我們可以建立乙個名為 .gitignore 的檔案,列出要忽略的檔案模式。下面是乙個 .gitignore 的例子。

# 忽略所有 .a 結尾的檔案

*.a# 但 lib.a 除外

!lib.a

# 僅僅忽略專案根目錄下的 todo 檔案,不包括 subdir/todo

/todo

# 忽略 build/ 目錄下的所有檔案

build/

# 會忽略 doc/notes.txt 但不包括 doc/server/arch.txt

doc/*.txt

git branch branch_name # 建立乙個名為 branch_name 的分支

git checkout branch_name # 將工作目錄切換到 branch_name 分支的最新版

git checkout -b branch_name # 新建乙個名為 branch_name 的分支,並將工作目錄切換到 branch_name 分支(head 指向 branch)

git branch -d branch_name # 刪除分支 branch_name

git branch -d branch_name # 強制刪除分支 branch_name

git branch # 顯示所有本地的分支

git branch -v # 檢視各分支最近一次提交物件的資訊

git branch --merge # 檢視那些分支是當前分支的直接上游

git branch --no-merged # 檢視那些分支尚未尚未合併到當前分支

git branch -f branch rev # 用 rev 覆蓋當前分支

git merge branch_name # 合併分支,將 branch_name 和當前分支合併

git clone url # 轉殖乙個**倉庫到本地

git clone url path # 轉殖乙個**倉庫到本地,放在 path 目錄中。

git remote -v # 顯示遠端倉庫的資訊

git remote add [shortname] [url] # 新增乙個新的遠端倉庫,並給遠端倉庫指定乙個 shortname

git fetch [remote-name]# 從遠端倉庫抓取資料到本地.

git pull [remote-name] # 從遠端倉庫抓取資料到本地,然後將遠端分支自動合併到本地倉庫中當前分支。

git push repository_name local_branch_name: remote_branch_name # 將本地的分支 local_branch_name 推送到遠端倉庫,成為名為 remote_branch_name 的遠端分支

git push repository_name local_branch_name # 將本地的分支 local_branch_name 推送到遠端倉庫,建立乙個同名的分支

git merge repository_name/remote_branch_name # 將遠端分支 remote_branch_name 合併到本地的當前分支上。

git checkout -b local_branch_name repository_name/remote_branch_name # 建立乙個本地分支 local_branch_name,與遠端分支 remote_branch_name 關聯。

git checkout --track repository_name/remote_branch_name # 建立乙個同名的本地分支 remote_branch_name,與遠端分支 remote_branch_name 關聯。

git push repository_name :remote_branch_name # 刪除遠端分支 remote_branch_name

git remote show [remote-name] # 檢視遠端倉庫 remote-name 的詳細資訊

git remote rename oldname newname# 將遠端倉庫 oldname 改名為 newname

git remote rm name # 刪除與遠端倉庫 name 的關聯

git tag name [revision] # 給 revision 打乙個輕量級標籤。

git tag -a name [revision] -m "your comment"

# 給 revision 打乙個含附註的標籤。

git tag -s name [revision] -m "your comment"

# 給 revision 打乙個含附註的標籤,並用用 gpg 來簽署標籤。

git push origin [tagname] # 分享標籤到遠端倉庫

git push origin --tags # 推送所有本地新增的標籤到遠端倉庫

git 支援我們給命令起個別名。下面是一些例子。

git config --global

alias.co checkout

git config --global

alias.br branch

git config --global

alias.ci commit

git config --global

alias.st status

git config --global

alias.unstage 'reset head --'

git config --global

alias.last 'log -1 head'

git config --global

alias.visual '!gitk'

Git學習筆記

git stash git stash list 顯示git棧內的所有備份,可以利用這個列表來決定從那個地方恢復。git stash clear 清空git棧。此時使用gitg等圖形化工具會發現,原來stash的哪些節點都消失了。關於git stash的詳細解釋,適用場合,這裡做乙個說明 使用git...

git 學習筆記

1 git checkout master 切換分支 2 git checkout b xx 新建分支,同時切換到該分支 3 dev 4 git add a 將變動檔案,提交到index 5 git commit m 將 暫存區 檔案,加入到版本控制中。6 git checkout master 7...

Git 學習筆記

顯示版本庫.git所在的目錄 git rev parse git dir 顯示工作區的根目錄 git rev parse show toplevel 相對於工作區根目錄的相對目錄 git rev parse show prefix git config 命令各引數的區別 git config e 版...