Git

update create

Git URL

設定

command file
git config --system /etc/gitconfigなど
git config --global ~/.gitconfig
git config --local リポジトリ内 .git/config

~/.gitconfig

[alias]
        ci=commit
        st=status -s
        br=branch -a
        co=checkout
        me=log --pretty='format:%C(yellow)%h %C(black)%ad %C(green)%an% C(black): %s' --date=iso --author=user@domain

cf. pycodestyle --ignore=W504,E501

.git/hook/pre-commit

追記

# check
for file in `git diff-index --cached --name-only HEAD`; do
        case "${file##*.}" in
        py)
                file "$file" | grep "CR"; test $? -ne 0 || exit 1
                python -m py_compile $file || exit 1
                pycodestyle $file || exit 1
                pydocstyle $file || exit 1
                ;;
        html | css | js | txt | md)
                file "$file" | grep "CR"; test $? -ne 0 || exit 1
                ;;
        *)
                ;;
        esac
done

gitignore

生成サービス
https://www.gitignore.io/
Template
https://github.com/github/gitignore/

Git-flow

URL

stage

git status --porcelain | sed -n 's/^[AM]M //p'
git diff --name-status
git diff --cached --name-status

削除

publishされたfeatureの例

git branch --delete feature/project
git push --delete origin feature/project
git remote show origin
git remote prune --dry-run origin
git remote prune origin

branch deleteでローカルのブランチを削除。push deleteでリモートブランチを削除。
remote showでリモートの状況を表示。
remote prune --dry-runでリポジトリ参照の削除シュミレーション。
remote pruneでリポジトリ参照の削除。

Stash

Stashする。

git stash

コメント付きStash。

git stash save "comment"

Stashされたリスト一覧。

git  stash list

Stashされたものの詳細を表示。

git stash show stash@{N}

show -pにすると差分。
Stashの反映と削除。

git stash pop

pop stash@{N} にすると最新以外を指定。
Stashを反映し、削除しない場合。

git stash apply

unstageをstash。

git stash -k

untrackも含める。

git stash -u

stash listを全部削除。

git stash clear

統計

フォーマットに関して

 git help log

PRETTY FORMATS

追加行と削除行の合計取得.

git log --numstat --pretty="%H" --author='[author]' --since=2018-10-01 --until=2018-10-31 --no-merges |awk 'NF==3 {plus+=$1; minus+=$2} END {printf("%d (+%d, -%d)\n", plus+minus, plus, minus)}'
# check
for file in `git diff-index --cached --name-only --diff-filter=AM HEAD`; do
        case "${file##*.}" in
        py)
                rtn=$(file "$file" | grep "CR");
                if [ "$rtn" ]; then echo $rtn; exit 1; fi
                python -m py_compile $file || exit 1
                pycodestyle $file || exit 1
                pydocstyle $file || exit 1
                ;;
        html | css | js | txt | md)
                rtn=$(file "$file" | grep "CR");
                if [ "$rtn" ]; then echo $rtn; exit 1; fi
                ;;
        *)
                ;;
        esac
done

# check stage v1
if [ "$(git diff --name-only)" ]; then echo "have unstage."; exit 1; fi

複数アカウント

自宅のgitlabと仕事先の混在で、commiterを間違える。
対策: git globalを使わない。

git config --global --unset user.name
git config --global --unset user.email

参考: 複数のGitアカウントを手動切替不要で運用する

git config --local url."github_w".insteadOf "git@github.com"
  • 案: ssh configでhostで設定を切り替える方法
  • 案: shellにfunctionで、実行時に任意のフォルダが含まれていたら、user.nameの設定をドヤんかする とか作るか?
  • 案: httpsあたりとか

forkに追随

参考: fork元のリポジトリへの変更をforkしたリポジトリに反映する

git remote add fork_master git@gitlab.likids.info:koich/mock-api.git
git fetch fork_master
git merge --no-ff fork_master/master