コンテンツにスキップ

基本的な使い方

概要

type-gitは2つの主要なインターフェースを提供します:

  • Git: リポジトリ非依存の操作(clone、init、version)
  • Repo: リポジトリ依存の操作(status、commit、branchなど)

リポジトリの操作

リポジトリのクローン

import { TypeGit } from 'type-git/node';
const git = new TypeGit();
// シンプルなクローン
await git.clone('https://github.com/user/repo.git', './repo');
// オプション付きクローン
await git.clone('https://github.com/user/repo.git', './repo', {
depth: 1, // シャロークローン
branch: 'develop', // 特定のブランチ
});

既存リポジトリを開く

const repo = await git.open('./repo');

新規リポジトリの初期化

await git.init('./new-repo');
const repo = await git.open('./new-repo');

よく使う操作

ステータスの確認

const status = await repo.status();
for (const file of status.files) {
console.log(`${file.path}: ${file.workingTree} / ${file.index}`);
}

ステージングとコミット

// ファイルをステージ
await repo.add(['file1.txt', 'file2.txt']);
// コミット
await repo.commit({ message: 'feat: 新機能を追加' });

ブランチ操作

// ブランチ一覧
const branches = await repo.branch.list();
// ブランチ作成
await repo.branch.create('feature/new-feature');
// ブランチ切り替え
await repo.switch('feature/new-feature');

次のステップ

実践パターンでは以下を解説します:

  • Git LFS操作
  • 進捗トラッキング
  • 中断制御
  • エラーハンドリング