GitUsage - osakichi/memo GitHub Wiki

Gitの使い方

以下の内容はGitのバージョンが上がると不要になるものもあるかも

大雑把な流れ

git clone https://devportal.kamome/git/hoge.git
編集&git add
git commit
git push
git pull

各種設定

メールアドレスと利用者名の設定

git config --global user.email "[email protected]"
git config --global user.name "Kohji Osamura"

日本語ファイル名の正しい表示

指定しないと日本語ファイル名は扱えても正しく表示されない

git config --global core.quotepath false

サーバ証明書の正当性を無視する

httpsアクセスでオレオレ証明書が使われている場合 sshアクセスでは不要

git config --global http.sslVerify false

パスワードの保存設定

git-credential-storeを使う(平文でファイルに保存する)

git config --global credential.helper store

git-credential-cacheを使う(一時的にメモリにキャッシュする)

git config --global credential.helper 'cache --timeout=72000'

他にもいろいろある(wincredとかosxkeychainとかlibsecretとか) ただし使えるのはバージョン1.7.9以降。 これより古い場合は.netrcを使う方法しかない。

リモートリポジトリ作成

リポジトリサーバ上で以下を実行

git init --bare --shared hoge.git

リポジトリの引っ越し

あるリポジトリから別のリポジトリに中身をまるっと移す場合は、 --mirrorオプションを使って以下の手順で行う。

  • git clone --mirror <移行元リポジトリURL> <ローカルコピー名>
  • cd <ローカルコピー名>
  • git push --mirror <移行先リポジトリURL>

例えば以下のような感じ。

git clone --mirror https://github.com/kamome-e/old-hoge.git hoge
cd hoge
git push --mirror https://github.com/kamome-e/new-hoge.git

ローカルコピー名は移行先リポジトリ名と同じでも異なっていても良い。 移行先にはあらかじめ空のリポジトリを作っておくこと。

メンテナンス

https://stackoverflow.com/questions/17683295/git-bash-error-rpc-failed-result-18-htp-code-200b-1kib-s

リモートリポジトリ上で実行。以下をひとつづつ試し、効果がなければ次を試す。

  1. git gc
  2. git gc --aggressive
  3. git repack -a -f -d --window=250 --depth=250
  4. git config http.postBuffer 24288000

Gitのお勉強アプリ

Git-it - 手を動かしながら習得できる日本語対応のGit/GitHub学習アプリ git-it

TIPS

  • ユーザ名の@ Git URLでメールアドレス形式のユーザ名を指定する場合、以下のようにURLエスケープを使うと上手くいくっぽい。 git clone 'https://umino%[email protected]/git/hoge.git' ↑で ちゃんと [email protected] というユーザ名で処理される。

  • コメント 1行目にsummary, 2行目は空行, 3行目以降に詳細 を書くのがよいらしい(GitHubだけ?) gitコマンドで複数行をコメントするときは、1行ごとに-mオプションをつけるらしいよ git commit -m "コメントその1summary" -m "" -m "コメントその2discription" ↓こんな感じになる。

[osakichi hello-world]$ git log
commit 90b01620d0c83c7c7453a5592fb2b7bb5f65f1ad
Author: Kohji Osamura <[email protected]>
Date:   Thu Jan 5 16:33:16 2017 +0900

    コメントその1summary

    コメントその2discription

GitHub Desktopを使うと、summaryとdiscriptionの入力欄が分かれてるので、それに従えばOK。(上記のようにくっついた感じになる)

  • git diffいろいろ 修正した量のサマリーだけ欲しい ⇒こんな感じで--shortstatオプションを使う。
git diff --shortstat <比較元リビジョン> <比較先リビジョン>
 127 files changed, 1363 insertions(+), 202 deletions(-)

特定ディレクトリやファイルのdiffだけ見たい ⇒コマンドの後ろに--で区切ってディレクトリ/ファイルを指定する。

git diff <比較元リビジョン> <比較先リビジョン> -- <ディレクトリ/ファイル名>
--- a/openam-oauth2/openam-oauth2-common/src/main/java/org/forgerock/openam/oauth2/utils/OAuth2Utils.java
+++ b/openam-oauth2/openam-oauth2-common/src/main/java/org/forgerock/openam/oauth2/utils/OAuth2Utils.java
@@ -859,11 +859,13 @@ public class OAuth2Utils {
 
             theID = results.iterator().next();
 
-            //if the client is deactivated return null
+            //if the client is deactivated throws Exception
    :

特定ファイルをdiffから除外したい ⇒リポジトリトップ(.gitignoreとかを置く場所)に.gitattributesというファイルを置き、  以下の感じで除外したいファイルを記述する。

vi <リポジトリトップ>/.gitattributes
cat <リポジトリトップ>/.gitattributes
Makefile -diff
pom.xml -diff

.gitattributesは他にもいろいろ使えそう。

⚠️ **GitHub.com Fallback** ⚠️