gitとは
チームで共同開発する場合コードを共有しながら作業できる技術で、gitコマンドまたは、UIツールを使ってコードの管理を行う。また、CI/CDを駆使した場合、自動ビルド、リリースが可能となる
Windows PCの場合
git for windowsをインストールする。
個人の識別情報の設定
Git上で誰か作業しているかを識別するための設定
この情報を設定していないとCommitはできない
$ git config --global user.name "your name"
$ git config --global user.email "your email"
よく使われるgitコマンド
# Githubからクローンする(1回目のみ)
git clone https://<address>
# 2回目以後のクローンではなくoriginと同期する
git pull
# ローカルの変更ファイルすべて追加する
git add .
# 限定したファイルを追加(Pythonファイルのみ)
git add *.py
# 追加変更の内容を確認する
git status
# コミットする
git commit -m 'コミット内容'
# プッシュする
git push
# ローカルブランチとリモートブランチを指定してpushする
git push origin <local branch>:<remote branch>
# ローカルブランチとリモートブランチを指定してpushする(強制)
git push -f origin <local branch>:<remote branch>
#現在のブランチを確認する
git branch -a
#ブランチを切り替える
git switch <branch_name>
SSL検証の無効化
gitの設定にSSL検証の無効化を行っていましたが、セキュリティ的によろしくないとのことなので、次の設定を行ってしまった場合は、設定の削除を実施してください。
このリポジトリのオレオレ証明書のSSL検証を無効化の設定
$ git config --global http.sslVerify false
SSL検証を無効化の設定の削除
$ git config --global --unset http.sslVerify
環境によって上手く接続できない場合は以下を実行
OS情報 :
Linux raspberrypi 4.19.118-v7+
Debian 10.4
$ sudo apt-get install --reinstall ca-certificates
$ sudo mkdir /usr/local/share/ca-certificates/cacert.org
$ sudo wget -P /usr/local/share/ca-certificates/cacert.org http://www.cacert.org/certs/root.crt http://www.cacert.org/certs/class3.crt
$ sudo update-ca-certificates
$ git config --global http.sslCAinfo /etc/ssl/certs/ca-certificates.crt
$ git config http.sslVerify false
git コマンド
gitコマンドは色々ありますが、ひとまずgitbucketのリポジトリ複製を行うgit cloneコマンドを記載します。
git clone(リポジトリの複製)
オレオレ認証によるhttpsを設定しているため、git cloneコマンドを実施する際は、オプション「GIT_SSL_NO_VERIFY=true」を付してください。
実行例:対象リポジトリ「respi_works/basic」
GIT_SSL_NO_VERIFY=true git clone https://mjec.ddns.net:39862/git/raspi_works/basic.git
git の使い方
#ローカルのブランチをリモートのブランチから強制上書きする
git reset --hard origin/<branch_name>
#強制Push
git push -f
#ブランチの切り替え
git switch <branch_name>
#変更を追加、コミット、Pushの一連操作
git add .
git commit -m "<commit text>"
#コミットの内容を上書き(変更Editerを表示しない)
git commit --amend --no-edit
#push (上記のコミットの場合は、強制上書きしないとPushできない)
git push -f origin local_branch:origin_branch
# gitコメントまとめる(2はコミットの階層を示す)
git rebase -i HEAD~2
editerでpick=>squashにして閉じる
再度#を入れて閉じる
git push -f origin local_branch:origin_branch
#現在のブランチを確認する
git branch -a
#ブランチ削除(Local)
git branch -D <Local_branch>
#リモートブランチ削除する場合
git push --delete origin branch_name
#リモートブランチで削除されたブランチを反映する
git fetch -p
#リモートブランチをマージする場合
git merge origin <ブランチ名>
#取得と作成を同時に行うとき
git checkout -b create_branch origin/copy_from_branch
#コミットをなかったにすることができる
git rest --soft HEAD
#ローカルブランチを最新にする方法
git branch -a
git fetch origin -p
git reset --hard origin/<branch_name>
git log
#上書きコミットする方法
git add .
git commit --amend --no-edit
git push -f origin <local branch>:<remote branch>
#状態を確認
git log
#行動履歴を見る
git reflog
#Originの情報を見る
git remote -v