LoginSignup
1
2

More than 1 year has passed since last update.

Gitの基本的な使い方~ローカル編~

Posted at

Introduction

Gitをローカルのみで使う時に必要な操作についての個人的メモ.
ターミナルでのコマンド操作とVSCodeでのGUI操作の両方まとめる.
環境はWindows

バージョンの確認

Gitがインストールされているかをまずは確認

terminal
git version
git version 2.38.0.windows.1

ユーザー設定

まずはユーザー名とメールアドレスを設定
この作業はgitインストール後に一度だけ実行すればよい

terminal
git config --global user.name "ユーザー名"
git config --global user.email "メールアドレス"

正しく設定できたか一応確認

terminal
git config --global --list
user.name=ユーザー名
user.email=メールアドレス

ローカルリポジトリの作成

フォルダ内に隠しファイル.gitが作成される

terminal
git init
Initialized empty Git repository in フォルダの場所

vscodeではソース管理タブの"initialize Repository"をクリック
init_vs.PNG

ステータスの確認

terminal
git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        sample.md

nothing added to commit but untracked files present (use "git add" to track)

vscodeではまだgitの管理下にないためsample.mdの横にU(Utracked)と表示される
image.png

ステージング

git管理下のすべてのファイルをステージング

terminal
git add .

ソース管理タブChanges内のマークをクリック
add_vs.PNG

ステージングの取り消し

ステージングの取り消し

terminal
git reset .

ソース管理タブStaged Changes内にカーソルを合わせると出現するマークをクリック
reset_vs.PNG

コミット

コミットメッセージを"first commit"としてコミット

terminal
git commit -m "first commit"
[master (root-commit) e1c3610] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 sample.md

ソース管理タブ内のメッセージ入力バーにコミットメッセージを入力してCommitをクリック

commit_vs.PNG

ブランチの作成

基本的にファイルの修正などはmasterブランチではなく新しいブランチを作成して行う.

terminal
git branch edit-file
  1. masterをクリック
  2. ポップアップが表示されその中のCreate new branchをクリック
  3. ブランチ名を入力しエンターキー
    1
    branch.PNG
    2
    branch2.png
    3
    branch3.png

ブランチの確認

現在選択されているブランチに*マークがつく

terminal
git branch
  edit-file
* master

ソース管理内に表示されているブランチが現在の選択されているもの
ブランチ名をクリックすると作成済みのブランチ一覧がポップアップする
branch.PNG

ブランチの切り替え

terminal
git checkout edit-file
  edit-file
Switched to branch 'edit-file'

ブランチ名をクリックしポップアップした中から移動先のブランチを選択すればよい
checkout.png

マージ

edit-fileブランチでファイルを修正しコミットした後masterブランチにマージする.
ターミナルではmasterブランチに移動した後以下のコマンドを実行

terminal
git merge edit-file
Updating e1c3610..24d49ef
Fast-forward
 sample.md | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
  1. masterブランチに移動した後・・・をクリック
  2. ポップアップからBranch→Merge Branchをクリック
  3. ブランチ一覧が表示されるのでマージしたいブランチを選択
    1
    merge.PNG
    2
    merge2.png
    3
    merge3.png

差分の確認

terminal
git diff

vscode上では変更点が緑で表示され,緑の部分をクリックすると差分が見れる
diff.PNG

ログの確認

terminal
git log

vscodeではログの確認をする機能がデフォルトでは見つからなかった
なので拡張機能のGit Historyを入れることにした
するとソース管理内に時計マークが表示されるようになるのでそれをクリックすることでログが確認可能

log.PNG
log2.PNG
log3.PNG

Conclusion

Vscodeで作業するときはターミナルを使用しないGUI操作がやり易いと感じた.
次回はGitHubとの連携をまとめようと思う.

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2