0
1

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 command を自分用にまとめ

Last updated at Posted at 2024-08-15

はじめに

新しいプロジェクトを開始した時 git のコマンドを思い出せないそこのあなた(自分)のために、散在しているコマンドの手順をここにまとめてあげよう。

開発初手順

1. ローカルでブランチを作成

git switch develop
git pull origin develop

git switch -c feature/@@@@@/@@@@
git branch

2. リモートにプッシュする

# コードに変更を加えずにコミット可能
git commit --allow-empty -m "空コミット"
git push origin HEAD

3. 開発

以下手順を繰り返す

〜開発〜

# 動作テスト
npm run build
npm run dev

# コミット
git add [ファイル名]
git commit -m "こみっとメッセージ"
git push origin HEAD

4. マージする

コンフリクトがあればローカルで解消する
リモートの develop からマージ

git fetch
git merge origin/develop

または
git pull origin develop

または
git pull --rebase origin develop

>違いを理解する
>さらに理解する
fetch & merge がおすすめです。

git add .
git commit -m "コンフリクト解消"
git push origin HEAD

>プルリクエスト承認申請につづく

first step

git -v
git clone h@@@@@

# ローカルでブランチを作成
git switch -c feature/@@@@@/@@@@
# 新しいローカルブランチをリモートから直接作成する
git switch -c feature-branch origin/feature-branch
# git switch -t remotes/origin/feature-branch
# git switch コマンドと -t オプションは、Git 2.23 以降で利用可能????

# 特定のコミットハッシュからブランチを切る
git switch --c 新しいブランチ名 コミットハッシュ

commit

git status -sb
git add .
git commit -m "COMMIT MESSAGE"
git push origin HEAD

ステージングの調整

作業ディレクトリや実際のファイルには変更がありません。

全ての add を完全に削除

# 全てのファイルを取り消し
git rm --cached -r .
# 特定のファイルのみ取り消し
git rm --cached -r file_name

一つ前の add を取り消す

# 全てのファイルを取り消し
git reset HEAD
# 特定のファイルのみ取り消し
git reset HEAD file_name

コミットの調整

# commitの履歴確認
git log
# 1コミット1行表示される
git log --oneline
# 表示するコミットの数を指定
git log -n 10
# パスやファイルを限定する
git log -p

# commitのメッセージ修正
git commit --amend -m "新しいメッセージ"
# 直近のコミットメッセージを変更
git commit --amend
# コミット ID も変更される。実質的には、古いコミットに代わる新しいコミットを作成。

git reset

# commitの打ち消し
git reset [コミットID] # ローカルリポジトリ内のコミット自体を削除する
git revert <コミットのハッシュ値> # コミットと逆のコミットを作成する
git reset --hard HEAD~1
# HEAD~1は直前のコミットを指し、--hardフラグは、コミット後の変更内容を完全に削除し、指定したコミットまでの履歴を変更することを意味します

# 特定のコミットに移動する
git reset --soft [コミットID] # HEAD のみを移動  変更点はステージされたまま(indexに残る)
git reset --mixed [コミットID] # (※デフォルト)HEADとindexを移動  変更点はワークツリーに残る(ステージから外れる)
git reset --hard [コミットID] # HEAD、index、ワークツリーすべて移動  変更点は完全に破棄される

git restore

作業ディレクトリの変更を取り消す

# ファイル指定で変更前に戻す
git restore ファイル名

git show

git show [コミットID] # コミット内容の詳細を表示
# コミットIDを省略またはheadにすることで最新のコミットを示す

git show [コミットID] --stat # ファイルごとの変更数(差分の概要)だけ表示
git show [コミットID] --name-only # 変更されたファイル名のみ表示
git show [コミットID] --name-status # ファイル名と変更タイプ(A:追加, M:変更, D:削除)

ブランチの調整

# ブランチを確認
git branch
# 全てのブランチを確認
git branch -a
# リモートブランチのみを表示
git branch -r
# 現在のコミットの所属するブランチを確認
git branch --con

# ブランチ名の変更
git branch -m <古いブランチ名> <新しいブランチ名>
# 今いるブランチの名前を変更
git branch -m <新しいブランチ名>

# ブランチの削除
git branch -d <ブランチ名>
# マージされていなくても強制的に削除したい場合
git branch -D <ブランチ名>

# ブランチをリモートから削除
git push origin --delete <ブランチ名>

# リモートのブランチをローカルに反映
git branch local_branch origin/remote_branch

# ブランチの比較
git diff <ブランチ名> <ブランチ名>

# ブランチをマージする(指定ブランチがHEADに取り込まれる)
git merge <ブランチ名>

# 現在のローカルブランチが紐づいているリモートブランチを確認する方法
git branch -vv
git status

# 特定のコミットだけ取り込む
git cherry-pick <コミットid>

git diff コマンドの使い方
git log コマンドの使い方
cherry-pick コマンドの使い方

stash

変更の一時撤退

# 変更を退避する
git stash
# 追跡されていないファイルも stash する
git stash -u
# ステージングしてない変更だけを stash する
git stash --keep-index

# stash一覧の確認
git stash list

# 退避した作業を元に戻すと同時に、stashのリストから消す
git stash pop N
# 番号を指定しない場合0番目(最新)がpopされる
git stash pop

# 最新のスタッシュを削除
git stash drop
# N番目のスタッシュを削除
git stash drop N

git stash 参考リンク
git stash コマンド一覧

Gitの情報を見る

# ユーザー名とメールアドレスを確認
git config --global user.email
git config --global user.name
# ユーザー名とメールアドレスを登録
git config --global user.email "you@example.com"
git config --global user.name "Your Name"

# remote urlを確認
git remote -v

# 情報を変更するコマンド
git config --global user.name "新しいユーザー名"
git config user.name "新しいユーザー名"
# コミットの署名文字列が変わる
# メールアドレスを変更した場合、GitHubアカウントと紐づかなくなる

コミットID指定でコミットメッセージを変更

# コミット履歴を表示して、対象のコミットIDを確
git log --oneline
# コミットIDを使って、変更したいコミットの1つ前のコミットからリベースを開始
git rebase -i <commit_id>^

エディタが立ち上がるので、変更したいコミットの行の先頭の pick を reword に変更
エディタが立ち上がるので、そこに表示されたコミットメッセージを編集

リモートにpushしたコミットメッセージを変更したい

git rebase -i HEASD~n # n: 整数(何個前のコミットまで遡るか)
# vi で修正したいコミットの pick を edit に変更
git commit --amend
# vi でコミットメッセージを修正
git rebase --continue
git push --force-with-lease origin <ブランチ名>

最新の状態にする

# リベース基を最新の状態にする
git switch develop
git pull origin develop

# リベースを実行
git switch feature
git rebase develop
# コンフリクトを解決
git add <conflicted-file>
git rebase --continue


# 履歴が書き換えられているため強制プッシュ
git push origin HEAD --force-with-lease

git rebase

git rebase についてはこちらの記事にまとめ直しました


references

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?