git
flowchart BT subgraph ローカル環境 subgraph remoteBranch["リモート追跡ブランチ"] origin/hoge upstream/hoge end subgraph localRepo ["ワーキングディレクトリ"] end subgraph index["インデックス (ステージング)"] ローカルブランチ end subgraph alias["リモートリポジトリエイリアス"] origin upstream end end subgraph Github リモートリポジトリ end localRepo <-->|add,commit| index <---|merge,rebase| remoteBranch alias<-->|fetch,push,pull|Github origin/hoge --->|push| origin upstream/hoge <---|fetch| upstream
一番最初だけ
git init
接続先の設定(他リポジトリに付け直すは.gitを削除する)
git remote add origin "push先のアドレス"
設定確認
git remote -v
アップロードするファイルの指定
git add -A #全ファイル git add ”ファイル名”
変更点のメッセージ
git commit # vim的なので編集 git commit -m “メッセージ”
初回プッシュ ※-u: --set-upstream トラッキング設定(コミットの差分管理)
git push -u origin master
ローカルリポジトリのリセット
.gitファイルを削除 →再度git init
ブランチの作成
git branch ”ブランチ名”
ブランチ一覧の確認
git branch
選択的にトラッキング設定(リモートリポジトリのhoge-branchにトラッキング設定)
git branch -u origin/hoge-branch
ブランチのポインタを強制的に変更(HEAD位置から2つ前に)
git branch -f "ブランチ名" HEAD^^
ブランチの変更
git checkout ”ブランチ名” ※現在のブランチの変更をコミットしないとブランチ変更できない
リモートリポジトリのブランチにPush ※origin:リモートリポジトリのエイリアス
git push origin ”ブランチ名”
※サーバーの状況によりエラーになる可能性が有る→時間をおいて再度実行
トラッキングしているローカルブランチとリモートブランチ間でコミットの差分を確認
git status -vv
ログを簡潔に表示
git log --oneline
ブランチのトラッキング状態を確認する
git branch -vv
ブランチを現在のブランチにマージする(Fast-Forward)
git merge ”ブランチ名”
常にマージコミットを残す
git merge --no-ff
指定ブランチの先にコミットを新規に作り足す(マージコミットは残らない)
git rebase "ブランチ名"
ローカルブランチを削除
git branch -d ”ブランチ名”
リモートブランチを削除
git push --delete origin ”ブランチ名”
リモートリポジトリを別リポジトリへ変更
git remote set-url origin "別リポジトリのURL"
#複数アカウントの競合などで403エラー(アクセス権で弾かれる)場合
git config --global user.name "Your Name"
git config --global user.email you@example.com
git remote set-url origin https://<ユーザ名>@github.com/<ユーザ名>/<リポジトリ名>.git
//参考URL
https://hacknote.jp/archives/54105/
変更内容を退避 ※-uオプションで新規作成のファイルも退避
git stash -u
特定のファイルだけ退避
git stash push -- ./filepath
退避内容に名前をつける
git stash -m "my-stash" git stash push ./hoge -m 'hoge'
退避した変更内容のリスト
git stash list
退避した変更内容を適用
git stash apply stash@{num}
退避した変更内容を削除
git stash drop stash@{num}
最新のstashを適用してリストから削除
git stash pop
インデックス変更前の変更をリセットして元に戻す
git checkout HEAD .
git clone フォルダ名
pushされたリポジトリをダウンロードして確認できる
指定したブランチをクローンする
git clone -b ブランチ名 https://リポジトリのアドレス
コミットIDのみ表示
git log --pretty=oneline | awk '{print $1}'
--pretty=format:”%h”
HEADは常に一つ、現在の最新のコミットを指す
リポジトリ内の操作ログを調べる
git reflog
直前の状態に戻す
git reset --hard HEAD^
操作ログの状態に戻す
git reset --hard (hash) ※hashはgit reflogで調べる
コミットのみ直前の状態に戻す(作業状況は変わらない)
git reset --soft HEAD^
^(ハット)に引数を渡すとマージの親を選択できる
2番目のマージの親へ参照を移動
git checkout HEAD2
コミットを2つ前まで戻す(リモートリポジトリには反映されない)
git reset HEAD~2
2つ前の状態のコミットを新たに追加
git revert HEAD~2
HEADから特定のコミットまでのコミット数をカウント
git rev-list --count HEAD ^(コミットのハッシュ)
4つ前までのコミットをインタラクティブ(抽出、順番入れ替え)に操作
git rebase -i HEAD~4
任意のコミットを現在のブランチに追加
git cherry-pick ...(references)
直近のコミットを修正
git commit --amend
コミットにタグを登録
git tag tagname reference(省略可能)
直近のタグ、タグからのコミット数、最新コミットのハッシュを取得
git describe reference
rebaseでツリーを統合
git fetch --rebase
ブランチをリモートブランチへの追跡を設定して作成
git checkout -b hogebranch origin/main
origin(リモートブランチ)のに現在のチェックアウトのコミットをpush
git push origin
sourceの参照(Refspec)へdestinationを移動
git push origin
git bisect
git branch -u o/main foo
//参考URL
https://qiita.com/wann/items/688bc17460a457104d7d
https://www.r-staffing.co.jp/engineer/entry/20200605_1
コミットの情報確認
特定ファイルに関するコミットのみ表示
git log -- filePath
省略して表示
git log --pretty=oneline --abbrev-commit
コミットのメッセージのみ表示
git log --pretty=oneline --abbrev-commit | awk '{print $2}'
特定コミットの内容を表示
git show commitID --name-only
複数アカウントでの使い分け(公開鍵の使い分け)
~/.ssh/configに設定(※windows C:\Users\hp/.ssh/id_rsa)
Host github.sub #任意のホスト名HostName github.com IdentityFile ~/.ssh/id_sub_rsa #サブアカウントの鍵のファイルUser git Port 22 TCPKeepAlive yes IdentitiesOnly yes
上記の設定の公開鍵を使う場合、clone時には下記ホスト名を指定
git clone git@github.sub:username/hoge.git
pull/pushも種々個別に指定する必要がある(デフォルトはgithub.com)
下記でリモートリポジトリのURLを設定しなおす
git remote set-url git@github.sub:username/hoge.git
git push
git config --global/--localでユーザー情報を設定
user.name
user.email
//参考URL
https://zenn.dev/taichifukumoto/articles/how-to-use-multiple-github-accounts
プルリクエストをローカルリポジトリにマージする
git remote add upstream git@github.ak-mugichans:ak-mugichans/PRtest.git
git checkout PRのブランチ
git fetch upstream
git checkout master
git merge upstream/<プルリクエストのブランチ>
特定のコミットIDをclone
git init git add remote origin hoge git fetch --depth 1 origin hogeSHA
//参考URL
https://mebee.info/2021/05/09/post-33725/
コミットを整理
git rebase -i HEAD~n
pick 005667b log1を作成 pick cf4487a log_2を作成 pick 651562b log_3を作成 pick 0ab1b17 log_1を修正 pick 7153c7c log1,2を修正 pick 7160b49 log3のみを修正
edit:コミット直後へ移動
reward:コミットメッセージのみ修正
squash:コミットを統合
#コミット内容に追加する git add #コミット内容を分割する git reset @^ git add git commit
前回のコミットに追加
git commit --amend
//学習サイト