GitHub for Beginners:VS Code で Git と GitHub を始める方法
GitHub Blog は、開発者が VS Code の統合機能を活用して Git リポジトリの初期化やブランチ操作を効率的に行うための実践的なチュートリアルを提供している。
キーポイント
VS Code と GitHub の連携強化
VS Code を使用することで、コンテキストスイッチングを減らし、Git 操作のワークフローを合理化し、生産性を向上させることができる。
リポジトリの初期化とブランチ管理
フォルダを開いてソースコントロールアイコンからリポジトリを初期化する方法や、コマンドパレットを使用してブランチ名を変更する手順が解説されている。
ファイルの状態確認と追跡
ソースコントロールパネルでファイルに付与される「U(未追跡)」の表示の意味を説明し、ファイルをリポジトリに追加して追跡する方法に触れている。
ファイルの追跡とコミット
「U」マークは未追跡ファイルを意味し、プラスボタンで「A」(ステージ済み)に変換してコミットメッセージと共に保存することで GitHub にアップロードする準備が整います。
ブランチの作成と切り替え
主要な機能開発では新しいブランチを作成し、コマンドパレットから「Git: Create Branch」を選択して作業用ブランチに自動切り替えることでコードを分離管理できます。
エディタ内の差分可視化
追加された行は緑線、変更された行は青い模様線、削除された行は赤矢印で示され、ソースコントロールパネルから個別または一括で変更をステージ、破棄、確認できます。
差分ビューの表示方法
VS Code のソースコントロールウィンドウからファイルを選択してサイドバイサイド比較が可能であり、設定によりインラインビューで単一ウィンドウ内に変更を閲覧・編集できます。
重要な引用
Using GitHub in VS Code reduces context switching, streamlines your workflow, and boosts your productivity.
Git is the program for managing that source code, and it can be used in multiple different ways (e.g., from the command line, through VS Code, etc.).
"U" stands for untracked, meaning that these files are new or changed, but have not been added to the repository.
This section of the editor is called the gutter, and this green line reflects a new line of code that you added.
This lets you see all of the changes in a single window without splitting it up over two separate views.
Select the option you want, and then VS Code handles the rest.
影響分析・編集コメントを表示
影響分析
この記事は、開発者コミュニティにおける Git の学習曲線を下げる重要な役割を果たしており、特に初心者や IDE を活用して効率化を図りたいエンジニアにとって実用的なガイドラインとなる。GitHub と VS Code の深い統合を強調することで、オープンソース貢献への参入障壁を下げ、開発ワークフローの標準化を促進する効果がある。
編集コメント
AI ツールの活用以前に、開発の基盤となる Git と IDE の使い方を理解することは不可欠です。この記事は、最新の AI エージェントやツールを効果的に運用するための基礎体力を養うための重要なステップと言えます。
GitHub for Beginners シリーズへようこそ。今シーズンでは多くのトピックを取り上げてきましたので、他のエピソードもぜひご覧ください。最新の回ではオープンソースとは何か、そしてコミュニティにどのように貢献できるかについて詳しく解説しました。
今回は、Microsoft が提供する無料かつ人気のあるソースコードエディタ「VS Code」をご紹介します。VS Code には GitHub と統合された豊富な機能が標準で備わっており、それが本日のテーマとなります。VS Code で GitHub を利用することで、コンテキストの切り替えを減らし、ワークフローを効率化し、生産性を向上させることができます。この記事を読み終える頃には、リポジトリの初期設定、ブランチの切り替え、変更のステージング、コミット、プッシュといった操作を VS Code だけで行えるようになるでしょう。何より素晴らしい点は、エディタから一度も離れることなくこれらすべてが完結することです。
なお、このブログ記事(または動画)に沿って実践したい場合は、Git と VS Code の両方をインストールしておく必要があります。Git のインストール方法について復習したい方は、以前の「GitHub for Beginners」のエピソードをご覧ください。
いつも通り、動画をご覧になりたい場合や参照されたい場合は、「GitHub for Beginners」シリーズの全エピソードを YouTube でご覧いただけます。
まずは基本から
おそらく皆さんは、GitHub がリポジトリ内にコードのコピーのみをホストするリソースであることをすでにご存知でしょう。では Git とは何でしょうか?Git はそのソースコードを管理するためのプログラムであり、コマンドラインから VS Code を通じてなど、さまざまな方法で使用できます。Visual Studio Code(略称:VS Code)は、Git を活用して GitHub 上でコードを管理できるようにしています。
フォルダの初期化
VS Code で Git を使用するための最初のステップは、GitHub のリポジトリを反映させるためにフォルダを初期化することです。
VS Code を開きます。
左側の列にある上部のアイコン(エクスプローラーアイコン)を選択します。これは上下に重なった2つのファイルのような見た目です。
「フォルダを開く」ボタンをクリックします。
ファイルエクスプローラーで、GitHub にアップロードしたいコードが含まれているフォルダを選択し、「開く」をクリックします。
コードを開いた後、「ソースコントロール」アイコンを選択します。デフォルトでは、上から3番目のアイコンです。
「リポジトリの初期化」ボタンをクリックします。
この時点で、UI にいくつかの変更が生じます。まず、左側の下部バーにブランチ名が表示されるようになります。デフォルトは main です。コマンドパレットを使用してブランチ名を変更できます。
コマンドパレットを開くには、Mac では Shift-Command-P を、PC では Ctrl-Shift-P を押します。
コマンドパレットで「rename」と入力し始め、「Git: Rename Branch」コマンドを選択します。
入力ボックスに新しいブランチ名を入力し、Enter キーを押します。
この時点で、左下のブランチ名が新しい名前に更新されているのが確認できます。同じ手順を踏むことで、名前を main に戻すことも可能です。
リポジトリの初期化後にもう一つ目にする変化は、ソースコントロールパネル内の各ファイルの隣に「U」が表示されることです。「U」は untracked(未追跡)を意味し、これらのファイルが新規または変更されたものだが、まだリポジトリに追加されていない状態であることを示しています。ファイルを追跡するには、ファイル名の隣にあるプラス記号をクリックするだけで十分です。すべてのファイルを追跡したい場合は、「CHANGES」という単語の上部にあるプラス記号をクリックします。
これを行うと、選択したファイルがステージングされ、その隣の文字が「A」に変わります。これはファイルがステージングされた状態であることを意味しますが、まだ GitHub へアップロードされていないことを示しています。変更をアップロードするには、ファイルをコミットする必要があります。
ソースコントロールウィンドウ上部のテキストボックスに、コミットの内容を説明するメッセージを入力します。あるいは、テキストボックス内の Copilot アイコンをクリックして、Copilot にコミットメッセージを生成させることもできます。
テキストボックスの下にある Commit ボタンを選択して、変更をコミットします。
これだけです!
ブランチの作成と変更
現在、おそらく main ブランチ上にいます。ウィンドウの左下を確認することで、現在のブランチを確認できます。もしこれが主要なアプリケーションで、新しいコードや機能を追加する場合は、新しいブランチを作成して、その上で作業を行うのが一般的です。
Mac では Shift-Command-P、PC では Ctrl-Shift-P を押してコマンドパレットを開きます。
テキストボックスに「create branch」と入力します。
オプションリストから「Git: Create Branch…」を選択します。
新しいブランチの名前をボックスに入力します。例えば「new-features」などです。
Enter キーを押します。
これを行うと、VS Code が新しいブランチを作成し、自動的にそのブランチに切り替わります。これは画面左下の隅に表示されるブランチ名を確認することで検証できます。
変更の追跡
作業中のブランチに入ったので、ファイルにコードの行を 1 行追加してみましょう。そうすると、追加したコードの隣にあるエディタの右側に細い緑色のラインが表示されます。このエディタの部分はガター(gutter)と呼ばれ、この緑色のラインはあなたが追加した新しいコードの行を示しています。
別の行に移動して、すでに存在するコードの行を変更してみましょう。そうすると、ガター内に模様が入った青いラインが表示されます。このラインは、既存のコードの行に変更を加えたことを示しています。
最後に、ファイル内の変更されていない行に移動して削除します。そうすると、ガターに赤い矢印が追加されるのがわかります。これは、その行のコードがファイルから削除されたことを示しています。
このファイルを変更すると、変更されたファイルが「CHANGES」ヘッダーの下にあるソース コントロール ウィンドウに表示されます。ファイル名にカーソルを合わせると、いくつかのボタンが表示されます。これらには、ファイルを開く、変更を破棄する、変更をステージングするボタンがあります。以前と同様に、複数のファイルに変更がある場合は、「CHANGES」ヘッダーにカーソルを合わせて、未ステージングの変更を確認したり、変更を開いたり、すべての変更を破棄したり、すべての変更をステージングしたりするためのボタンが表示されます。
差分の表示
ファイル内で行った変更内容を確認したい場合があります。VS Code では、別のツールを使用せずに並列比較(diff)を行うことでこれを実現できます。ファイルの変更を確認するには、ソース コントロール ウィンドウで確認したいファイルの名前をクリックします。ここから、ファイル内の変更内容を確認し、差分を比較することができます。
設定によっては、「インラインビュー」と呼ばれる形式で差分を表示することも可能です。
差分ビューの右上にある 3 つの点(…)をクリックしてください。
ドロップダウンメニューから「Inline View」を選択します。
これにより、変更内容を2つの別々のビューに分割せず、単一のウィンドウ内ですべて確認できるようになります。このビューからは、差分ビュー内でも直接編集を行うことができます。
ファイルに対して必要な変更を加えたら、それらを GitHub にアップロードする時です。以前に説明した手順に従って、変更をステージングし、その後ステージングされた変更をコミットしてください。これらのステップを終えると、ソース コントロール ウィンドウにはファイルが表示されなくなるはずです。
ブランチの統合
アップロードした変更は、まだ作業中のブランチ内にあることに注意してください。メイン ブランチに戻ると、加えた変更前の元のコードが表示されます。
ウィンドウの左下にあるブランチ名をクリックします。
ブランチ名はプログラムの上部にあるドロップダウンに表示されます。メイン ブランチを選択します。
これらの変更をメイン ブランチに反映させるには、ブランチの統合が必要です。
ソース コード ウィンドウで、CHANGES の隣にある 3 つの点(…)をクリックします。
コンテキスト メニューで「Branch」にカーソルを合わせ、「Merge…」を選択します。
上部のボックスで、統合したいブランチがプロンプトとして表示されます。メイン ブランチに統合したい変更が含まれているブランチを選択してください。
おめでとうございます!これでメイン ブランチに変更が取り込まれました!
GitHub への公開
プロジェクトを GitHub に公開したい場合を考えましょう。そのためには、ソース コントロール ウィンドウにある「Publish Branch」ボタンをクリックします。VS Code は、これをプライベート リポジトリとして公開するか、パブリック リポジトリとして公開するかを確認するプロンプトを表示します。希望するオプションを選択すると、残りの処理は VS Code が自動的に実行してくれます。
VS Code が公開プロセスを完了すると、プロジェクトが GitHub のリポジトリに公開されたことを通知します。[Open on GitHub] ボタンをクリックして GitHub 上のプロジェクトページへ移動し、オンラインで確認できます。
リポジトリのクローン作成
次に、マシン上で作業できるようにリポジトリをクローンしたい場合を考えましょう。これにより、両方の場所間で変更を同期しながら使用できるローカルコピーが作成されます。リポジトリをクローンする方法は複数ありますが、VS Code 内で行うのは非常に簡単な方法です。
クローンしたいリポジトリのホームページへ移動します。
リポジトリのファイルリストの上部にある緑色の <> [Code] ボタンをクリックします。
ドロップダウンメニューで、リポジトリの URL が表示されているボックスの隣にある [Copy URL to clipboard] ボタンを選択します。
VS Code を開きます。
Mac では Shift-Command-P、PC では Ctrl-Shift-P を押してコマンドパレットを開きます。
"clone" と入力します。
オプションリストから [Git: Clone] を選択します。
URL をボックスに貼り付けます。
貼り付けた URL を使用した [Clone from URL] を選択します。
ポップアップウィンドウが表示され、保存場所を尋ねてきます。プロジェクトファイルを保存したいフォルダを選択してください。
[Select as Repository Destination] ボタンをクリックします。
リポジトリを開くかどうかを尋ねるポップアップメニューが表示されます。[Open] を選択します。
おめでとうございます!これでリポジトリがマシンにクローンされ、ローカル環境で作業を開始できます!
Model Context Protocol
VS Code ですべての作業を手動で行う必要はないことを知っていましたか?Model Context Protocol (MCP) を活用すれば、AI ツールが外部ツールやデータに安全にアクセスできるようになります。最初のステップは、GitHub MCP 拡張機能を追加することです。
左側のナビゲーションバーで、Extensions アイコンをクリックします。
検索ボックスに「@mcp github」と入力します。
リストから GitHub 拡張機能を選択します。
拡張機能の説明欄にある Install をクリックします。
ポップアップが表示され、MCP サーバーの認証を許可するよう求められます。Allow を選択します。
リストから自分の GitHub ユーザー名を選択します。
この時点で、GitHub MCP サーバーがインストールされます。これは、Extensions 表示の下部にあるインストール済み MCP サーバーセクションを確認することで検証できます。MCP サーバーがインストールされた状態で、Copilot チャットを使用してコードを作成させることができます。必要に応じて外部ツールを活用して実行されます。
Command Palette ウィンドウ隣にある Chat アイコンを選択してチャットウィンドウを開きます。
プロジェクトにいくつかの機能を追加するよう Copilot に指示するプロンプトを入力します。
この仕組みの例については、フラッシュカードアプリケーションを改善するための複数の課題を作成するよう Copilot に依頼する手順を紹介した本エピソードの動画版をご覧ください。
次のステップ
これで完了です!開発者が VS Code を使って Git と対話する際に最もよく使われるいくつかの方法をご紹介しました。リポジトリの作成から GitHub への公開、そして最後に AI の活用まで、すべてをカバーしました。より高度なテクニックも存在しますが、これらが最も頻繁に使用される要素となります。
Happy coding!
本記事「GitHub for Beginners: Getting started with Git and GitHub in VS Code」は、The GitHub Blog で最初に掲載されました。
原文を表示
Welcome back to GitHub for Beginners. We’ve covered a lot this season, so make sure to check out our other episodes. Our most recent one was all about open source, what it is and how to contribute to the community.
This time, we’re going to take a look at VS Code, a free popular source code editor provided by Microsoft. It has a fair amount of functionality built in that integrates with GitHub, which is what we’ll be taking a look at today. Using GitHub in VS Code reduces context switching, streamlines your workflow, and boosts your productivity. By the end of this post, you’ll understand how to use VS Code to initialize a repository, switch branches, as well as stage, commit, and push your changes. And the best part is, you’ll be able to do all this without leaving the editor.
Note that if you want to follow along with this blog post (or the video), you will need to install both Git and VS Code. If you need a refresher on how to install Git, you can check out one of our earlier GitHub for Beginners episodes.
As always, if you prefer to watch the video or want to reference it, we have all of our GitHub for Beginners episodes available on YouTube.
First some basics
You probably already know that GitHub is a resource that hosts only copies of your code in repositories. So what is Git? Git is the program for managing that source code, and it can be used in multiple different ways (e.g., from the command line, through VS Code, etc.). Visual Studio Code, often abbreviated as VS Code, leverages Git to enable you to manage your code in GitHub.
Initializing a folder
The first step to using Git with VS Code is initializing a folder to reflect your repository on GitHub.
Open VS Code.
Select the top icon (the Explorer icon) in the left-hand column. It looks like two files on top of each other.
Click the Open Folder button.
In the file explorer, select a folder that has some code you want to upload to GitHub, and click Open.
After opening your code, select the Source Control icon. By default, it’s the third icon from the top.
Click the Initialize Repository button.
At this point, a few things will change in your UI. First, you can see the branch name in the bottom bar on the left-hand side. The default is main. You can rename your branch by using the Command Palette.
To open the Command Palette, press Shift-Command-P on Mac or Ctrl-Shift-P on PC.
In the Command Palette, start typing “rename” and select the Git: Rename Branch command.
In the box, provide the new name of the branch and press Enter.
At this point, you can see that the name of the branch in the bottom-left corner has been updated to the new name. You can rename it back to main by following the same steps.
Another change you’ll see after initializing your repository is that each of the files in the Source Control Panel have a “U” next to them. “U” stands for untracked, meaning that these files are new or changed, but have not been added to the repository. To track a file, you just need to click the plus sign adjacent to the file name. If you want to track all of the files, you can click the top plus next to the word CHANGES.
When you do this, the file(s) that you select will be staged, and the letter next to them will change to “A”. This means the file is staged, but not yet uploaded to GitHub. In order to upload the changes, you’ll need to commit the files.
Enter a message in the text box at the top of the Source Control window describing the commit. Alternatively, you could click the Copilot icon in the text box to have Copilot generate a commit message for you.
Select the Commit button underneath the text box to commit your changes.
It’s that simple!
Creating and changing branches
Right now, you’re likely on the main branch. Remember that you can check the branch by looking in the bottom-left corner of your window. If this were a major app and you were adding new code or features, you’d want to create a new branch and use that for your work.
Open the Command Palette by pressing Shift-Command-P on Mac or Ctrl-Shift-P on PC.
Enter “create branch” in the text box.
Select Git: Create Branch… from the list of options.
Enter the name of the new branch in the box. For example, “new-features”.
Press Enter.
Once you do this, VS Code will create the new branch and automatically transfer you to this branch. You can verify this by looking at the branch name in the bottom-left corner.
Tracking changes you make
Now that you’re in your working branch, go ahead and enter a line of code in a file. When you do this, you’ll notice that a thin green line appears on the right side of your editor next to the code you added. This section of the editor is called the gutter, and this green line reflects a new line of code that you added.
Move to a different line and make some changes in the line of code that already exists. When you do this, you’ll see a blue line with a pattern across it in the gutter. This line indicates that you’ve made changes to an existing line of code.
Finally, move to an unchanged line in the file and delete it. Notice that the gutter adds a red arrow. This indicates that a line of code was removed from the file.
When you modify this file, you can see that the file appeared in your Source Control window under the CHANGES header. If you hover over the filename, you’ll see several buttons appear. There are buttons to open a file, discard your changes, and stage your changes. Similar to before, if you have multiple files with changes, you can hover over the CHANGES header and see buttons that will let you review unstaged changes, open the changes, discard all the changes, and stage all the changes.
Viewing diffs
Sometimes you want to see the changes that you made in a file. VS Code lets you do this by performing side-by-side diffs without needing another tool. To see the changes on a file, click on the name of the file you want to see in the Source Control window. From here you can see the changes in the file and compare the differences.
Depending on your preferences, you can also view your diffs in what is called an inline view.
Click the three dots (…) in the top-right of the diff view.
Select Inline View from the drop-down menu.
This lets you see all of the changes in a single window without splitting it up over two separate views. From this view, you can even make edits inside of the diff view.
Once you’ve made any changes you want to make to the file, it’s time to upload them to GitHub. Following the steps we went over before, go ahead and stage your changes, and then commit your staged changes. Once you finish these steps, you shouldn’t have any files displayed in the Source Control window.
Merging branches
Note that changes you’ve uploaded will still be in your working branch. If you navigate back to the main branch, you’ll see the original code before the changes you made.
Click the branch name in the bottom-left of the window.
Branches names appear in the drop-down at the top of the program. Select the main branch.
In order to get these changes into your main branch, you’ll need to merge branches.
In the Source Code window, click the three dots (…) next to CHANGES.
In the context menu, hover over Branch and select Merge…
The box at the top will prompt you with branches that you want to merge from. Select the branch with the changes you want to merge into main.
Congratulations! Now your main branch has incorporated the changes!
Publishing to GitHub
Let’s say you want to take your project and publish it up to GitHub. To do so, click the Publish Branch button in the Source Control window. VS Code will prompt you with whether you want to publish it as a private or as a public repository. Select the option you want, and then VS Code handles the rest.
Once VS Code finishes the publishing process, it will notify you that the project has been published to a repository on GitHub. You can click the Open on GitHub button to visit your project on GitHub and see it online.
Cloning a repository
Now let’s say you want to clone a repository so that you can work on it on your machine. This creates a local copy that you can use and sync the changes between the two locations. There are multiple ways that you can clone a repository, and this is an easy way to do it in VS Code.
Navigate to the home page of the repository you want to clone.
Click the green <> Code button at the top of the repository file list.
In the drop-down menu, select the Copy URL to clipboard button next to the box containing the repository’s URL.
Open VS Code.
Open the Command Palette by pressing Shift-Command-P on Mac or Ctrl-Shift-P on PC.
Type in “clone”.
Select Git: Clone from the list of options.
Paste the URL into the box.
Select Clone from URL with the URL you pasted after it.
A pop-up window will ask you for a location. Choose the folder where you want to store the project files.
Click the Select as Repository Destination button.
A pop-up manu will appear asking if you want to open the repository. Select Open.
Congratulations! You’ve just cloned the repository to your machine and can start to work on it in your local environment!
Model Context Protocol
Did you know that you don’t have to do everything manually in VS Code? You could leverage Model Context Protocol (MCP) to safely let AI tools access external tools and data. The first step is to add the GitHub MCP extension.
In the left navigation bar, click the Extensions icon.
In the search box, enter “@mcp github”.
Select the GitHub extension from the list.
In the description for the extension, click Install.
A pop-up appears, asking you to allow the MCP server to authenticate. Select Allow.
Select your GitHub username from the list.
At this point, the GitHub MCP server is installed. You can verify this by looking at the bottom of the Extensions view and seeing the section for installed MCP servers. With the MCP server installed, you can use Copilot chat to create some code for you, and it will do so by leveraging external tools where necessary.
Open the chat window by selecting the Chat icon next to the Command Palette window.
Enter a prompt asking Copilot to add some features to your project.
For an example of how this works, check out the video version of this episode which walks through asking Copilot to create several issues to improve a flash card application.
Next steps
And that’s it! We covered some of the most common ways developers use VS Code to interact with Git. We’ve gone over everything from creating repositories, to publishing on GitHub, and even threw in a little bit of using AI at the end. There are more advanced tips, but these elements are what you’ll use most frequently.
Happy coding!
The post GitHub for Beginners: Getting started with Git and GitHub in VS Code appeared first on The GitHub Blog.
関連記事
今日のまとめ
AI日報で今日の重要ニュースをまとめ読み