Gitの備忘録を記載していく。
リポジトリのチェックアウト
git clone -b ブランチ名※ URL ※タグ名でも可
ブランチ作成
# リモートブランチをローカルにチェックアウト
git checkout -b 作成ブランチ名 origin/リモートブランチ名
# 現在のブランチから派生ブランチを作成
git branch 作成ブランチ名
git push origin ブランチ名
マージ
# マージ
git merge --no-ff --no-commit マージ元のブランチ名
# コミット
git commit -m "コメント"
# プッシュ
git push origin ブランチ名
fatal: refusing to merge unrelated histories
エラーがでる場合
git merge --no-ff --no-commit マージ元のブランチ名 --allow-unrelated-histories
特定のコミットを指定してマージ
git cherry-pick コミットID
git push origin ブランチ名
競合が発生した場合
# マージ元のブランチを最新とする
git checkout --theirs ファイル名(フルパス)
git add ファイル名(フルパス)
# 現在のブランチを最新とする
git checkout --ours ファイル名(フルパス)
git add ファイル名(フルパス)
# ファイルを削除する
git rm ファイル名(フルパス)
初期化
# 最新の状態に戻す
git reset --hard
# 未コミットの新規追加ファイルを削除
git clean -f
Git環境設定
# 長いパスを許容する
git config --global core.longpaths true
# パスワードを記憶
git config --global credential.helper store
# ユーザ情報の設定
git config --global user.name "名前"
git config --global user.email Eメールアドレス
# コミット時の改行コードの自動変換を無効化
git config --global core.autocrlf false
git config --global core.safecrlf true
# ファイル名の大文字小文字を区別
git config --global core.ignorecase false
# 日本語ファイル名の文字化け防止
git config --global core.quotepath false
# 同名のリモートブランチ名が存在する場合のみpushする
git config --global push.default simple
# diffツールにWinMergeを設定 → git windiffコマンドで差分の比較ができるようになる
git config --global diff.tool WinMerge
git config --global difftool.WinMerge.path WinMergeU.exeのフルパス
git config --global difftool.WinMerge.cmd "\"WinMergeU.exeのフルパス\" -f \"*.*\" -e -u -r \"\$LOCAL\" \"\$REMOTE\""
git config --global alias.windiff "difftool -y -d -t WinMerge"
# mergeツールにWinMergeを設定 → コンフリクト時のgit winmergeコマンドで競合の確認、修正ができるようになる
git config --global merge.tool WinMerge
git config --global mergetool.WinMerge.path WinMergeU.exeのフルパス
git config --global mergetool.WinMerge.cmd "\"WinMergeU.exeのフルパス\" -e -u \"\$LOCAL\" \"\$REMOTE\" \"\$MERGED\""
git config --global alias.winmerge "mergetool -y -t WinMerge"
git config --global mergetool.keepbackup false
ローカルファイルからGitリポジトリを作成する手順
# ターミナルから対象のディレクトリに移動
cd 対象ディレクトリ
# git初期化
git init
# git対象外の設定
.gitignoreファイルを作成
New-Item -Type File .gitignore # powershell
# git管理に追加
git add --all
# コミット
git commit -m "コメント"
# リモートのGitリポジトリを追加
git remote add origin XXX.git
# Push
git push origin master