GitHub初次執行前配置與基本操作

2023-01-22 06:25:48 字數 1980 閱讀 2476

前段時間弄過github,一段時間沒用又忘了不少,這兩天又把相關知識整理了一下。有關安裝和配置git的內容,在另一篇文章裡

1 初次執行git前的配置

git配置檔案為

1) /etc/gitconfig 對系統所有使用者起作用的配置

2) 使用者的主目錄/.gitconfig 當前使用者的配置

3) 專案資料夾根目錄/.git/config 當前專案的配置

每個級別的配置,都會覆蓋上層相同配置

配置使用者資訊

git config --global user.name "realwall"

git config --global user.email "[email protected]"

這裡使用的了--global選項,修改的是配置檔案2),若使用--system選項,則修改配置檔案1),若不加選項,則修改當前專案的配置檔案3)。

配置文字編輯器

git config --global core.editor vim

配置差異分析工具

git config --global merge.tool vimdiff

檢視配置資訊

git config --list

2 git基本操作

取得專案的倉庫

1)在工作目錄中初始化新倉庫

git init

2)從現有倉庫克隆

git clone git: [重新命名]

這裡採用的是git協議,也可以是其他,如http(s)

將當前目錄下的檔案納入版本控制

git add *.c

git add readme

git commit -m 'initial project version'

檢查當前檔案狀態

檔案狀態:untracked(未跟蹤),unmodified(未修改),modified(已修改),staged(已暫存)

git status

暫存檔案

git add readme

注:git add是一個多功能命令,根據目標檔案的狀態不同,此命令的效果也不同:可以用它來跟蹤新檔案,或者把已跟蹤的檔案放入暫存區域,還能用於合併時把有衝突的檔案標記為已解決狀態。

忽略某些檔案

在專案任意目錄建立檔案.gitignore,以忽略某些檔案,使它們不會總是出現在未跟蹤檔案列中。

.gitignore檔案示例:

#此為註釋,將被git忽略

*.a #忽略左右字尾名為a的檔案

!lib.a #lib.a除外

檢視已暫存和未暫存的更新

git diff

提交更新

git commit

注:每次提交前,要先用git status命令檢視所有修改是否都已暫存

從跟蹤列表移除檔案

git rm readme

移動/重新命名檔案

git mv file_from file_to

檢視當前遠端庫

git remote [-v]

選項-v顯示遠端庫的地址

新增遠端倉庫

git remote add [shortname] [url]

從遠端倉庫抓取資料

git fetch [remote-name]

推送資料到遠端倉庫

git push [remote-name] [branch-name]

檢視某個遠端倉庫的詳細資訊

git remote show [remote-name]

重新命名遠端倉庫

git remote rename [old-remote-name] [new-remote-name]

刪除遠端倉庫

git remote rm [remote-name]