Skip to content

Instantly share code, notes, and snippets.

@callmekohei
Last active January 2, 2018 03:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save callmekohei/aa97b7d721f6b6760cdc6b1119e99408 to your computer and use it in GitHub Desktop.
Save callmekohei/aa97b7d721f6b6760cdc6b1119e99408 to your computer and use it in GitHub Desktop.
個人的なgitのおぼえがき

用語

Working Directory : ファイルとかフォルダとか
Staging Area      : repositoryにcommitする際の指示書みたいなもの
Repository        : ファイル・フォルダの記録

https://git-scm.com/book/en/v2/Getting-Started-Git-Basics#The-Three-States


準備

コマンドをインストール

$ brew install git
$ brew install hub
$ brew install gibo

SSH-Keyの設定(Githubとの連携のときに必要)

Generating a new SSH key and adding it to the ssh-agent

https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/

Adding a new SSH key to your GitHub account

https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/


.bash_profile に設定しておくと便利

hub

hubコマンドをgitコマンドとしてあつかう

eval "$(hub alias -s)"
gittree

みやすい git log が表示される

alias gittree='git log --oneline --graph --all --format="%x09 %Cred%h %Cgreen%cd %Cblue%s %d"'
completion

tab を2回押すと補完される

source /usr/local/etc/bash_completion.d/git-completion.bash

その他

.gitignoreのテンプレート

$ gibo macOS vim > .gitignore

.bashrcに書くと楽な設定

コマンドプロンプトに現在のブランチを表示する

# http://qiita.com/caad1229/items/6d71d84933c8a87af0c4
function parse_git_branch {
    git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ [\1]/'
}

function promps {
    local BLUE='\[\033[01;34m\]'
    local RED='\[\033[01;31m\]'
    local GREEN='\[\033[01;32m\]'
    local WHITE='\[\033[0;00m\]'
    local GRAY='\[\033[01;37m\]'
    PS1="${BLUE}\W${GREEN}\$(parse_git_branch)${BLUE}\$${WHITE} "
}

PROMPT_COMMAND='promps'

stage and unstage

// stage Area にのせる
$ git add

// stage Area に部分的にのせる
$ git add -p

// stage Area からはずす
$ git reset

commit and reset(chekout)

commitしたあとで、特定のファイルをcommitしたくなかった場合は いったんresetしてもう一度commitしなおす

$ git commit

// コミットの取り消し(直前のコミットをなかった事にする)
$ git reset head~

// コミットの打ち消し(履歴はのこる)
$ git revert {hash}

// コミットの上書き(コメントの修正に便利!)
$ git commit --amend

特定のファイルを特定のコミットの状態に戻したい場合

$ git checkout {hash} {filepath}

編集中の特定のファイルをheadの状態に戻したい場合 コードをかいてて、元に戻したいとき

$ git checkout {filepath}

コミットの履歴をみる

$ git log --oneline

コミットコメントを変更する

// 直前のコミットコメントのみ
$ git commit --amend

// 任意のコミットコメントを変更できる!
$ git rebase -i / reword

コミットをまとめる

// 直前のコミットのみ
$ git commit --amend

// 細かくまとめる
$ git rebase -i head~3 / pickup

コミットしているファイル名を変更する

$ git mv fileA fileB
$ git commit

コミットしてるファイルを削除

$ git rm fileA
$ git rm —cached fileA // fileAはのこす

操作履歴からのhead移動

$ git reflog
$ git reset --hard

ブランチをきる

$ git branch -b featureA

ブランチをmasterにとりこむ

$ git checkout master
$ git merge featureA

ブランチをけす

$ git branch
$ git branch -d featureA

リモートブランチを消す

$ git branch -r  // リモートブランチの確認
$ git push --delete origin featuerA

リモートブランチを消す

git fetch --prune ( git fetch -p )

リモートリポジトリとの連携

origin とは?

リモートリポジトリの名称

clone(remote add)

// remote ->  local
$ git clone path
or
$ git remote add origin path

push

$ git push origin master // $ git push

pull ( fetch + merge )

$ git pull origin master // $ git pull

// 差異を確認してマージする(慎重派)
$ git fetch origin
$ git diff
$ git merge origin/master

ローカルブランチとリモートブランチの紐付け

// remote <-  local
$ git push -u origin feature-A

// remote ->  local
$ git checkout -t origin/feature-A

// remote <-> local
$ git branch -u origin/feature-A

tracking の確認

$ branch -vv

リモートブランチの作成と削除

$ git push origin feature-A

$ git push --delete origin feature-A

Githubとの連携

アップロード

// ローカルレポジトリをGithubにアップロード
$ git create
$ git push -u origin master

// wineブランチを作成してアップロード
$ git checkout -b 'wine'
$ git push -u origin wine

プルリク

// プルリク
$ git pull-request -b master featureD

// マージ
$ git checkout master
$ git merge master wine
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment