18
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[やりたい事別]個人的に使って役立ったGitコマンドまとめ(2014/05/14版)

Last updated at Posted at 2014-05-14

使って覚えたGitコマンドをメモしていきます。

commitが消えてしまったcommit状態に戻す

①commit履歴と状態確認 (git log)

$ git log
commit 9f785932913bc1d9a054e269ca82ff14fa31edd4
Author: sugawara ryousuke <hogehoge@gmail.com>
Date:   Wed May 14 15:35:35 2014 +0900

    ページ表示まで

commit 6d245ae928f80f15cc41435d35eed8acd6244585
Author: sugawara ryousuke <hogehoge@gmail.com>
Date:   Wed May 14 14:21:08 2014 +0900

    app.jsだけの起動確認 DBとの接続完了
.
.
.

commit状態を確認しましたが、何らかの要因で直前のcommitなどが無いです汗
ココで言うと、9f78593よりも最新commitがあるのに表示されていません。

②作業履歴を確認する (git reflog)

$ git reflog
9f78593 HEAD@{0}: checkout: moving from dev-n0bisuke-2 to dev-n0bisuke
f5b4843 HEAD@{1}: checkout: moving from 000e2bc49e795dab3be3214ccc5a6a32cb504efb to dev-n0bisuke-2
000e2bc HEAD@{2}: commit: readAPI&unreadAPI復旧
b2dd355 HEAD@{3}: commit: moreAPI復活
d1620c6 HEAD@{4}: commit: get/mesまで復元
9f78593 HEAD@{5}: checkout: moving from dev-n0bisuke to 9f785932913bc1d9a054e269ca82ff14fa31edd4
9f78593 HEAD@{6}: reset: moving to HEAD^1
9a8a90f HEAD@{7}: commit: 修正
9f78593 HEAD@{8}: commit: ページ表示まで
6d245ae HEAD@{9}: commit: app.jsだけの起動確認 DBとの接続完了

9f78593のcommitよりも最新の作業が見つかりました!

③特定作業の段階に復元する (git cherry-pick コミットid)

$ git cherry-pick 000e2bc

これで 000e2bc HEAD@{2}: commit: readAPI&unreadAPI復旧のコミットまで復元することができます
※git resetでも良いかも

特定commit時点の特定ファイルを確認する

commit1つ前の時点のファイルを閲覧する (git show HEAD^:ファイルパス)

$ git show HEAD^:path/hoge/app.js

commitログ000e2bc時点のファイルを閲覧する

$ git show 000e2bc:path/hoge/app.js

commitログ000e2bc時点のファイルを現在のファイルに書き込む

$ git show 000e2bc:path/hoge/app.js >> path/hoge/app.js

現時点のファイルの記述内容に追記されます。手動マージしましょう。

コミット状態をリセットする

--hardオプションを付ける事でcommitログと状態を戻せます。

1つ前のcommitの状態に戻す

$ git reset --hard HEAD^

2つ前のcommitの状態に戻す

$ git reset --hard HEAD^^

--softオプションを付ける事でcommitログを戻せます。

ファイルの状態はそのままで、コミットid000e2bcのcommitログに戻す

$ git reset --soft 000e2bc

不要になったブランチの削除

ブランチを切ったりマージしたりとブランチ操作していくと使わなくなるブランチが出てきます。不要なブランチは削除して整理しておきましょう。

①ローカル&リモートのブランチ確認 (git branch -a)

$ git branch -a
  dev-n0bisuke
* master
  stash
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/dev-n0bisuke

②ローカルブランチ削除 (git branch -d ブランチ名)

$ git branch -d dev-n0bisuke

一応確認
$ git branch -a
* master
  stash
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/dev-n0bisuke

③リモート(origin)ブランチの削除 (git push ラベル名 :ブランチ名)

$ git push origin :dev-n0bisuke
- [deleted]         dev-n0bisuke

一応確認
$ git branch -a
* master
  stash
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

GithubやBitbucketなどのリモート側を確認してみましょう!

18
19
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
18
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?