初回設定
git config --global user.name "John Doe"
git config --global user.email johndoe@example.com
新規作成
git init <project name>
branch作成
git checkout -b issues/3
ブランチ名変更
# チェックアウトしているブランチの名前変更なら
git branch -m new-branch-name
# ブランチ名を指定して実施するなら
git branch -m old-branch-name new-branch-name
リモートから取得
git checkout -b feature/short-operation origin/feature/short-operation
remote追加
-
https
git remote add origin https://github.com/seriwb/study_gradle.git
-
git
git remote add origin git@github.com:seriwb/angular-sample.git
どちらかの後にpush
git push -u origin master
remoteのURL変更
git remote set-url origin git@github.com:seriwb/grpc-sample.git
clone
git clone ssh://git@hogehoge.com:huga/hoge/foo.git
git clone https://hogehoge.com/huga/hoge/foo.git
ブランチ指定
git clone -b ブランチ名 ssh://git@hogehoge.com:huga/hoge/foo.git
サブモジュールも取得
git clone --recursive ssh://git@hogehoge.com:huga/hoge/foo.git
ブランチ指定してサブモジュールも一緒に取得しつつ、ディレクトリ名を変える
git clone -b ブランチ名 --recursive ssh://git@hogehoge.com:huga/hoge/foo.git foo2
やり直し系
addの取消
git reset ファイル名(*での指定も使える)
commitの取消
git reset --soft HEAD^
変更の取り消し(編集も戻る)
git reset --hard HEAD
ファイル単位で変更を戻したい場合
git checkout HEAD -- test_file.txt
commitの修正
コミットメッセージを間違えたときとか
git commit --amend
gitでリモートのブランチにローカルを強制一致させたい時
git fetch origin
git reset --hard origin/master
submodule
submoduleをリモートの最新に追従
git submodule foreach git pull origin master
submoduleを強制更新したい場合
git submodule update --checkout --force
push後の取消
git log --oneline
の一番上をメモ
git revert --no-edit メモした値
git push
削除
HEAD にマージしたブランチを削除する
git branch -d foo
git branch --delete foo
マージしたかどうかを問わずに削除する
git branch -D foo
リモートブランチ foo を削除する
git push --delete origin foo
または
git push origin :foo
マージ済みのブランチを共有ブランチ残して削除する
git branch --merged | egrep -v '\*|develop|staging|master' | xargs git branch -d
add
何がadd対象かを確認する。
git add -An
addは-nでdry-run
commit
git commit
git commit -m ‘hello git’
git commit --allow-empty -m 'WIP commit.'
push
git push
master以外のリポジトリにプッシュ
git push origin feature-groovy
リモートにローカルのブランチがまだないとき
git push --set-upstream origin ローカルのブランチ
リモートと比べローカルが最新ならforce push
git push --force-with-lease origin feature/nanika
指定のタグの内容で指定のブランチを更新
git checkout refs/tags/タグ名
git push -f origin HEAD:ブランチ名
rebase
git stash
git checkout develop
git pull
git checkout feature/add-settings
git rebase develop
git stash pop
マージ後にpush。
config
git config --list --local
git config --local -l
git config --global push.default matching
鍵作成
ssh-keygen -t rsa -b 4096 -C "メールアドレス"
特定リポジトリのGit情報を変更
対象のgitプロジェクト配下で
git config user.name seri
git config user.email seri.wb@gmail.com
デフォルトorigin設定
git config --add branch.master.remote origin
git config --add branch.master.merge master
向き先変更
git remote set-url origin git@github.com:seriwb/centos-server.git
接続確認
ssh -vT git@github.com
.bashrcへの設定
alias gia='git add'
alias gian='git add -nA'
alias giaa='git add -A'
alias gicm='git commit -m'
alias gica='git commit --amend --no-edit'
alias gib='git branch'
alias gibd='git branch --merged | egrep -v "\*|develop|staging|master" | xargs git branch -d'
alias gic='git checkout'
alias gid='git diff'
alias gis='git status'
alias gip='git push'
alias gipu='git push -u origin'
alias gipl='git pull'
alias gsubp='git submodule foreach git pull origin master'
alias gicd='cd `git rev-parse --show-toplevel`'
コンテナ利用Tips
ゲスト側でホストの鍵情報を利用する
ssh agentに鍵登録しておけばOK。
$ ssh-add $HOME/.ssh/id_rsa