0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Git学習記録 Part 2】効率を上げる設定と、ブランチ・マージ・コンフリクトの理解

Posted at

Git学習2日目では、作業効率を上げる設定(エイリアス)、.gitignore の使い方、
さらに Git の根幹であるブランチ・マージ・コンフリクトについて学んだ。

ーーーーーーーーーーーーーーーーーーーーーーーーー
エイリアスでコマンドを短縮する
同じコマンドを何度も書くのは非効率。
Gitでは「エイリアス(別名)」をつけることで短縮操作が可能になる。

設定例:

git config --global alias.ci commit
git config --global alias.st status
git config --global alias.br branch
git config --global alias.co checkout

使い方:

git st
git br
git co main

設定は以下に反映される。
~/.gitconfig
~/.config/git/config
ーーーーーーーーーーーーーーーーーーーーーーーーー
バージョン管理したくないファイルを除外する
機密情報や個人ファイルなど、
コミットしてはいけないファイルを .gitignore で除外できる。

重要な管理ポイント。
.gitignore の書き方:

# コメント
# 除外するファイル
index.html

# ルート直下を指定
/root.html

# ディレクトリを丸ごと除外
dir/

# パターン(例:CSSを全部除外)
*.css

ーーーーーーーーーーーーーーーーーーーーーーーーー
変更を元に戻す
作業中の変更を取り消す
古い書き方:

git checkout -- <file>

新しい書き方(Git 2.23以降):

git restore <file>

すべて取り消す場合:

git restore .

「--」が使われる理由は、
ファイル名とブランチ名が衝突するのを避けるため。
ーーーーーーーーーーーーーーーーーーーーーーーーー
ステージした変更を取り消す
ファイルをステージから外したいとき:

git restore --staged <file>

ディレクトリなら:

git restore --staged <directory>

すべて取り消す場合(新しい書き方):

git restore --staged .

古い書き方:

git reset HEAD .

ーーーーーーーーーーーーーーーーーーーーーーーーー
直前のコミットをやり直す
コミットメッセージの修正や小さな変更を加えたいとき:

git commit --amend

ただし、
リモートにすでに push したコミットを amend すると履歴が破壊される。
push 済のコミットには使わない。
ーーーーーーーーーーーーーーーーーーーーーーーーー
リモートリポジトリ関連の操作
登録されているリモートの確認

git remote
git remote -v

新しいリモートの追加

git remote add <remote name> <remote url>

例:

git remote add tutorial https://github.com/user/repo.git

「tutorial」というショートカット名で扱えるようになる。
リモートの詳細を確認

git remote show origin

リモート名の変更と削除

git remote rename <old> <new>
git remote remove <name>

ーーーーーーーーーーーーーーーーーーーーーーーーー
fetch と pull の違い
fetch(取得だけ)

git fetch origin

•リモートの変更をローカルにダウンロード
•ただし、ワークツリーには反映されない
反映するには、別途マージする:

git merge origin/main

ーーーーーーーーーーーーーーーーーーーーーーーーー
pull(取得+マージ)

git pull origin main

つまり pull は内部的に

git fetch origin main
git merge origin/main

を同時に行っている。

使い分け
•pull は便利だが、今いるブランチにそのまま統合してしまうため、誤操作が起こりやすい
•安全に確認したいときは fetch → merge が良い
ーーーーーーーーーーーーーーーーーーーーーーーーー
ブランチの基礎
ブランチとは、作業を並行して進めるための分岐。
他の人の作業に干渉せず進められる。

ブランチは 「コミットIDを指すポインタ」 であり、
HEAD は「今作業しているブランチ」を指す。
ーーーーーーーーーーーーーーーーーーーーーーーーー
ブランチ操作
ブランチの作成

git branch <branch name>

ブランチ一覧の表示

git branch
git branch -a

ブランチの切り替え

git checkout <branch name>

作成と切り替えを同時に行う

git checkout -b <branch name>

ーーーーーーーーーーーーーーーーーーーーーーーーー
マージ(変更を取り込む)

git merge <branch name>

またはリモート:

git merge origin/<branch name>

ーーーーーーーーーーーーーーーーーーーーーーーーー
マージの種類
fast-forward
•枝分かれしていない場合
•ポインタを前に進めるだけ

auto merge(通常のマージ)
•枝分かれしている場合
•新しい「マージコミット」が作成される
•親コミットが2つ(または複数)になる
ーーーーーーーーーーーーーーーーーーーーーーーーー
コンフリクト(衝突)
同じファイルの同じ行を複数人が編集すると発生する。

コンフリクト発生例:

CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.

git status で確認すると:

Unmerged paths:
  both modified: index.html

解決方法は以下のいずれか:
1.ファイル内容を修正する
2.<<<<<<<, =======, >>>>>>> の記述を削除して整える
image.png
ーーーーーーーーーーーーーーーーーーーーーーーーー
今日学んだことのまとめ
•エイリアスで作業効率が大幅向上する
•.gitignore はセキュリティと品質管理に重要
•restore/reset は「どの状態に戻すのか」を理解する必要がある
•fetch と pull は「確認してから統合するか」「自動で統合するか」の違い
•ブランチは並行開発の基本
•マージの種類によって履歴が変わる
•コンフリクトは避けられないので、仕組みを理解して対処する

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?