1. 程式人生 > >Git 操作命令

Git 操作命令

系統 工作流 用戶 遠程 span style value str hist

一、Git 基本配置

1、配置

命令:git config --global prop_name prop_value

如配置git用戶名與郵箱:

git config --global user.name matt cheng  
git config --global user.email [email protected]

2、查看配置

查看所有配置命令:git config --list --global

查看單項配置命令:git config --global user.name

3、配置的級別

git config --local    只對某個倉庫有效(此為缺省配置)
git config 
--global 對當前用戶所有倉庫有效 git config --system 對系統所有登錄用戶有效

優先級:local > global > system

4、其他命令

git --version    查看版本
git command -h    查看命令幫助
git command --help 查看命令幫助(web頁面)

二、Git 工作流

技術分享圖片

  說明:

     Workspace:     工作區
     Index/Stage:   暫存區
     Repository:    倉庫區(或本地倉庫)
     Remote:        遠程倉庫

1、遠程倉庫與本地倉庫的互操作

git clone url    從遠程倉庫clone到當前目錄,倉庫名與遠程倉庫相同
git clone url dir_name 從遠程倉庫clone到當前目錄,倉庫名為dir_name 

git fetch remote_name    將遠程分支代碼同步到本地遠程分支
git fetch remote_name remote_branch_name    將遠程指定分支代碼同步到本地遠程分支

git push remote_name local_branch:remote_branch    將本地分支同步到遠程分支,可不同名
git push remote_name local_branch    將本地分支同步到遠程同名分支,如遠端不存在同名分支,則新建一個
git push    將本地當前分支同步到關聯遠端同名分支,如關聯分支不同名,則報錯
git push remote_name 
--all 同步所有分支到遠端 git merge remote_name/remote_branch_name 將本地遠程分支代碼合並到本地當前分支,如出現錯誤:fatal: refusing to merge unrelated histories,使用--allow-unrelated-histories,如git merge --allow-unrelated-histories github/master,詳細加幫助說明 git pull remote_name remote_branch:local_branch 將遠程倉庫的指定分支的代碼同步到本地倉庫指定分支,若本地分支不存在則新建一個 git pull remote_name remote_branch 將遠程倉庫的指定分支的代碼同步到本地倉庫當前分支(HEAD) git pull 將關聯的遠程倉庫指定分支的代碼同步到本地倉庫當前分支,無關聯分支則報提示信息 pull = fetch + merge git branch -vv 查看分支關聯信息 git branch --set-upstream-to=origin/remote_branch local_branch 設置分支關聯信息 git remote -v 查看關聯的遠端(遠端包括備份、github、gitlab等) git remote add remote_name remote_url 添加關聯的遠端,remote_name:遠端名,可自定義;remote_url:遠端地址,如https、ssh等

補充說明

  • 從遠端clone出來的項目,remote_name默認為orgin
  • push操作需要滿足fast-forwards,詳見:git push --help -> help page -> NOTE ABOUT FAST-FORWARDS

2、本地倉庫

可以將本地目錄納入git管理

git init    將已有項目納入git管理:
git init project_name    創建新項目,會在當前路徑下新建同名目錄

3、本地備份

操作流程如下:

Git 操作命令