github常用操作

2022-09-01 17:48:13 字數 4659 閱讀 4391

$cd ~/hello-world        //到hello-world目錄,本地目錄名與repository的名字不一定相同

$git init                    //初始化

$git add .                  //把所有檔案加入到索引(不想把所有檔案加入,可以用gitignore或add 具體檔案,見下文)

$git commit              //提交到本地倉庫,然後會填寫更新日誌( -m 「更新日誌」也可,如$git commit -m 「my first vesion of ...」)

$git remote add origin [email protected]:wadeleng/hello-world.git        //增加到remote

$git push origin master    //push到github上

$cd ~/hello-world

$git add .                  //這樣可以自動判斷新加了哪些檔案,或者手動加入檔案名字

$git commit              //提交到本地倉庫,不加引數會提示,注意:^=ctrl,按照提示來就好了~~~

$git push origin master    //不是新建立的,不用再add 到remote上了

3.更新專案(沒新加檔案,只有刪除或者修改檔案):

$cd ~/hello-world

$git commit -a          //記錄刪除或修改了哪些檔案

$git push origin master  //提交到github

$cd ~/hello-world

$vim .gitignore    //把檔案型別加入到.gitignore中,儲存

然後就可以git add . 能自動過濾這種檔案

$git clone [email protected]:wadeleng/hello-world.git

假如本地已經存在了**,而倉庫裡有更新,把更改的合併到本地的專案:

$git fetch origin    //獲取遠端更新

$git merge origin/master //把更新的內容合併到本地分支

$git reset

$git rm  * // 不是用rm

//------------------------------常見錯誤-----------------------------------

1.$ git remote add origin [email protected]:wadeleng/hello-world.git

解決辦法:$ git remote rm origin

然後在執行:$ git remote add origin [email protected]:wadeleng/hello-world.git 就不會報錯誤了

2. $ git push origin master

解決辦法:$ git pull origin master //先把遠端伺服器github上面的檔案拉先來,再push 上去。

1 git簡介

git是用c語言開發的分布版本控制系統。版本控制系統可以保留乙個檔案集合的歷史記錄,並能回滾到另外乙個狀態(歷史記錄狀態)。對 於任何乙個檔案,在 git 內都只有三種狀態:已提交(committed),已修改 (modified)和已暫存(staged)。已提交表示該檔案已經被安全地儲存在本地資料庫中了;已修改表示修改了某個檔案,但還沒有提交儲存;已暫 存表示把已修改的檔案放在下次提交時要儲存的清單中。

2 git命令基本格式

一般情況下,git的命令都是git [command] [option] [argument]格式。其中 command指的是某種操作命令,比如config,mv,rm,pull,push等等。而option和 argument指的是操作命令後面的具體引數,比如:

git config --golobal user.name="hic" 其中,config指的就是配置命令,接著後面跟上配置的具體引數,其中–global指的是對全域性配置生效,後面的user.name就是對使用者名稱進行設定。

3 配置檔案

配置檔案是從事所有所有工作的基礎,在這裡配置檔案決定了工作時候的使用者,郵箱,以及預設編輯器等等常用的配置引數,下面就先說一說配置檔案。

在git中,配置檔案有三大類,分別放在不同的位置。

/etc/gitconfig

系統中所有使用者都普遍適用的配置。git config –system就是用來配置這個檔案的。

~/.gitconfig

使用者目錄下的配置檔案,該檔案只適用於該使用者。git config –global就是用來配置這個檔案的。

~/*/.git/config

這是當前專案的配置檔案,僅對該專案適用。

注意:每一級別的配置,都會覆蓋上層的相同配置。

3.1 配置資訊

使用者名稱設定

git config --system user.name "hic"

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

電子郵件

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

設定文字編輯器,預設為vi,這裡設定為emacs

git config --global core.editor emacs

檢視配置資訊

git config --list

如果要檢視某個特定的環境變數,只要把它的名字放到最後即可

git config user.name

4 選擇工作目錄

選擇乙個工作目錄,開始工作,同時我們新增一些檔案和目錄。

~$ mkdir test

~$ cd test/

~/test$ ls

~/test$ echo "hello world! " > readme

~/test$ echo "this is a test! " > file

5 初始化

~/test$ git init initialized empty git repository in /home/hic/test/.git/

~/test$ la file .git readme

執行這條命令之後,在test目錄下會出現乙個隱藏的.git目錄,該目錄會儲存工程中所有的資訊。

9 分支

如果事先沒有作出任何的更改,乙個專案是沒有分支的,它只有一條主線,例如:

~/test$ git branch * master

這裡master就是主幹,就是整個開發的流程,它前面的星號表示當前開發的流程。

如果在開發過程中,我們突然需要新增某個功能,或者打上某個補丁,可以在 master主幹上新增乙個分支,比如host,例如:

~/test$ git checkout -b host

switched to a new branch 'host'

~/test$ git branch

* host master

這個時候,你可以清楚的看到星號已經在host頭上,表示當前已經切換到host上,你可以開發你的補丁,而不會打亂主幹的開發。

如果你在開發過程中想要回到主幹,可以使用checkout命令進行切換。

~/test$ git checkout master

switched to branch 'master'

此時,你又回到主幹了。

10 合併

當某個分支的開發結束後,你會需要將其合併到主幹上,從而集中精力進行主幹的開發。合併前只需要將指標切換到主幹,即master,然後使用merge命令。

~/test$ git checkout host switched to branch 'host'

~/test$ echo "lsdknl" >> file

~/test$ git add file

~/test$ git commit file -m "add somthing"

[host 0dacfd9] add somthing 1 file changed, 1 insertion(+)

~/test$ git checkout master switched to branch 'master'

~/test$ git merge host updating 81fde76..0dacfd9 fast-forward file | 1 + 1 file changed, 1 insertion(+)

此時的host分支已經失去作用了,可以將刪除。

~/test$ git branch -d host deleted branch host (was 0dacfd9).

11.1 檢視當前遠端庫

git remote

11.2 新增遠端庫

git remote add emacs git:

11.3 從遠端庫抓取資料

git fetch [remote-name]

11.4 推送資料到遠端倉庫

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

11.5 檢視遠端倉庫

git remote [remote-name]

11.6 遠端倉庫的刪除和重新命名

git remote rm [remote-name] git remotw rename form-name to-name

github常用操作總結

git環境配置 git命令 ssh配置 github上不去怎麼辦?git日常工作流 前提 github建立專案 進入專案目錄 進入git bash here 流程 1.初始化倉庫 git init 2.設定遠端倉庫 git remote add origin git github.com cat h...

github常用操作命令

先在github上建立並寫好相關名字,描述。cd hello world 到hello world目錄,本地目錄名與repository的名字不一定相同 git init 初始化 git add 把所有檔案加入到索引 不想把所有檔案加入,可以用gitignore或add 具體檔案,見下文 git c...

Git與GitHub常用操作

基本操作 clone 拷貝遠端倉庫 commit 本地提交 push 遠端提交 pull 更新本地 初始化git git init 初始化本地庫 自動建立.git隱藏目錄 不要刪除and修改 ll la 檢視當前目錄的所有檔案 包含隱藏檔案 及許可權 設定簽名 作用 區別不同開發人員的身份 與登入遠...