LoginSignup
21
35

More than 5 years have passed since last update.

Gitコマンド一覧

Last updated at Posted at 2014-08-22

git init

git initコマンドで、Gitリポジトリを新規に作成して、初期化することができます。初期による実行の場合、.gitというフォルダが作成されます。

git config

git configコマンドで、Gitリポジトリの設定を行うことができます。 git configのみの場合、オプションを見ることができます。

$ git config --global user.name 名前

Gitで利用する名前を設定できます。

$ git config --global user.email メールアドレス

Gitで利用するメールアドレスを設定できます。

$ git config --global --list

設定した内容を表示することができます。

$ git config --global --unset user.name

設定した全てのGitの名前を削除することができます。末尾に名前を指定することもできます。

$ git config --global alias.ショートカット名 'コマンド名'

aliasでショートカット名を作成することができます。

$git config --global alias.test '!echo test'

''の先頭に!をつけることで、Gitのコマンドだけではなく、外部のコマンドもショートカット名として、作成することができます。

$ vi ~/.gitconfig

git configで作成した箇所を確認することができます。

git clone (URL)フォルダ名.git (URL)

git cloneコマンドで、Gitリポジトリのコピーを作成する事ができます。

git add

git addコマンドで、インデックスというファイル情報を一時的に保存しておく場所に追加されます。

$ git add .

現在表示している場所の中身を全て追加

$ git add ファイル名またはフォルダ名

現在表示している場所からファイルまたはフォルダを追加します。

git status

git statusコマンドは、コミットするとどのように変更されるのかをファイルやフォルダ名で表示されます。

git commit

git commitコマンドで、インデックスに入っているデータを元にコミットすることができます。--amendオプションで、直前のコミットをやり直すことができます。

$ git commit(git commit -m "メッセージ"にすると一度にコミットできます。)

 ←ここにメッセージを入力(例:test)
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Committer: sample <user@USER-no-MacBook-Pro.local>
#
# On branch test
# Changes to be committed:
#   modified:   result.html
[test a056fe1] test
 Committer: sample <user@USER-no-MacBook-Pro.local>
Your name and email address were configured automatically   based
on your username and hostname. Please check that they are   accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+), 1 deletion(-)

$ git log
commit a056fe1612936f50be95391e84c79c0ff9b3148a
Author: test <user@USER-no-MacBook-Pro.local>
Date:   Fri Sep 5 11:17:05 2014 +0900

    test
$ git add .
$ git rm sample.txt 
$ git commit --amend

test←メッセージの変更もできます。(test←test2)

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Committer: test <user@USER-no-MacBook-Pro.local>
#
# On branch test
# Changes to be committed:
#   new file:   test.txt
#
# Changes not staged for commit:
#   modified:   test.txt
$ git log
commit 2172ddd7695365ae1a346b863d09f66a650fef69
Author: test <user@USER-no-MacBook-Pro.local>
Date:   Fri Sep 5 10:31:55 2014 +0900

    test2

git commit -av

一度インデックスに入れたファイルを自動で検出し更新します。その結果をエディタ上で確認したあと、コミットすることができます。

git submodule add カレントディレクトリの絶対パス フォルダ名

Gitリポジトリの中にGitリポジトリを入れることができます。git submoduleコマンドで、サブモジュールを確認することができます。

$ git submodule init

サブモジュールを初期化することができます。cloneで、作成した時に必要です。

$ git submodule update

サブモジュールを更新することができます。cloneで、作成した時に必要です。

git log

コミットで行った時のログが表示される。logの場合、コミット時のメッセージとGitをした名前とメールアドレスが表示されます。また、コミットをしたidも表示されます。shortlogの場合、コミット時のメッセージとGitをした名前が表示されます。

$ git log --oneline
aef8adb a
41918d2 20140823
b2754ce 20140822
$ git log -1
aef8adb a

最新のコミットのログを表示することができます。

$ git log --since="6 hours" 
aef8adb a
41918d2 20140823

過去6時間のコミットを表示することができます。

$ git log --name-status --oneline
581851a test5
D       test3.txt
bd62449 test4
A       test3.txt
6755fc1 test3
M       test2.txt
22403b3 test2
M       test2.txt
275cb96 test
A       test.txt
A       test2.txt

コミットした時に変更があったファイルを表示します。Dが削除。Aが新規。Mが変更です。

$ git log --grep="test2"

ログメッセージの文字を検索することができます。

git show コミットid

コミットの差分やファイルの内容を表示することができます。

git blame ファイル名

ファイルが最後に編集された履歴を表示することができます。

$ git blame test.txt
^b9e18d0 (test 2014-09-11 10:51:06 +0900 1) test2

ファイルの内容とコミットidを確認することができます。

$ git blame -M test.txt
^b9e18d0 (test 2014-09-11 10:51:06 +0900 1) test2

ファイル内でのコピーペーストおよび行の移動も含めて、ファイルの内容とコミットidを確認することができます。

$ git blame -L 3,5 index.html
58b07eef 3) <head>
58b07eef 4) <meta http-equiv="Last-Modified" content="Tue, 12   Aug 2014 14:39:39 GMT">
58b07eef 5) <meta charset="UTF-8">

3行目から5行目までのファイルの内容を表示することができます。5を+3にしても同じ表示をすることができます。

git diff

インデックスに未登録の内容や、次のコミットで反映される内容を表示します。

$ git add 変更したファイル名または変更したフォルダ名
$ git diff --cached

変更した内容が表示されます。

$ git diff コミットid コミットid

コミットした内容から変更された内容が表示される

$ git diff --stat コミットid(起点) コミットid(終点)

コミットの起点から終点までの変更があったファイルを表示することができます。

git rm ファイル名またはフォルダ名

git rmコマンドで、インデックスの中からファイルまたはフォルダを削除することができます。

git revert コミットidまたは':/コミットメッセージ'

git revertコマンドで、コミットした内容を取り消すコミットを自動で作成します。
エディターが表示されるので、コミットメッセージをつけることができます。

$ git log --oneline
14b565d test3
6b2d940 test
$ git revert ':/test3'
Revert "test3"←コミットメッセージを変更することができます。

This reverts commit 14b565d493951c7bedb78ec85dbbbaa26772380f.

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Committer: test <user@USER-no-MacBook-Pro.local>
#
# On branch master
# Changes to be committed:
#   modified:   test3.txt
#
$ git log --oneline
4eb4025 Revert "test3"
14b565d test3
6b2d940 test

git grep 検索名

git grepコマンドで、ファイルの中の文字を検索することができます。

git branch

git branchコマンドで、ブランチ名を表示することができます。ブランチ名をつけることで、ブランチを作成することができます。また、ブランチ名のあとに別のブランチ名、コミット名、タグ名をつけることで、その時点を起点にしたブランチ名を作成することもできます。

branch:履歴の流れを分岐して記録させること

$ git branch
* master
$ git branch test
$ git branch
* master
  test

*印は現在のブランチです。

$ git branch -r
  test/master
  test/test

リモートのブランチだけを表示することができます。

$ git branch -a
* master
  test
  remotes/test/master
  remotes/test/test

ローカルとリモートのブランチをすべて表示することができます。

$ git branch test2 test

testブランチと同じ起点をtest2ブランチに作成します。

$ git branch -m test test3

mオプションで、ブランチ名を変更することができます。

$ git branch --merged

mergedオプションで、現在表示しているブランチからマージ済みのブランチ一覧を表示することができます。

$ git branch -d test
error: The branch 'test' is not fully merged.
If you are sure you want to delete it, run 'git branch -D test'.
$ git branch -D test
Deleted branch test (was f4b2259).

dオプションで削除することができますが、削除したいブランチ名が、masterにマージしていない場合、dオプションで削除することはできません。その場合、Dオプションを使うと削除することができます。

git checkout ブランチ名

git checkoutコマンドで、ブランチを切り替えることができます。bオプションで、ブランチを追加して、チェックアウトすることができます。
コミットをした後、git branchコマンドでブランチを作成して、ファイルを「管理外」にした後から、管理下に同名のファイルが残っていた場合に、コミットを行うとcheckoutができなくなることがあります。

$ git checkout test
$ git branch
  master
* test
$ git init
$ echo test > test.txt
$ git add .
$ git commit -m "test"
$ git branch test2
$ git rm test.txt
$ echo test2 > test.txt
$ git commit -m "test2"
$ git checkout test2
error: The following untracked working tree files would be overwritten by checkout:
    test.txt
Please move or remove them before you can switch branches.
Aborting

git checkout HEAD ファイル名またはフォルダ名

HEADオプションで、git rmで削除したファイルやフォルダをHEADの状態に戻すことができます。git checkout -fで、rmで削除した全ファイルをもとに戻すことができます。

$ vi test.txt
<<<<<<< HEAD
test2
=======
test
>>>>>>> test
$ git checkout --ours test.txt
test2
$ git checkout --theirs test.txt
test

競合があった時に、oursオプション、theirsオプションで、選択して競合を解消することができます。

git merge ブランチ名

git mergeコマンドで、ファイルを併合することができます。

git rebase ブランチ名

分岐点の変更

git rebaseコマンドで、指定したブランチの最新コミットに対して別のコミットに移動することができます。競合があった場合、コミットではなく、--continueオプションで再度行うことができます。

$ git rebase master test

testブランチの分岐元をmasterブランチの最新のコミットにする事ができます。

$ git rebase --onto master test test2

元が、test2ブランチは、testブランチに含まれていましたが、--ontoオプションで、test2ブランチは、testブランチの変更を含まずにmasterブランチから直接分岐させることができます。

コミット履歴の編集

-iオプションで、エディタを開くことができます。
その中で、pick,reword,edit,squash,fixupを用いることで、コミットの削除、分割、改変を行って履歴を整理することができます。
git commit --amendコマンドの強化版のようなものです。
git rebase -i HEAD~数字またはgit rebase -i コミットidで実行します。

pickの場合

コミット時のまま実行することができます。

$ git log --oneline
13bbb5c test5
8216222 test4
9554393 test3
1bdd1b1 test2
d6d263d test
$ git rebase -i 1bdd1b1(この後エディタが開きます)
pick 9554393 test3
pick 8216222 test4
pick 13bbb5c test5

 Rebase 1bdd1b1..13bbb5c onto 1bdd1b1

 Commands:
 p, pick = use commit
 r, reword = use commit, but edit the commit message
 e, edit = use commit, but stop for amending
 s, squash = use commit, but meld into previous commit
 f, fixup = like "squash", but discard this commit's log message
 x, exec = run command (the rest of the line) using shell

 These lines can be re-ordered; they are executed from top to bottom.

 If you remove a line here THAT COMMIT WILL BE LOST.

 However, if you remove everything, the rebase will be aborted.

 Note that empty commits are commented out

コミットを削除する場合

pickから始まる行を削除します。この時コミットを削除したいものの一つ前と一つ後の作業が、テキスト修正のみだと削除したいもの以降もコミットが削除されたり、マージしたりとおかしくなります。その場合、git rebase --abortで取り消すことができます。削除した場合、削除したコミットデータの後のコミットIDが変更されます。

$ git log --oneline
13bbb5c test5
8216222 test4
9554393 test3
1bdd1b1 test2
d6d263d test
$ git rebase -i 1bdd1b1(この後エディタが開きます)
pick 9554393 test3←削除
pick 8216222 test4
pick 13bbb5c test5

 Rebase 1bdd1b1..13bbb5c onto 1bdd1b1

 Commands:
 p, pick = use commit
 r, reword = use commit, but edit the commit message
 e, edit = use commit, but stop for amending
 s, squash = use commit, but meld into previous commit
 f, fixup = like "squash", but discard this commit's log message
 x, exec = run command (the rest of the line) using shell

 These lines can be re-ordered; they are executed from top to bottom.

 If you remove a line here THAT COMMIT WILL BE LOST.

 However, if you remove everything, the rebase will be aborted.

 Note that empty commits are commented out
$ git log --oneline
3157297 test5
a3b4a10 test4
1bdd1b1 test2
d6d263d test

コミットを入れ替える場合

pickから始まる行を入れ替えます。この時コミットを入れ替えたいものの一つ前と一つ後の作業が、テキスト修正のみだと入れ替えたいもの以降もコミットが削除されたり、マージしたりとおかしくなります。その場合、git rebase --abortで取り消すことができます。入れ替えた場合、入れ替えたコミットデータとその後のコミットIDが変更されます。

$ git log --oneline
13bbb5c test5
8216222 test4
9554393 test3
1bdd1b1 test2
d6d263d test
$ git rebase -i 1bdd1b1(この後エディタが開きます)
pick 9554393 test3←入れ替え
pick 8216222 test4←入れ替え
pick 13bbb5c test5

 Rebase 1bdd1b1..13bbb5c onto 1bdd1b1

 Commands:
 p, pick = use commit
 r, reword = use commit, but edit the commit message
 e, edit = use commit, but stop for amending
 s, squash = use commit, but meld into previous commit
 f, fixup = like "squash", but discard this commit's log message
 x, exec = run command (the rest of the line) using shell

 These lines can be re-ordered; they are executed from top to bottom.

 If you remove a line here THAT COMMIT WILL BE LOST.

 However, if you remove everything, the rebase will be aborted.

 Note that empty commits are commented out
$ git log --oneline
4e0c0af test5
bd64c6e test3
1100677 test4
1bdd1b1 test2
d6d263d test

rewordの場合

コミットメッセージを編集します。このときコミットのメッセージも変更します。その後再度同じメッセージを入力します。

$ git log --oneline
13bbb5c test5
8216222 test4
9554393 test3
1bdd1b1 test2
d6d263d test
$ git rebase -i 1bdd1b1(この後エディタが開きます)
pick 9554393 test3
pick 8216222 test4
pick 13bbb5c test5←reword 13bbb5c 5に編集

 Rebase 1bdd1b1..13bbb5c onto 1bdd1b1

 Commands:
 p, pick = use commit
 r, reword = use commit, but edit the commit message
 e, edit = use commit, but stop for amending
 s, squash = use commit, but meld into previous commit
 f, fixup = like "squash", but discard this commit's log message
 x, exec = run command (the rest of the line) using shell

 These lines can be re-ordered; they are executed from top to bottom.

 If you remove a line here THAT COMMIT WILL BE LOST.

 However, if you remove everything, the rebase will be aborted.

 Note that empty commits are commented out

上書き保存して閉じた後

test5←5に編集

Please enter the commit message for your changes. Lines starting
with '#' will be ignored, and an empty message aborts the commit.

Committer: USER <user@USER-no-MacBook-Pro.local>

rebase in progress; onto 1bdd1b1
You are currently editing a commit while rebasing branch 'master' on '1bdd1b1'.

また別のエディタが開かれるので、さきほど入力したメッセージと同じメッセージで上書き保存して閉じます。その後git logコマンドで確認します。

$ git log --oneline
*43a5498 5
2265020 test4
28f202b test3
1bdd1b1 test2
d6d263d test
*:コミットidが13bbb5cから43a5498になり、メッセージがtest5から5に変わりました。

editの場合

コミットを編集します。

$ git log --oneline
43a5498 5
8216222 test4
9554393 test3
1bdd1b1 test2
d6d263d test
$ git rebase -i 1bdd1b1(この後エディタが開きます)
pick 9554393 test3
pick 8216222 test4
pick 43a5498 5←edit 43a5498 5に変更

# Rebase 1bdd1b1..13bbb5c onto 1bdd1b1
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

保存して閉じた後

Stopped at 43a549818b319f4e7ac9ff1db8bbd0f269e78ca2... 5
You can amend the commit now, with

git commit --amend

Once you are satisfied with your changes, run

git rebase --continue

メッセージを修正で、停止しています。

$ git commit --amend
[detached HEAD dfec3e3] test5
 Committer: USER <user@USER-no-MacBook-Pro.local>
Your name and email address were configured automatically   based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 deletion(-)

git commit --amendで、コミットを修正することができます。

$ git rebase --continue
Successfully rebased and updated refs/heads/master

git rebase --continueで、修正したコミットを行います。

squashの場合

前のコミットにマージさせることができます。

$ git log --oneline
43a5498 test5
8216222 test4
9554393 test3
1bdd1b1 test2
d6d263d test
$ git rebase -i 1bdd1b1(この後エディタが開きます)
pick 9554393 test3
pick 8216222 test4
pick 43a5498 test5←squash 43a5498 test5に変更

# Rebase 1bdd1b1..13bbb5c onto 1bdd1b1
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

上書き保存して閉じた後

# This is a combination of 2 commits.
# The first commit's message is:

test4

# This is the 2nd commit message:

test5

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Committer: USER <user@USER-no-MacBook-Pro.local>
#
# rebase in progress; onto 1bdd1b1
# You are currently editing a commit while rebasing branch 'master' on '1bdd1b1'.
#
# Changes to be committed:
#   modified:   a.txt
#

git logコマンドで確認します。

$ git log --oneline
87deb4b test4
9554393 test3
1bdd1b1 test2
d6d263d test
$ git log
commit 87deb4bd51081ba1b139ee24dc23473d11be8103
Author: USER <user@USER-no-MacBook-Pro.local>
Date:   Wed Aug 27 15:00:02 2014 +0900

    test4

    test5

fixupの場合

前のコミットにマージさせ、コミットメッセージが破棄されます。

$ git log --oneline
87deb4b test4
9554393 test3
1bdd1b1 test2
d6d263d test
$ git rebase -i 1bdd1b1(この後エディタが開きます)
pick 9554393 test3
pick 8216222 test4←finxup 8216222 test4に変更

# Rebase 1bdd1b1..13bbb5c onto 1bdd1b1
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

上書き保存して閉じた後、git logで確認します。

$ git log
commit 22e1a8b7188bcd396cf874e7580c26af7a28a00e
Author: USER <user@USER-no-MacBook-Pro.local>
Date:   Wed Aug 27 14:59:51 2014 +0900

    test3

git reflog

gitコマンドの全ての操作ログを表示させることができます。reflogで閲覧できる履歴情報は、デフォルト設定で30日間保存されます。git gcコマンドを実行すると履歴情報が削除されます。show ブランチ名で、ブランチ別に表示することもできます。

$ git reflog
830053b HEAD@{0}: commit: test4
c4d12f3 HEAD@{1}: commit: test3
7b7206a HEAD@{2}: checkout: moving from master to test
7b7206a HEAD@{3}: commit: test
f418956 HEAD@{4}: commit: test2
$ git reflog show master
7b7206a master@{0}: commit: test
f418956 master@{1}: commit: test2
$ git reflog show test
830053b test@{0}: commit: test4
c4d12f3 test@{1}: commit: test3
7b7206a test@{2}: branch: Created from HEAD

git reset

git resetコマンドで、間違ってインデックスへ登録した時やコミットした時、インデックス、ブランチを特定の状態まで戻すことができます。

$ git reset --hard HEAD^(HEADは小文字でも可能です。)

コミットを1つ前に戻すことができます。2つ以上の場合も、繰り返し操作できます。HEAD^の^を取って実行すると、最新のコミットの状態に作業フォルダを戻すことができます。

$ git reset --mixed HEAD^

作業フォルダはそのままの状態で、コミットを一つ前に戻すことができます。

$ git reset --hard ORIG_HEAD(または消したコミットid)

取り消しを行ったコミットを戻すことができます。消したコミットidは、cat .git/
ORIG_HEADで取得することができます。

$ git reflog
43102a6 HEAD@{0}: reset: moving to ORIG_HEAD
e7d9c14 HEAD@{1}: reset: moving to ORIG_HEAD
e7d9c14 HEAD@{2}: rebase -i (finish): returning to refs/heads/master
e7d9c14 HEAD@{3}: rebase -i (pick): test
$ git reset HEAD@{1}

rebase後の状態まで戻します。

git stash

git stashコマンドで、コミットした状態から作業を一時保存することができます。

$ git stash
Saved working directory and index state WIP on master: 6b2d940 test
HEAD is now at 6b2d940 test
$ git stash list
stashのリストを表示することができます。
stash@{0}: WIP on master: 6b2d940 test
$ git stash show stash@{0} -p
stashの内容を確認することができます。
diff --git a/test3.txt b/test3.txt
index 39bd688..ae319df 100644
--- a/test3.txt
+++ b/test3.txt
@@ -1,2 +1,3 @@
 test
-test2
\ No newline at end of file
+test2
+tes    
\ No newline at end of file
$ git stash pop
一時保存した変更内容を取りすことができます。
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   test3.txt

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0}(82e0decfbe1d4f8fc86f160c4fca375f47528a8c)

git tag タグ名 コミットid

git tagコマンドで、コミットを行って印をつけた部分を確認することができます。タグ名とコミットidをつけることで、印をつけることができます。

$ git show タグ名

タグをつけたコミットを確認することができます。

$ git tag -d タグ名

タグを削除することができます。

git remote

git remoteコマンドで、リモートリポジトリの一覧を見ることができます。addオプションで、リモートリポジトリを追加することができます。複数追加することもできます。rmオプションで削除することもできます。showオプションでリモートリポジトリの情報を見ることもできます。

$ git remote
$ git remote add test /Users/user/www/test
$ git remote show test
* remote test
  Fetch URL: /Users/user/www/test
  Push  URL: /Users/user/www/test
  HEAD branch: test
  Remote branches:
    master new (next fetch will store in remotes/test)
    test   new (next fetch will store in remotes/test)
  Local refs configured for 'git push':
    master pushes to master (up to date)
    test   pushes to test   (up to date)
$ git remote
test
$ git remote rm test

git fetch リモートのブランチ名

git fetchコマンドで、リモートリポジトリの最新の履歴の取得だけを行うことができます。それにより、mergeで統合する前に確認することができます。リモートリポジトリからローカルリポジトリにブランチをインポートします。

$ git fetch test
From /Users/user/www/test
 * [new branch]      master     -> test/master
 * [new branch]      test       -> test/test

git pull リモートのブランチ名 ローカルのブランチ名

git pullコマンドで、リモートリポジトリの履歴を取得することができます。git fetchコマンドとgit mergeコマンドをまとめたものです。

$ git pull test test
From /Users/user/www/test
 * branch            test       -> FETCH_HEAD
 * [new branch]      test       -> test/test
Already up-to-date.
$ git branch -a
  master
* test
  remotes/test/test
$ git pull test
From /Users/user/www/test
 * [new branch]      master     -> test/master
You asked to pull from the remote 'test', but did not specify
a branch. Because this is not the default configured remote
for your current branch, you must specify a branch on the command line.

git push リモートのブランチ名

git pushコマンドで、ローカルリポジトリからリモートリポジトリにブランチをエクスポートします。

$ git push test
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes,    use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Everything up-to-date
$ git branch -a
master
* test
  remotes/test/master
  remotes/test/test

git cherry-pick

git cherry-pickコマンドで、別のブランチからコミットの情報をコピーすることができます。

$ git cherry-pick コミットid

コミットをコピーすることができます。

$ git cherry-pick コミットid コミットid

2つのコミットをコピーすることができます。

$ git cherry-pick 一つ前のコミットid 終点のコミットid

複数のコミットをコピーすることができます。

$ git cherry-pick --abort

競合解決中にcherry-pickを中止し、cherry-pick開始時まで戻ります。


ブランチ作成後に役に立つコマンド

$ git checkout - 

以前作業していたbranchにチェックアウトすることができます。

$ git merge - 

以前作業していたbranchをマージすることができます。

$ git checkout test/master

remote上にある特定のブランチをチェックアウトすることができます。

Gitの裏技

自動補完

自動補完をするにあたって、以下のファイルの設定を行います。
https://github.com/git/git/tree/master/contrib/completion
にgit-completion.bashというファイルがあるので、ダウンロードしてください。
そこから.bashrcファイルが置いてある場所と同じ場所にコピーします。
.bashrcファイルの最終行に

source ~/.git-completion.bash

を追加します。これで、設定は完了です。

$ git co tabキー
commit                    -- record changes to repository
commit-tree               -- create new commit object
config                    -- get and set repository or global   options
count-objects             -- count unpacked objects and display their disk consumption

gitのあとに使うコマンドを一部入力した後、tabキーを入力するとコマンドを表示することができます。tabキーを繰り返すと、表示されている順にコマンドが自動入力されます。

$ git commit -m tabキー
-a                        -- stage all modified and deleted paths
-e                        -- edit the commit message before committing
-i                        -- update the given files and commit the whole index
-n                        -- do not look for suspicious lines the commit introduces
-o                        -- commit only the given files
-q                        -- suppress commit summary message
-s                        -- add Signed-off-by line at the end of the commit message
-u                        -- show files in untracked directories
-v                        -- show unified diff of all file changes
-z                        -- separate dry run entry output with NUL

git co tabキーと同様にオプションでもtabキーを入力するとオプションを表示することができます。

21
35
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
21
35