LoginSignup
1

More than 5 years have passed since last update.

Git メモ

Last updated at Posted at 2016-10-03

設定

鍵作成

作成前に有無確認

ls -la ~/.ssh/

鍵作成

ssh-keygen -t rsa
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/(username)/.ssh/id_rsa):id_github_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:

権限

chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa

公開鍵の設定

秘密鍵 id_dsa
公開鍵 id_dsa.pub

公開鍵をサーバーに転送

scp .ssh/id_rsa.pub user@server:

~/.ssh/configを作成しその中に

サーバー側作業

## 公開鍵のセット
cat id_rsa.pub >>  .ssh/authorized_keys
rm  id_rsa.pub

#パーミッションの設定
chmod 755 ~
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

権限情報まとめ

クライアント側の .ssh/id_rsa のパーミッション (600)
サーバー側のホームディレクトリのパーミッション (755)
サーバー側の .ssh ディレクトリのパーミッション (700)
サーバー側の .ssh/authorized_keys のパーミッション (600)

ssh config

Host github github.com
  HostName github.com
  IdentityFile ~/.ssh/id_git_rsa #ここに自分の鍵のファイル名
  User git
ssh -T github

sshの場合はこちら。。。

~/.gitconfig
[url "github:"]
    InsteadOf = https://github.com/
    InsteadOf = git@github.com:

Git config

git config --global user.name "your name"
git config --global user.email "your email address"
git config --global http.proxy "your proxy host if needed"
# 日本語文字化け対応
git config --global core.quotepath false
git config core.editor "your prefferd editor"

# 確認
git config --list

操作

ForkしてからPull Requestをするまでの流れ

チームで作業するときは
プロジェクトのレポジトリ(Upstream)から直接ブランチを切るのではなく、
フォークしてからブランチを切る
プッシュは自分のレポジトリに対して行い
プルリクエストをUpstreamに投げる

upstream:フォーク元、プロジェクトのレポジトリ
origin:フォーク先、自分のアカウントにフォークしてきたのレポジトリ
local:クローン先、自分のローカルにクローンしたレポジトリ

クローン

local:自分のローカルにレポジトリをクローンする

git clone <repository>

upstreamの設定

git remote add upstream <repository>
git branch -a
git fetch upstream
git pull upstream master

ブランチを切って作業する

git checkout -b ブランチ名

checkout直後の状態に戻す

git reset --hard

状態を確認する

git status

コミット

git add ファイル名
git commit -m <title>

コミットの取り消し

git commit --amend
git commit --amend -m <title>

プッシュ

origin:自分のアカウントにフォークしたレポジトリにプッシュする

git push origin <branch>

リバート

git revert <commit>

プッシュの取り消し

git log --oneline
git reset --hard コミット番号
git push -f origin <branch>

プルリクエストの作成方法

upstream:フォーク元のプロジェクトのレポジトリに対して
origin、自分のアカウントにフォークしたレポジトリから
プルリクエストを作成する

プル

# upstreamからのプル
git pull upstream master

# originからのプル
git pull origin master

スタッシュ

# 現在の作業保存
git stash

# ブランチ移動後作業を終えた後、元の作業ブランチに戻り、以下作業中の状況に戻れる
git stash apply

# stashのリストを見る
git stash list

コミットを取り消す3つのリセット

# ワーキングディレクトリはそのままでステージとコミットをリセット
git reset <commit>

# ワーキングディレクトリもステージもそのままでコミットだけリセット
git reset --soft <commit>

# ワーキングディレクトリ, ステージ, コミットの全てをリセット
git reset --hard <commit>

Rename branch

git branch -m <oldname> <newname>

自分の一週間分のコミットを確認

git log --pretty=format:"%h - %an, %ad (%ar) : %s" --since=1.weeks --author="Toshifumi Kurosawa"

空のプルリクエストを作る

# 空のコミット
git commit --allow-empty -m "<title>"

# リモートブランチに push
git push origin <branch>

# リモートブランチと紐付け
git branch -u origin/<branch>

# ローカルブランチがどのリモートブランチと紐付いているか確認
git branch -vv

alias

alias ga='git add'
alias gaa='git add .'
alias gaaa='git add -A'
alias gb='git branch'
alias gbd='git branch -d '
alias gc='git commit'
alias gcm='git commit -m'
alias gco='git checkout'
alias gcob='git checkout -b'
alias gcod='git checkout develop'
alias gcom='git checkout master'
alias gd='git diff'
alias gda='git diff HEAD'
alias gg='alias | grep git'
alias gi='git init'
alias gitkeep='find . -type d -empty -not -path "./.git/*" -exec touch {}/.gitkeep \;'
alias gitremove='find ./ -name .git | xargs sudo rm -rf'
alias gl='git log'
alias gld='git log --pretty=format:"%h %ad %s" --date=short --all'
alias glg='git log --graph --pretty=format:"%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset" --abbrev-commit --date=relative'
alias glga='git log --graph --all --pretty=format:"%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset" --abbrev-commit --date=relative'
alias glp='git log -p'
alias gm='git merge --no-ff'
alias gp='git pull'
alias gpom='git push origin master'
alias grh='git reset --hard'
alias gs='git status'
alias gss='git status -s'
alias gst='git stash'
alias gstd='git stash drop'
alias gstl='git stash list'
alias gstp='git stash pop'
alias podRepoRemoveMaster='git conf
5.コミットの順番を間違えちゃった! -- コミットログの並べ替えと削除
エディタが立ち上がるので並べ替えたり削除したりしましょう。
push前にログを整理するのによく使われます。
$ git rebase -i <commit>
6.コミット細かすぎィ! -- コミットログの統合と編集
まずはエディタを立ち上げます。
$ git rebase -i <commit>
親コミットに統合したい子コミットのpickをfixupに書き換えます。
コミットメッセージを編集したいコミットはpickをeditにします。
pick xxxxxx parent
fixup yyyyyy child
edit zzzzzz hoge
7.あのコミットさえあれば…! -- 他のブランチのコミットを適用する
リベースだと都合が悪いときに使います。
$ git cherry-pick <commit>
8.やばい!ハードリセットしたら消えちゃった! -- 過去の状態の復元
落ち着いてください。全ての操作は記録されています。
ログから親コミットを指定してハードリセットします。
$ git reflog
zzzzzz HEAD@{0}: reset: moving to <commit>
yyyyyy HEAD@{1}: foo
xxxxxx HEAD@{2}: hoge
$ git reset --hard HEAD@{1}
9.masterにマージ後にバグ発生!どうする!? -- コミットを打ち消すコミット
revertして修正後に再度マージしましょう。
revertはコミットログに残ります。
$ git revert <commit>

参考URL:
ForkしてからPull Requestをするまでの流れForkしてからPull Requestをするまでの流れ
ブランチの切り替えをしなくてよくなるコマンド git worktree がすごい! ブランチの切り替えをしなくてよくなるコマンド git worktree がすごい!
初心者から一歩抜け出すためのGitの業 〜 git rebase -i初心者から一歩抜け出すためのGitの業 〜 git rebase -i
初心者から一歩抜け出すためのGitの業 〜 git reflog初心者から一歩抜け出すためのGitの業 〜 git reflog

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1