LoginSignup
0
1

More than 5 years have passed since last update.

元CUI恐怖症でSourcetree愛用者のgitコマンドまとめ

Last updated at Posted at 2018-10-02

自分用のよく使うgitコマンドです。

git

git clone

git clone [リポジトリURL] [ディレクトリパス]
リポジトリをコピー

git init

ディレクトリにリポジトリを追加

git add

ファイルやディレクトリをインデックスに追加

git add [ファイル名]
ファイル名を指定して追加

git add -A
全ての変更を追加

git add .
新規作成もしくは変更されたファイルを追加

git commit

インデックスに追加されたファイルをコミット
esc i でinsertモード(編集モード)
問題なければ esc + :wq でコミット

git commit --allow-empty
アローコミット。空コミット。

git commit --amend
直前のコミットを書き換える。リモートにプッシュした場合は差異が生じるので、使わない。

git log

コミットログを参照

git log --grep [検索したい文字]
指定した文字がコミットログに含まれるコミットを表示

git diff

インデックスとワーキングツリーの差異を表示

git diff [比較したいブランチ名] --name-only
差分のファイル名だけ抽出

git status

前回のコミットと比較してどのファイルが変更されたかを表示

git branch

現在のブランチを確認

git branch -a
すべてのブランチを確認

git branch -r
リモートブランチを確認

git branch -d [ブランチ名]
(マージ済みの)ローカルブランチを削除

git branch -D [ブランチ名]
どんなローカルブランチでも削除

git branch -m [ブランチ名] [新しい名前のブランチ名]
ブランチの名前を変更

git fetch

リモートリポジトリの最新情報を追加

git checkout

git checkout [ブランチ名]
ブランチを作成(もしくは変更)

git checkout -b [ブランチ名]
ブランチを作成して変更

git checkout -b [ローカルブランチ] [origin/リモートブランチ]
リモートブランチからローカルブランチを作成

git merge

git merge [取り込むブランチ]
現在のブランチに指定したブランチをマージ

git pull

git pull origin [リモートブランチ]
git fetch も含む
リモートブランチをプル

git push

git push origin [リモートブランチ]
リモートブランチにプッシュ

git push --delete origin [リモートブランチ]
リモートブランチを削除

git stash

現在の状態を保存

git stash save "[メッセージ]"
メッセージ付きで現在の状態を保存

git stash list
保存した状態の一覧を表示

git stash pop
最新の保存状態を復元

git stash pop stash@{[numbar]}
番号を指定して保存状態を復元

git stash apply
保存状態をリストに残したまま最新の保存状態を復元

git stash apply stash@{[number]}
保存状態をリストに残したまま指定した番号の保存状態を復元

git stash clear
保存状態をすべて削除

git reset

git reset --soft HEAD^

直前のコミットを取り消し。
ワークディレクトリの内容はそのままでコミットだけを取り消したい場合に使用。

master から作業ブランチを切って作業。
master に作業ブランチをマージ。
master が更新されていて push および pull できない。

error: failed to push some refs to 'リポジトリ.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

エイリアス

git はエイリアスを設定すると便利。

優先度

3 オプションなし リポジトリ固有
2 --global  Gitユーザー固有
1 --system  マシン全体

設定コマンド

git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.cm commit
git config --global alias.df diff
git config --global alias.gr grep

設定確認方法

git config --global --list | grep ^alias\.

git svn(おまけ)

git svn clone

git svn clone --trunk=/ [リポジトリ]
cloneする(チェックアウト)

git svn fetch

更新する(git fetch)

git svn rebase

最新コミットを取り込む(git pull)

git svn dcommit

リモートに送る(git push)


参考

基本的なGitコマンドまとめ

よく使うgitコマンドのエイリアスを設定して開発効率をアップする

gitで便利なエイリアス達

Markdown記法 チートシート

0
1
0

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
0
1