2019年5月19日

Git Commands Cheating Sheet

Abstract
  この投稿では,使用頻度の高い git command とその使用方法を示す.
Initialization
.git の作成
  ローカルリポジトリの作成
$ git init

  リモートリポジトリのクローン
$ git clone git@github.com:[user]/[repository]

.gitignore の作成
  ローカルリポジトリの作成
$ touch .gitignore
$ emacs .gitignore    OR    $ vi .gitignore

ユーザー情報をローカルの Git に登録
  Git のデフォルト値として設定
$ git config --global user.email "[email@example.com]"
$ git config --global user.name "[yourname]"

  リポジトリ別に設定
$ git config --local user.email "[email@example.com]"
$ git config --local user.name "[yourname]"
add / commit / push / pull
add
指定したファイルを追加
$ git add [fileName]
カレントディレクトリのファイルを全て追加
$ git add .
前回と同じファイルを追加
$ git add -u

直前の add を取り消し
$ git reset .

status
add されたファイルを確認する.
$ git status

commit
$ git commit -m "[comment]"
または
$ git commit
 -> writing comment on vi.
 -> :wq
 -> Enter

直前の commit message を上書き
$ git commit --amend -m "[comment]"
直前の commit を取り消し
$ git reset --soft HEAD^

push
$ git push origin master

$ git push -f origin master を行うと,push 元以外のクローンから pull できなくなるので通常は行わない. 多人数での開発では禁止と考えてよい. 逆に,個人ブランチで他人に影響が無い場合は行って問題ない.

pull
  conflict により push に失敗した場合は,pull により conflict を解消する.このとき,automatic merge が行われる.automatic merge に失敗した場合は,ソースコード中にメッセージが自動挿入されるので,修正した後,add -> commit -> push を行う.
$ git pull origin master
branch / rebase / fetch / merge
branch の生成
$ git branch [branchName]

branch の確認
$ git branch

branch の遷移
$ git checkout [brachName]

branch の依存関係を整理
$ git rebase [brachName]

指定した branch を,参照中の branch へマージ
$ git merge origin [brachName]

master branch へ merge する場合は下記.
$ git checkout master
$ git merge origin [brachName]

branch をリモートリポジトリへ push
$ git push origin [brachName]

branch をローカルリポジトリへダウンロード
$ git fetch
$ git checkout [brachName]

branch の削除.(通常はこちら.master へマージされていない branch を削除しようとすると警告が出るため安全.)
$ git branch -d [branchName]

branch の強制削除.(作業に失敗し,branch を破棄する場合に使用.)
$ git branch -D [branchName]

branch tree の可視化
$ git log --oneline --graph --all
Go back to past commit
遷移先ハッシュ値の調査
$ git log
または
$ git log --oneline --graph --all

指定したハッシュ値へ遷移
$ git reset --soft [hashVal]

※ このとき,--hard オプションを用いると,遷移元の履歴が削除される.遷移先までのデータを破棄する場合以外では用いない.
Deletion of $ git push [2]
安全な手順 (コミット履歴は残る).
直前のコミットを打ち消す
$ git revert   OR   $ git revert HEAD
直前の N コミットを打つ消す
$ git revert HEAD~[N-1]
※コミット履歴が残るのが難点.

不安全な手順 (コミット履歴は消える).
直前のコミットを削除
git reset --hard HEAD^
強制的に push
$ git push -f origin master
※複数人開発では,-f が難点.
Over write remote to local [4]
ローカルをリモートで強制的に上書きする.
$ git fetch origin master
$ git reset --hard origin/master
git pull from master into the development branch [5]
開発用ブランチ (dev) に master をマージする.
$ git checkout dev
$ git fetch origin
$ git merge origin/master
Changing https -> ssh
https で clone したリポジトリを ssh に変更する[3]
設定の確認.
$ git remote -v
$ git remote -v
origin git@github.com:admiswalker/SubStandardLibrary (fetch)
origin git@github.com:admiswalker/SubStandardLibrary (push)

url の変更.
$ git remote set-url origin git@github.com:admiswalker/SubStandardLibrary
$ git remote set-url origin git@github.com:admiswalker/SubStandardLibrary
$ git remote -v
origin git@github.com:admiswalker/SubStandardLibrary (fetch)
origin git@github.com:admiswalker/SubStandardLibrary (push)
References

0 件のコメント:

コメントを投稿