Skip to content

Instantly share code, notes, and snippets.

@externvoid
Last active July 10, 2018 09:08
Show Gist options
  • Select an option

  • Save externvoid/299862f706e1950627fc7bd003d84ac8 to your computer and use it in GitHub Desktop.

Select an option

Save externvoid/299862f706e1950627fc7bd003d84ac8 to your computer and use it in GitHub Desktop.
gitコマンド

ブランチの操作

現在存在するブランチの確認

git branch
* master

新規にブランチ作成

git checkout -b feature-A

これは

git branch feature-A
git checkout feature-A

と同じ。 checkin = 手続き後入室 checkout = 手続き後退出

するとこうなる

git branch
* feature-A
master

新しいブランチ(feature-A)でコミット

git add README.md
git commit -m "Add feature-A"

masterブランチへ戻る

git checkout master
ls

lsしてもREADME.mdは無い。feature-Aブランチじゃ、無いから。 feature-Aブランチで再度作業。

git checkout -

ハイフンを引数に用いると直前のブランチへ移動。

統合ブランチ

master
      \
       feature-A
       
     * -----------|-master
      \          /
       feature-A

上の状態から、下の状態への移行がブランチの統合、あるいはブランチのマージと言う。

git checkout master
git merge --no-ff feature-A

--no-ffオプションが無ければ

git merge feature-A
     *
      \          
       feature-A, master

feature-Aブランチがmasterブランチへと改名される。(fast-forwardな関係であるため)

fast-forwardな関係

X - A 
     \
      Y - B
      
X - Y - A 
     \
      Z - B

下のコミットグラフ上のA, Bはff関係が無い 図で判るgit-merge

↔️AとBは、通過地点と終点の関係

ブランチを視覚的に確認

git log --graph

コミットを変更

> git log --graph
* commit 526e873aa4bd02100200ddccac1bb46ac9f1d764
| Author: externvoid <[email protected]>
| Date:   Fri Dec 2 16:53:12 2016 +0900
| 
|     scH is retrieved from TestDraw View bound size height
| 
* commit 0af6b8a292a5ecb2fafe2059870231183ae5909c
| Author: externvoid <[email protected]>
| Date:   Fri Dec 2 14:49:19 2016 +0900
| 
|     enable 30 candles
| 
* commit fb57722bc5ff4e0884868ac455a7368c5b8ef588
| Author: externvoid <[email protected]>
| Date:   Fri Dec 2 09:22:39 2016 +0900
| 
|     initial Swift3
|     CGMutablePath object related methods must be corrected manually.
| 
* commit 6bdc3c7701b83da0de3c9749b45d01073aff5c1f
  Author: externvoid <[email protected]>
  Date:   Fri Dec 2 08:56:27 2016 +0900
  
      swift2.x code

コミットログをさかのぼってブランチを分岐する事ができる。 リポジトリの

  1. HEAD
  2. ステージ
  3. ワーキングツリー

を指定の状態へ戻すには、

git reset --hard 6bdc3c7701b83da0de3c9749b45d01073aff5c1f

ここで、fix-Bブランチを作ってコミットする事ができる。

git checkout -b fix-B

git-resetする前の状態へ戻るには、git-logが使えない。現在の状態から前の状態のみがgit-logで表示される。 そこで、

git reflog

このコマンドで、全ての作業ログを確認できる。ただし、GCにより数日間で古い作業ログは無くなる。

コンフリクトを解消

git-mergeで発生するコンフリクトは、コンフリクト情報を参照しながらファイルの編集を行う。 コンフリクトを解消した旨のコメントを残すために、コミットログを修正できる。

git commit --amend "fix conflict"

些細な修正、typoを発見し新しくcommitするまでも無い

ような時、歴史の改変ができる。

git rebase -i

i = interactive

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment