歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> 使用Git進行源代碼管理

使用Git進行源代碼管理

日期:2017/2/28 13:47:52   编辑:Linux教程

Git是一款非常流行的分布式版本控制系統,使用Local Repository追蹤代碼的修改,通過Push和Pull操作,將代碼changes提交到Remote Repository,或從Remote Repository中獲取代碼的最新版本,實現團隊源代碼的共享和管理。VS2013 集成了git插件,能夠使用git進行源代碼管理,比如:Merge Branch,Code Review,Code Changes的Push和Pull等,這裡簡單介紹一下VS2013中git插件的使用。

一,創建Repository

在使用git之前,先創建Remote Repository,用於存儲源代碼

比如,Remote Repository的URL是 http://xxx/git/git_test

二,管理的TargetProject

1,在VS2013中創建Database Project,命名為db_test

2,將DB的Schema同步到Project中

選中Project,點擊-》Schema Compare,彈出一下窗口

  • 左邊是源(Source),右邊是Target,執行Compare,將Source(DB)和Target(Project)進行比較;
  • 點擊Update,更新Target;

三,將Project添加到git Repository

1,將Project添加到源代碼管理

2,選擇源代碼管理 git

第一個選項是TF 版本控制,第二個選項是Git,選擇Git。

四,切換到Team Explorer

1,點擊Home菜單,如果是初次使用,會提示“Install 3rd-party Git command prompt tools”,點擊“install”,按照 git 命令行工具。

在Project下面有Changes,Branches,Unsynced Commits 和 Settings 選項,使用這些選項對Project進行管理

五,管理Local Branch

1,點擊Branches選項,點擊New Branch,在Local Repository中創建Branch

2,合並Branch(Merge Branch)

3,發布Branch(Published Branches)

  • Unpublished Branches :列出在Local Repository中創建的所有Branches
  • Published Branches :列出已經發布到Remote Repository的Branches

六,管理Changes

1,點擊Changes選項

初次使用,會提示“Configure your user name and email address before committing changes”,輸入Name 和 Email即可,這些配置信息,能夠通過“Settings”選項查看。

2,提交修改

修改的提交(Commit) 有三種方式:

  • Commit:提交到Local Repository
  • Commit and Push:提交到Local Repository,並推送(Pull)到Remote Repository
  • Commit and Sync:提交到Local Repository,並同步(Sync)到Remote Repository

3,提交到特定的Branch

Branch選擇db_test,點擊Commit

4,修改已經提交到Local Repository,該Commit並沒有Push 或Sync

5,未同步提交(Unsynced Commits)

點擊"Unsynced Commits"選項,初次使用,需要輸入Remote Repository的URL

6,發布到Remote Repository

輸入Remote Repository的URL,點擊Publish,將提交到Local Repository的Commit,同步到Remote Repository

七,管理代碼同步

在“Unsynced Commits”選項中,能夠對當前的Branch進行代碼Changes的Fetch,Pull,Push操作。Unsynced Commits 的含義是指Local Repository沒有同步到Remote Repository,也指Remote Repository沒有同步到Local Repository,因此,Sync 按鈕的作用是Pull 和 Push的封裝。

1,獲取當前Branch的最新版本(Incomming Commits)

Fetch 和 Pull 都是從當前Branch獲取代碼的最新版本,但是Pull和Fetch是有區別的:Pull=Fetch+Merge:

  • Pull :下拉遠程分支並與本地分支合並,git Pull會將Local Branch更新至Remote Branch的最新狀態;
  • Fetch:只是下拉遠程分支,不會自動執行Merge操作;

當Remote Repository中有Incomming Commits時,推薦使用git Pull來獲取當前Branch的最新版本。

2,將代碼修改Push到Remote Repository(Outgoing Commits)

當存在Unsynced Commits時,通過git Push,將提交到Local Repository的代碼Changes同步到Remote Repository。

八,發布Branch

必須配置Remote Repository之後,才能發布Branch,將Local Branch發布到Remote Repository。

操作步驟:在“Branches”選項中,從“Unpublished Branches”列表中選擇Branch,點擊右鍵,選擇“Publish Branch”,將該Branch發布到Remote Repository中。

九,創建Code Review請求

在TFS中,能夠將代碼的changes發送到同事,請求對代碼進行審查(Code Review),在git中,也能實現Code Review,不過,名稱叫做Pull Request,VS2013中的git插件,不能創建Pull Request,必須通過Web客戶端來創建Pull Request。

創建Pull Request的流程是:

1,首先將Branch發布到Remote Repository,即Publish Branch

2,創建Pull Request

點擊"Create a pull request",創建Pull Request

3,輸入Reviewers,並關聯Work Items

注意,Pull是把Brach和其他Brach進行合並,必須注意要合並的Target Branch。

十,克隆Repository,獲取Remote Repository中代碼的最新版本

點擊"Connect to Team Projects",輸入Remote Repository的URL,將Remote Repository復制到Local Repository中。

Git 教程系列文章

GitHub 使用教程圖文詳解 http://www.linuxidc.com/Linux/2014-09/106230.htm

Git 標簽管理詳解 http://www.linuxidc.com/Linux/2014-09/106231.htm

Git 分支管理詳解 http://www.linuxidc.com/Linux/2014-09/106232.htm

Git 遠程倉庫詳解 http://www.linuxidc.com/Linux/2014-09/106233.htm

Git 本地倉庫(Repository)詳解 http://www.linuxidc.com/Linux/2014-09/106234.htm

Git 服務器搭建與客戶端安裝 http://www.linuxidc.com/Linux/2014-05/101830.htm

Git 概述 http://www.linuxidc.com/Linux/2014-05/101829.htm

分享實用的GitHub 使用教程 http://www.linuxidc.com/Linux/2014-04/100556.htm

Git從入門到學會 http://www.linuxidc.com/Linux/2016-10/135872.htm

Git基本操作詳解 http://www.linuxidc.com/Linux/2016-10/135691.htm

Git 的詳細介紹:請點這裡
Git 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved