「[[.NET 開発基盤部会 Wiki>http://dotnetdevelopmentinfrastructure.osscons.jp]]」は、「[[Open棟梁Project>https://github.com/OpenTouryoProject/]]」,「[[OSSコンソーシアム .NET開発基盤部会>https://www.osscons.jp/dotNetDevelopmentInfrastructure/]]」によって運営されています。

-[[戻る>Git]]
--[[Gitの基本]]
--Gitコマンド
--[[インストール>Git#f825d4d1]]

*目次 [#pda27e8e]
#contents

*基本操作 [#t6467371]
-わざわざ、Gitコマンドを使う ≒ Linux上での操作を想定
-複雑な操作は、GitHub上で行う想定(マージはプルリクエスト(実体はマージリクエスト)で)。
-試してみたが、俯瞰性能の低いCUIは使い難い。最低限、[[git-gui]]を使った方が良さそう。
-GitHubで認証を求められたら、https://github.com/settings/tokens で発行したトークンを指定する。
-試してみたが、俯瞰性能の低いCUIは使い難い。編集中は最低限、[[git-gui]]を使った方が良さそう。

**インストール [#zacbd2ed]

***バージョン表示 [#j1060f65]
 git --version

***インストール [#qbfa055f]
インストールされない場合はインストール
 sudo apt update
 sudo apt install git

**準備 [#m875bb31]
Gitにあるリポジトリを操作~
(testリポジトリのtestブランチ)

***clone [#d3cde2a4]
 git clone https://github.com/daisukenishino2/test.git
 cd test

***branch切替 [#x66ec82d]
-既存
 git switch test

-新規
 git switch -c your-task

**開発 [#bc235d98]

***編集 [#k4c56650]
 nano README.md

***確認 [#teb2586c]
-変更のあったファイルを確認
 git status

-それぞれの差分を確認
 git diff

***追加 [#d48dd1fc]
ステージングに追加

-ファイル指定
 git add README.md

-すべてのファイル
 git add .

-間違った場合(作業ディレクトリに何の変更も加えることなくステージエリアをreset)
 git reset

※ 基本的に1ファイル1ファイル指定したほうが良さそう。

***commit [#j950f480]
 git commit -m "updated."

***pull [#n9f24272]
git pullコマンドはgit fetchコマンドとgit mergeコマンドを一括実行、必要に応じてコンフリクトを解決する。
 git pull origin <branch>

***push [#yec449a2]
pullしてpushすればコンフリクトはない。
 git push origin <branch>

※ 任意の認証が必要になる。

*repository [#bcd73d11]

**clone [#md913091]
git clone は既存の git repositoryのclone (コピー) を作成する

-Git clone | アトラシアン Git チュートリアル~
https://www.atlassian.com/ja/git/tutorial/git-basics#!clone
--既存の Git repositoryのコピーを作成する
--コピー元repositoryをポイントする origin という名称のremote接続を自動で作成。
--svn checkout と異なり、作業コピーがそれ自身で
---完全な git repositoryを構成する。
---履歴を持ってファイルを管理する。

-command
--local or remoteのrepositoryをcloneする。
 git clone <repo> 
--クローン先のdirectoryを指定してcloneする。
 git clone <repo> <directory>

**repository間 [#p50d4a25]

***git remote [#pd21aa82]

-Git remote | アトラシアン Git チュートリアル~
https://www.atlassian.com/ja/git/tutorial/remote-repositories#!remote
--他のrepositoryとの接続の作成、内容確認、削除を行う
--非短縮 URL への参照として使用可能な短縮名称として機能

***git fetch [#u260b8a6]
remote repositoryからfetchする。

-Git fetch | アトラシアン Git チュートリアル~
https://www.atlassian.com/ja/git/tutorial/remote-repositories#!fetch
--remote repositoryからlocal repositoryにbranchをインポートする。
--local branchとしてではなく、remote branchとして保存される。
--pullと異なりlocal repositoryにmergeする前に変更内容を確認できる。

-commit
--remoteのすべてのbranchをfetchする。
 git fetch <remote>
--remoteの特定のbranchをfetchする。
 git fetch <remote> <branch>

***git pull [#r6fe7f69]
remote repositoryからpull(fetch & merge)する。

-Git pull | アトラシアン Git チュートリアル~
https://www.atlassian.com/ja/git/tutorial/remote-repositories#!pull
--git fetch commandとそれに続く git merge commandをひとつにまとめたcommand

-command
--現在のbranchにremote branchをfetchして即時merge
 git pull <remote>
--remote branchを現在のbranchにmergeする際に git [[rebase]] commandを使用
 git pull --rebase <remote>

***git push [#ne215224]
remote repositoryへpushする。

-Git push | アトラシアン Git チュートリアル~
https://www.atlassian.com/ja/git/tutorial/remote-repositories#!push
--local repositoryからremote repositoryにbranchをエクスポートする。
---localな変更の中央repositoryへの公開
---remote repository内で git merge master commandを実行する場合と同等
---push先は --bare フラグを指定して作成したrepositoryに限定するべき。

-command
--指定したbranchを <remote> にpushする
 git push <remote> <branch>
--すべてのlocal branchを指定したremote repositoryにpush
 git push <remote> --all
--すべてのlocal tagをremote repositoryに送る
 git push <remote> --tags

*branch [#df55a0b8]

**branch [#tf3a027e]
branchする

-Git branch | アトラシアン Git チュートリアル~
https://www.atlassian.com/ja/git/tutorial/git-branches#!branch
--branch(独立な開発ライン)の作成
--作業ディレクトリやステージングエリア、プロジェクト履歴を全く新しく作成する手段

-command
--現在のbranchから<new-branch>をbranchする~
「作成するだけで、切り替えはしない」 ので注意
  git branch <new-branch>

**checkout [#u1308258]
branchをcheckoutする(切り替える)。

-Git checkout | アトラシアン Git チュートリアル~
https://www.atlassian.com/ja/git/tutorial/git-branches#!checkout
--branchを切り替える。
--HEADが移動することで、使用するbranchが変更される。

-command
--既存branch <existing-branch> に切り替える。
 git checkout <existing-branch>
--新規branch <new-branch> を作成して即時checkout
 git checkout -b <new-branch>
--既存branch <existing-branch>から新規branch <new-branch> を作成して即時checkout
 git checkout -b <new-branch> <existing-branch>

**merge [#u18db080]
branchをmergeする。

-Git merge | アトラシアン Git チュートリアル~
https://www.atlassian.com/ja/git/tutorial/git-branches#!merge
--branch(分岐した履歴)を現在のbranchへ統合する手段

-command
--指定したbranchを現在のbranchにmergeする
 git merge <branch>
-- 「早送り」可能であっても、常にmerge commitを作成してmerge
 git merge --no-ff <branch>

-参考
--merge commitとFast-forward merge - Qiita~
http://qiita.com/shyamahira/items/59ff8aa1cf7b893aab60

**削除 [#v5c058a3]
branchを削除する。

-mergeされたbranchを削除する
 git branch -d <experimental>
-mergeされたかどうかにかまわず削除する
 git branch -D <experimental>

*開発 [#w9a915cb]

**ステージング [#y7ac6e3e]

***git add [#td795c21]
作業ディレクトリ内の変更をステージングエリアに追加

-Git add | アトラシアン Git チュートリアル~
https://www.atlassian.com/ja/git/tutorial/git-basics#!add
--個々のファイルの変更内容を次回commitの対象とすることを Git に指示。
--git commit を実行するまでは変更がは実際に記録されない。

-command
--<file> に加えられたすべての変更をステージして次回のコミットの対象とする。
 git add <file>
--<directory> 内のすべての変更をステージして次回のコミットの対象とする。
 git add <directory>
--インタラクティブなステージングセッションを開始する。
 git add -p

***git clean [#maa79d68]
作業ディレクトリから追跡対象外のファイルを削除する

-Git clean | アトラシアン Git チュートリアル~
https://www.atlassian.com/ja/git/tutorial/undoing-changes#!clean
--通常の rm コマンド同様 git clean コマンドも元に戻すことはできない。

-command
--git clean の「予行演習」を行うコマンドで削除されるファイルを表示する。
 git clean -n
--追跡対象外のファイルをカレントディレクトリから削除する。~
ただし、.gitignore. で指定したファイルは削除しない。
 git clean -f
--追跡対象外のファイルを指定したパスから削除する。
 git clean -f <path>
--追跡対象外のファイルとディレクトリをカレントディレクトリから削除する。
 git clean -df
--追跡対象外ファイルと通常無視されるファイルをカレントディレクトリから削除する。
 git clean -xf

***git reset [#y5c171a9]
ステージングエリアをリセットする(ワーキングツリーは変更されない)。

-Git reset | アトラシアン Git チュートリアル~
https://www.atlassian.com/ja/git/tutorial/undoing-changes#!reset

-command
--作業ディレクトリに変更も加えずに、指定したファイルをステージングエリアから削除する
 git reset <file>
--作業ディレクトリに何の変更も加えることなくステージエリアをresetして直前のcommit時の状態と一致させる
 git reset
--ステージエリアと作業ディレクトリをresetして直前のcommit時の状態と一致させる
 git reset --hard

**commitの操作 [#ufb99886]
&lt;commit&gt; = commit の"SHA-1 id"

***git commit [#sd4b5e04]
変更のcommit

-Git commit | アトラシアン Git チュートリアル~
https://www.atlassian.com/ja/git/tutorial/git-basics#!commit
--ステージされたスナップショットを取り込んでlocal repositoryにcommitする。
--スナップショットは常にlocal repositoryにcommitされ、他のrepositoryには影響を与えない。

-command
--ステージされたスナップショットをcommit。
 git commit
--commit メッセージ付きでcommit。
 git commit -m "<message>"

***git reset --hard [#y5c171a9]
履歴を完全に戻したい(かつ単独作業中)

-Git reset | アトラシアン Git チュートリアル~
https://www.atlassian.com/ja/git/tutorial/undoing-changes#!reset
--commit済みのスナップショットを削除する目的でも使用される。
--git revert コマンドは変更を元に戻す「安全な」方法
--コレに対し、git reset コマンドは変更を元に戻す「危険な」方法。
--復元方法が無いため、ローカルな変更を元に戻す場合に限るべき。

-command

--現在のbranchの先端を <commit> の位置に戻した上でステージングエリアをその状態と一致するように元に戻すが、作業ディレクトリのみはそのままにしておく。~
これにより、変更規模が小さく整理されたスナップショットを作成してlocal repositoryへ再commitできる。
 git reset <commit>

--現在のbranchの先端を <commit> の位置に戻した上でステージングエリアおよび作業ディレクトリをその状態と一致するように元に戻す。~
commit前の変更に加えて <commit> の後に行われたすべてのcommitも全くなかったものとなる。
 git reset --hard <commit>

***git revert [#j96c5e46]
指定したコミットを「打ち消す」新しいコミットを作る(履歴を保持しながら変更を戻す)。

-Git revert | アトラシアン Git チュートリアル~
https://www.atlassian.com/ja/git/tutorial/undoing-changes#!revert
--履歴を保全し、履歴の完全性とコラボレーションの信頼性を維持・確保する。
--commitがなかったものとするのではなくそのcommitによって加えられた変更を元に戻す方法を見出してその結果を新しいcommitとして追加する。

-command
--特定のcommitのrevert
 git revert <commit>

*確認 [#ed1a8ab6]

**branch [#l771f1ea]

***git branch [#t5e56642]
branchの確認

-Git branch | アトラシアン Git チュートリアル~
https://www.atlassian.com/ja/git/tutorial/git-branches#!branch
--一覧表示

-command
--local branchの確認
 git branch
--remote追跡branchを表示
 git branch -r
--local branchとremote追跡branchの両方のbranchを表示
 git branch -a
--名称の変更
 git branch -m <branch>

**ステージング [#b46c38af]

***git status [#c4ba7161]
statusの確認

-Git status | アトラシアン Git チュートリアル~
https://www.atlassian.com/ja/git/tutorial/git-basics#!status
--作業ディレクトリとステージングエリア(ステージされたスナップショット)の状態を確認
---ステージされた変更内容、
---ステージされていない変更内容
---Git による追跡の対象外となっているファイル

-command
--ステージされたファイル、されていないファイル、追跡対象外のファイルを一覧表示。
 git status

**commit [#dde136ae]

***git log [#aa03bcf5]
logの確認

-Git log | アトラシアン Git チュートリアル~
https://www.atlassian.com/ja/git/tutorial/git-basics#!log
--commit済みのスナップショットを表示するcommand
--commit済みの履歴 (commit履歴) のみが対象
---commit済み変更履歴の一覧表示
---それに対するフィルター処理
---特定の変更内容の検索

-command
--commit履歴全体をデフォルトの形式で表示
 git log
--表示するcommit数を <limit> に制限する。
 git log -n <limit> 
--commit履歴を概観する(各々のcommitの内容を1行に圧縮して表示)
 git log --oneline
--改変されたファイルおよびその中での追加行数と削除行数を増減数で表示
 git log --stat
--各々のcommitに対応するパッチ(commitの完全な差分情報)を表示します。
 git log -p

*Rebase、Reset、Revert [#h1b9459f]
Rebase、Reset、Revert の違いと使い分け

**Rebase [#j203a3fa]
「履歴を綺麗にしたい」場合に使う(ローカルでのみ行うべき)

***概要 [#g5360a3f]
-ブランチの基点(親コミット)を変更することで、履歴を整理
-ブランチを最新の `main` ブランチに追従させる(の最新の状態に適用し直す。)

***動作イメージ [#z60b8dd4]
https://www.google.com/search?q=Rebase&udm=2

***注意点 [#g68d11bd]
-履歴を書き換えるため、リモートにプッシュしたコミットに対しては使わない。
-既にリモートにpushしたコミットの履歴をrebaseで書き換えた後、push --forceすると結果として、チームの開発効率が低下する。
--他の開発者のローカル履歴とリモートの履歴が不一致になり、何が起こったのか分からず、他の開発者が混乱する。
--リモートの意図しない履歴を書き換える可能性があり、また、他の開発者のコミットが消える可能性がある。

**Reset [#jde6ea7e]
「間違えてコミットした」場合に使う(ローカルでのみ行うべき)

***概要 [#le07b64f]
-指定したコミットに対して、現在のブランチを強制的に戻すコマンド。
-履歴を巻き戻すとともに、ワーキングツリーやインデックスの状態をどうするかによって、3つのモードがある。  
|モード|`git reset <commit>` の影響|h
|`--soft`|コミット履歴だけ戻し、インデックスやワーキングツリーの変更はそのまま残る|
|`--mixed`(デフォルト)|コミットとインデックスは戻るが、ワーキングツリーの変更は残る|
|`--hard`|コミット、インデックス、ワーキングツリーすべてを戻す(変更が完全に消える)|

***注意点 [#b0a1d65c]
- `--hard` を使うと 変更が完全に消える ので、慎重に使うこと。
- リモートにプッシュ済みの履歴を変更すると他の開発者に影響を与える。

**Revert [#yc7d4458]
「リモートに影響を与えずに変更を取り消したい」(安全なので推奨される)場合に使う

***概要 [#ef4308a4]
-履歴を維持しながら特定のコミットの変更を取り消すため、リモートリポジトリに影響を与えずに修正できる。
--指定したコミットを打ち消す「新しいコミット」を作成するコマンド。  
--履歴を改変せずに変更を取り消すことができるため、安全な方法。

***注意点 [#m9c5025d]
--履歴が残るため、リセットのように「なかったこと」にはならない。
--逆順にrevert(打ち消す「新しいコミット」)してもコンフリクトが発生する場合がある。

***比較表 [#c9211f1f]
|コマンド|目的|履歴の改変|影響範囲|h
|rebase|履歴を整理・ブランチを最新にする|する|ローカルのみ(要 `push --force`) |
|reset|コミットを取り消す・巻き戻す |する|ローカルのみ(特に `--hard` に注意) |
|revert|特定のコミットを打ち消す |しない|リモートでも安全|

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS