ความเดิมตอนที่แล้ว ได้แนะนำการทำ Version Control ด้วย Git บน Visual Studio 2013 ไปแล้ว คราวนี้มาถึงการใช้งานกับ Visual Studio รุ่นเก่า ๆ กันบ้าง
เนื่องจาก Microsoft ได้เพิ่มความสามารถให้ใช้งานร่วมกับ Git ได้ตั้งแต่ Visual Studio 2013 และได้ออก Plug-in สำหรับ Visual Studio 2012 ด้วย ซึ่งจะสามารถใช้งานได้กับ Visual Studio 2012 Update 3 ขึ้นไป แต่สำหรับผู้ที่ใช้งาน Visual Studio รุ่นเก่ากว่านั้น ก็จะมี Plug-in เสริม ดังที่กล่าวไปในบทความตอนที่แล้ว
แต่ในบทความตอนนี้ จะขอแนะนำการใช้งานร่วมกับ Git โดยไม่ใช้งาน Plug-in ใด ๆ แต่จะใช้ Git บน Windows ผู้อ่านที่ยังไม่ได้ติดตั้ง Git บน Windows แนะนำให้ไปอ่านบทความตอน ใช้ Git เพื่อชีวิต (Source Code) ที่ดีกว่า – การติดตั้ง Git บน Windows และ ใช้ Git เพื่อชีวิต (Source Code) ที่ดีกว่า – การใช้งาน Git GUI เบื้องต้น ก่อนนะครับ
ถ้าใครได้อ่านบทความตอนการใช้งาน Git GUI เบื้องต้นแล้วนั้น ก็คงจะพอรู้แล้วว่า Git ไม่ได้ขึ้นกับภาษาโปรแกรม หรือเครื่องมือ (IDE) ใด ๆ มีเพียง Git บน Windows ก็สามารถใช้งานกับ Visual Studio ได้แล้ว เพียงแต่ว่า เราต้องมาเลือกไฟล์ที่จะเก็บลง Git Repository เอง คงไม่สนุกนัก ถ้าจะต้องมาเลือกไฟล์ทุกครั้งที่เริ่มสร้าง Project ใหม่
สำหรับพระเอก ที่จะมาช่วยให้การเลือกไฟล์สำหรับ Visual Studio Project เพื่อเก็บลง Git Repository นั่นก็คือ gitignore นั่นเอง
ใครยังไม่รู้จัก gitignore สามารถอ่านบทความตอน Git ไม่ต้องมายุ่งน่า~!! มาทำความรู้จักกับไฟล์ .gitignore กันหน่อย ก่อนนะครับ
ถ้าสังเกตุในบทความตอนที่แล้ว จะเห็นว่า VS2013 นั้น ได้สร้างไฟล์ .gitignore ไว้ให้ตั้งแต่เริ่มต้น ซึ่ง Microsoft ได้กำหนดค่าไว้ให้แล้วในไฟล์ เพราะฉะนั้น เราจะขโมยมาใช้กันเลย
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
## Ignore Visual Studio temporary files, build results, and ## files generated by popular Visual Studio add-ons. # User-specific files *.suo *.user *.sln.docstates # Build results [Dd]ebug/ [Dd]ebugPublic/ [Rr]elease/ x64/ build/ bld/ [Bb]in/ [Oo]bj/ # Roslyn cache directories *.ide/ # MSTest test Results [Tt]est[Rr]esult*/ [Bb]uild[Ll]og.* #NUNIT *.VisualState.xml TestResult.xml # Build Results of an ATL Project [Dd]ebugPS/ [Rr]eleasePS/ dlldata.c *_i.c *_p.c *_i.h *.ilk *.meta *.obj *.pch *.pdb *.pgc *.pgd *.rsp *.sbr *.tlb *.tli *.tlh *.tmp *.tmp_proj *.log *.vspscc *.vssscc .builds *.pidb *.svclog *.scc # Chutzpah Test files _Chutzpah* # Visual C++ cache files ipch/ *.aps *.ncb *.opensdf *.sdf *.cachefile # Visual Studio profiler *.psess *.vsp *.vspx # TFS 2012 Local Workspace $tf/ # Guidance Automation Toolkit *.gpState # ReSharper is a .NET coding add-in _ReSharper*/ *.[Rr]e[Ss]harper *.DotSettings.user # JustCode is a .NET coding addin-in .JustCode # TeamCity is a build add-in _TeamCity* # DotCover is a Code Coverage Tool *.dotCover # NCrunch _NCrunch_* .*crunch*.local.xml # MightyMoose *.mm.* AutoTest.Net/ # Web workbench (sass) .sass-cache/ # Installshield output folder [Ee]xpress/ # DocProject is a documentation generator add-in DocProject/buildhelp/ DocProject/Help/*.HxT DocProject/Help/*.HxC DocProject/Help/*.hhc DocProject/Help/*.hhk DocProject/Help/*.hhp DocProject/Help/Html2 DocProject/Help/html # Click-Once directory publish/ # Publish Web Output *.[Pp]ublish.xml *.azurePubxml ## TODO: Comment the next line if you want to checkin your ## web deploy settings but do note that will include unencrypted ## passwords #*.pubxml # NuGet Packages Directory packages/* ## TODO: If the tool you use requires repositories.config ## uncomment the next line #!packages/repositories.config # Enable "build/" folder in the NuGet Packages folder since # NuGet packages use it for MSBuild targets. # This line needs to be after the ignore of the build folder # (and the packages folder if the line above has been uncommented) !packages/build/ # Windows Azure Build Output csx/ *.build.csdef # Windows Store app package directory AppPackages/ # Others sql/ *.Cache ClientBin/ [Ss]tyle[Cc]op.* ~$* *~ *.dbmdl *.dbproj.schemaview *.pfx *.publishsettings node_modules/ # RIA/Silverlight projects Generated_Code/ # Backup & report files from converting an old project file # to a newer Visual Studio version. Backup files are not needed, # because we have git ;-) _UpgradeReport_Files/ Backup*/ UpgradeLog*.XML UpgradeLog*.htm # SQL Server files *.mdf *.ldf # Business Intelligence projects *.rdl.data *.bim.layout *.bim_*.settings # Microsoft Fakes FakesAssemblies/ # LightSwitch generated files GeneratedArtifacts/ _Pvt_Extensions/ ModelManifest.xml |
หรือถ้าสร้าง Repository ใหม่บน GitHub แล้วเลือกเพิ่มไฟล์ .gitignore สำหรับ Visual Studio ก็จะได้ไฟล์ .gitignore ตามด้านล่างนี้ สามารถขโมยมาใช้ได้เช่นกัน
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
## Ignore Visual Studio temporary files, build results, and ## files generated by popular Visual Studio add-ons. # User-specific files *.suo *.user *.sln.docstates # Build results [Dd]ebug/ [Dd]ebugPublic/ [Rr]elease/ [Rr]eleases/ x64/ x86/ build/ bld/ [Bb]in/ [Oo]bj/ # Roslyn cache directories *.ide/ # MSTest test Results [Tt]est[Rr]esult*/ [Bb]uild[Ll]og.* #NUNIT *.VisualState.xml TestResult.xml # Build Results of an ATL Project [Dd]ebugPS/ [Rr]eleasePS/ dlldata.c *_i.c *_p.c *_i.h *.ilk *.meta *.obj *.pch *.pdb *.pgc *.pgd *.rsp *.sbr *.tlb *.tli *.tlh *.tmp *.tmp_proj *.log *.vspscc *.vssscc .builds *.pidb *.svclog *.scc # Chutzpah Test files _Chutzpah* # Visual C++ cache files ipch/ *.aps *.ncb *.opensdf *.sdf *.cachefile # Visual Studio profiler *.psess *.vsp *.vspx # TFS 2012 Local Workspace $tf/ # Guidance Automation Toolkit *.gpState # ReSharper is a .NET coding add-in _ReSharper*/ *.[Rr]e[Ss]harper *.DotSettings.user # JustCode is a .NET coding addin-in .JustCode # TeamCity is a build add-in _TeamCity* # DotCover is a Code Coverage Tool *.dotCover # NCrunch _NCrunch_* .*crunch*.local.xml # MightyMoose *.mm.* AutoTest.Net/ # Web workbench (sass) .sass-cache/ # Installshield output folder [Ee]xpress/ # DocProject is a documentation generator add-in DocProject/buildhelp/ DocProject/Help/*.HxT DocProject/Help/*.HxC DocProject/Help/*.hhc DocProject/Help/*.hhk DocProject/Help/*.hhp DocProject/Help/Html2 DocProject/Help/html # Click-Once directory publish/ # Publish Web Output *.[Pp]ublish.xml *.azurePubxml # TODO: Comment the next line if you want to checkin your web deploy settings # but database connection strings (with potential passwords) will be unencrypted *.pubxml *.publishproj # NuGet Packages *.nupkg # The packages folder can be ignored because of Package Restore **/packages/* # except build/, which is used as an MSBuild target. !**/packages/build/ # If using the old MSBuild-Integrated Package Restore, uncomment this: #!**/packages/repositories.config # Windows Azure Build Output csx/ *.build.csdef # Windows Store app package directory AppPackages/ # Others sql/ *.Cache ClientBin/ [Ss]tyle[Cc]op.* ~$* *~ *.dbmdl *.dbproj.schemaview *.pfx *.publishsettings node_modules/ # RIA/Silverlight projects Generated_Code/ # Backup & report files from converting an old project file # to a newer Visual Studio version. Backup files are not needed, # because we have git ;-) _UpgradeReport_Files/ Backup*/ UpgradeLog*.XML UpgradeLog*.htm # SQL Server files *.mdf *.ldf # Business Intelligence projects *.rdl.data *.bim.layout *.bim_*.settings # Microsoft Fakes FakesAssemblies/ |
ที่เหลือ จะ Commit สร้าง Branch จะ Merge จะติด Tag ก็ทำบน Git ได้เลย จะใช้ Git Bash, Git GUI ก็ตามสะดวกเลยนะ
จบแล้ว… ห๊ะ~!!
เด๋วบทความจะสั้นไป ขอยกตัวอย่างหน่อยแล้วกัน…
คราวนี้ผมจะใช้ VS2008 ทำให้ดูนะครับ ตามมาเลยยยยยยย
สมมติว่า มี Visual Studio Project อยู่แล้ว ดังรูปด้านล่าง ก็ git init ได้เลย

เมื่อลองใช้ Git GUI เปิดดู จะเห็นว่า มีไฟล์ที่เราต้องเลือก ดังรูปด้านล่าง
ที่ทำ Highlight สีเขียวไว้ คือไฟล์ที่ควรจะเก็บ ส่วนสีแดง คือไฟล์ที่ไม่ควรจะเก็บ (เพราะ Visual Studio สามารถ Generate ใหม่ได้ และสามารถลบทิ้งได้ โดยไม่มีผลกับไฟล์ใน Project)

ทำการเพิ่มไฟล์ .gitignore ไว้ที่ Working Directory โดยเนื้อไฟล์ก็ลอกของ VS2013 มาได้เลย ตามที่แปะไว้ด้านบน

เมื่อเปิด Git GUI ดูอีกครั้ง จะเห็นว่า ไฟล์ที่ไม่ต้องการ ไม่ถูกนำมาแสดงผลแล้ว…
กด Stage Changed แล้ว Commit ได้เลย สบ๊ายยยยยยย~!!

สำหรับไฟล์ .gitignore จะเลือกใช้ของ VS2013 หรือของ GitHub ก็แล้วแต่จะชอบ เพราะไม่ต่างกันมากนัก ดูความเหมาะสมของ Project ละกันนะ
ส่วนผม จับไฟล์ .gitignore ของทั้ง 2 จ้าว Merge และ Solve Conflict ซะ ออกมาเป็นดังด้านล่างนี้
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
## Ignore Visual Studio temporary files, build results, and ## files generated by popular Visual Studio add-ons. # User-specific files *.suo *.user *.sln.docstates # Build results [Dd]ebug/ [Dd]ebugPublic/ [Rr]elease/ [Rr]eleases/ x64/ x86/ build/ bld/ [Bb]in/ [Oo]bj/ # Roslyn cache directories *.ide/ # MSTest test Results [Tt]est[Rr]esult*/ [Bb]uild[Ll]og.* #NUNIT *.VisualState.xml TestResult.xml # Build Results of an ATL Project [Dd]ebugPS/ [Rr]eleasePS/ dlldata.c *_i.c *_p.c *_i.h *.ilk *.meta *.obj *.pch *.pdb *.pgc *.pgd *.rsp *.sbr *.tlb *.tli *.tlh *.tmp *.tmp_proj *.log *.vspscc *.vssscc .builds *.pidb *.svclog *.scc # Chutzpah Test files _Chutzpah* # Visual C++ cache files ipch/ *.aps *.ncb *.opensdf *.sdf *.cachefile # Visual Studio profiler *.psess *.vsp *.vspx # TFS 2012 Local Workspace $tf/ # Guidance Automation Toolkit *.gpState # ReSharper is a .NET coding add-in _ReSharper*/ *.[Rr]e[Ss]harper *.DotSettings.user # JustCode is a .NET coding addin-in .JustCode # TeamCity is a build add-in _TeamCity* # DotCover is a Code Coverage Tool *.dotCover # NCrunch _NCrunch_* .*crunch*.local.xml # MightyMoose *.mm.* AutoTest.Net/ # Web workbench (sass) .sass-cache/ # Installshield output folder [Ee]xpress/ # DocProject is a documentation generator add-in DocProject/buildhelp/ DocProject/Help/*.HxT DocProject/Help/*.HxC DocProject/Help/*.hhc DocProject/Help/*.hhk DocProject/Help/*.hhp DocProject/Help/Html2 DocProject/Help/html # Click-Once directory publish/ # Publish Web Output *.[Pp]ublish.xml *.azurePubxml # TODO: Comment the next line if you want to checkin your web deploy settings # but database connection strings (with potential passwords) will be unencrypted *.pubxml *.publishproj # NuGet Packages *.nupkg # The packages folder can be ignored because of Package Restore **/packages/* # except build/, which is used as an MSBuild target. !**/packages/build/ # If using the old MSBuild-Integrated Package Restore, uncomment this: #!**/packages/repositories.config # Windows Azure Build Output csx/ *.build.csdef # Windows Store app package directory AppPackages/ # Others sql/ *.Cache ClientBin/ [Ss]tyle[Cc]op.* ~$* *~ *.dbmdl *.dbproj.schemaview *.pfx *.publishsettings node_modules/ # RIA/Silverlight projects Generated_Code/ # Backup & report files from converting an old project file # to a newer Visual Studio version. Backup files are not needed, # because we have git ;-) _UpgradeReport_Files/ Backup*/ UpgradeLog*.XML UpgradeLog*.htm # SQL Server files *.mdf *.ldf # Business Intelligence projects *.rdl.data *.bim.layout *.bim_*.settings # Microsoft Fakes FakesAssemblies/ # LightSwitch generated files GeneratedArtifacts/ _Pvt_Extensions/ ModelManifest.xml |
จะเอาไปใช้ก็ไม่ได้ว่าอะไรนะ~!!