Google Code Prettify

顯示具有 git 標籤的文章。 顯示所有文章
顯示具有 git 標籤的文章。 顯示所有文章

git 如何將本地數據庫推到遠端數據庫

如果你要觀察本地數據庫跟遠端數據庫的關聯,就得仰賴 git remote 系列指令


加入遠端數據庫:git remote add <遠端數據庫簡稱> <url>

觀看遠端數據庫列表:git remote

觀看遠端數據庫列表(包含 rul):git remote -v

下載遠端數據庫:git clone <url>

如果你本地有數據庫,想將它推到 GitHub


先在本地端建立數據庫

建立 2~3 個 commit

在 GitHub 新增一個遠端數據庫

並使用此指令,將本地數據庫加入遠端數據庫 git remote add <遠端數據庫簡稱> <url>

這裡筆者開了一個新的數據庫,你可以看到在裡面的 …or push an existing repository from the command line 的指令就符合我們的情境。


git remote add origin https://github.com/gonsakon/emptyGitRepo.git

git push -u origin master


第一個指令是在本地數據庫上,加入一個遠端數據庫,所以你可以先下此指令後。再用 git remote 查看,此時你就會看到多了一個 origin 的遠端數據庫簡稱。


origin 就是這個遠端數據庫的簡稱,你也可以取自己想要的,例如 GitHub。


在推(push)上去時,系統會提示你輸入帳號密碼,以確保你是帳號的主人。


master 是什麼?

當你一開始建立 commit 時,其實是有一條線串起那好幾個 coomit,這條線叫做分支(branch),而預設分支名稱就叫做 master。這一段我們在後面談論分支時,會再進行詳細講解。


總結

如果你本地有一個數據庫,想加入遠端數據庫時,請用 git remote add <遠端數據庫簡稱> <url>

你想將本地資料推送到遠端數據庫時:git push <數據庫簡稱> <分支名稱>



Git 常用指令大全

用戶配置

//顯示用戶名
git config user.name
 
//顯示用戶信箱位址
git config user.email
 
//設定本地用戶名
git config user.name "your_name"
 
//設定本地用戶信箱位址
git config user.email "your_name@example.com"
 
//設定全域用戶名
git config --global user.name "your_name"
 
//設定全域用戶信箱位址
git config --global user.email "your_name@example.com"


初始化與複製專案

//初始化儲存庫
git init
 
//在當前目錄下複製現有儲存庫
git clone <url>


新增與移除

//新增檔案至暫存區域
git add <file>
 
//新增所有副檔名為 extension 的檔案至暫存區域
git add *.extension
 
//新增所有更改過的檔案至暫存區域
git add .
 
//將檔案從暫存區域中移除
git reset <file>
 
//將所有副檔名為 extension 的檔案從暫存區域中移除
git reset *.extension
 
//將所有檔案從暫存區域中移除
git reset .
 
//將所有已追蹤檔案還原
git reset --hard
 
//將所有已追蹤檔案還原
git reset --hard
 
//將未追蹤檔案捨棄
git clean -f -d
 
//提交並附上提交訊息
git commit -m "commit message"
 
//修改提交訊息
git commit --amend
 
//取消追蹤並刪除檔案
git rm <file>
 
//取消追蹤檔案
git rm --cached <file>
 
//取得提交內容並提交到當前分支
git cherry-pick <sha>
 
//取得合併內容並提交到當前分支
git cherry-pick -m 1 <sha>

//還原提交內容
git revert <sha>


檢查與比較

//查看當前檔案狀態
git status

//比對工作目錄及暫存區域的差異
git diff
 
//顯示提交歷史紀錄
git log
 
//顯示 n 個提交歷史紀錄
git log -n
 
//顯示提交內容
git show <sha>


遠端和更新

//顯示所有遠端儲存庫
git remote
 
//顯示所有遠端儲存庫及 URL
git remote -v
 
//新增遠端儲存庫並命名為 short_name
git remote add <short_name> <url>
 
//更名遠端儲存庫
git remote rename <short_name> <new_name>
 
//移除遠端儲存庫
git remote rm <short_name>
 
//擷取所有遠端儲存庫
git fetch
 
//擷取遠端儲存庫的 branch_name 分支
git fetch <branch_name>
 
//擷取並合併遠端儲存庫
git pull 
 
//將當前分支上傳到遠端儲存庫
git push <remote_name> <branch_name>


分支與合併

//顯示本地所有分支
git branch
 
//顯示儲存庫所有分支
git branch -a
 
//新增分支
git branch <branch_name>
 
//刪除分支
git branch -d <branch_name>
 
//強制刪除分支
git branch -D <branch_name>
 
//更名分支
git branch -m <old_branch_name> <new_branch_name>
 
//切換分支
git checkout <branch_name>
 
//新增並切換至分支
git checkout -b <branch_name>
 
//將 branch_name 合併到當前分支
git merge <branch_name>
 
//以 branch_name 為基底衍合當前分支
git rebase <branch_name>
 
//新增暫存
git stash
 
//顯示查看所有暫存
git stash list
 
//提取暫存
git stash apply
 
//提取特定暫存
git stash apply <stash_name>
 
//刪除特定暫存
git stash drop <stash_name>


子模組

//新增子模組
git submodule add <url>
 
//初始化子模組
git submodule init
 
//更新子模組
git submodule update