👨🏻‍💻

[Git]Git config操作 ナレッジ

2021/11/08に公開

よく忘れるので自分用メモ

基本操作(よく利用する。)

  • add
git add .
  • commit
git commit -m "TestCommit"
  • push
git push origin HEAD

HEADを指定するとカレントブランチ
macOSだと大文字小文字が区別されないのでheadでもpush可能 他OSだと以下エラーになる可能性がある。

error: src refspec head does not match any
error: failed to push some refs to 'https://github.com/XXXXXXXXXXX.git'
  • fetch
git fetch
  • pull
git pull origin HEAD
  • checkout(ブランチ切り替え)
git checkout develop
  • checkout(新規ブランチ作成)
git checkout -b develop

既にある場合はエラーになる。

  • branch確認
git branch -a
  • tag付与
git tag -a v1.0.0 -m 'Tag add.'
  • tag push
git push origin v1.0.0

GitHubであればmerge後Releasesを作成する。

  • 確認
git show

CodeCommit設定

前提条件

codecommit操作可能な権限を所持かつ AWS profile設定済(AWS SSOでもOK)

SSH版

  1. AWSコンソールでIAM SSH鍵を払い出す。
  1. ~/.ssh/configに以下を定義
Host git-codecommit.*.amazonaws.com
User AXXXXXXXXXXXXX001
  1. Clone
git clone ssh://AXXXXXXXXXXXXX001@git-codecommit.us-east-2.amazonaws.com/v1/repos/MyDemoRepo my-demo-repo

複数定義が必要がなければAXXXXXXXXXXXXX001@の箇所は省略可能

HTTPS版

公式だと以下だが、想定した通りの挙動にならなかったので、個人用のまとめる。

  1. AWSコンソールでもCLIでもいいのでcodecommitのリポジトリを作成する。
  2. git ローカル作業
REPONAME=[1で作成したリポジトリ名]
PROFILE=[AWSプロファイル名]
REGION=$(aws configure get region --profile ${PROFILE})
git init ${REPONAME}
cd ${REPONAME}
  1. git初期設定
git config --local credential.helper "\!aws --profile ${PROFILE} codecommit credential-helper \$@"
git config --local credential.UseHttpPath true
git remote add origin https://git-codecommit.${REGION}.amazonaws.com/v1/repos/${REPONAME}

トラブルシュート

fatal: unable to auto-detect email address

ユーザ設定しないとfatal: unable to auto-detect email addressが起こる。

  • 参考
  • global(全体)
    ユーザ設定
git config --global user.name "ユーザー名"

メール設定

git config --global user.email メールアドレス

確認

git config -l --global

補足:~/.gitconfig に情報が追記されている。

利用している環境でユーザが統一できていれば問題ないが、
特定のリポジトリは認証情報を変えたいという場合はlocalで設定する必要がある。
  • local(該当リポジトリのみ)

ユーザ設定

git config --local user.name "ユーザー名"

メール設定

git config --local user.email メールアドレス

確認

git config -l --local

補足:リポジトリ名/.git/config に情報が追記されている。


GitHubで編集を提案

Discussion