LoginSignup
60
58

More than 3 years have passed since last update.

【 Git 】特定のcommitをチェックアウト

Last updated at Posted at 2016-03-05

備忘録

githubから特定のcommitをローカルにcheckoutしてくるだけなら以下のコマンドだが

$ git checkout {sha1} 

毎度別コマンドで諸々確認しつつになる。毎回忘れて調べるのでメモしておく
(以下の手順でやるわけではなくあくまで発生しうる作業の羅列)

発生する作業

  • リモートレポジトリをローカルにクローン
  • Current Branch を確認
  • 該当リモートブランチがあるかを確認
  • 該当リモートブランチをローカルにも一応そのブランチを切ってからそこにcheckout
  • Current Branch を確認
  • ローカルブランチにリモートブランチの特定のcommitをcheckout
  • (確認作業)
  • 確認作業やdotfile(ex .DS_STORE)によってローカルレポジトリに意図しない変更が追加されていないかを確認
  • 上記で追加されている場合は変更をstash
  • ローカルブランチにリモートブランチの特定のcommitをcheckout
  • (確認作業)
  • 確認作業後、特にリモートブランチに加える変更がなければstashしていた変更を削除
  • Finish upとして、ローカルリポジトリのの状態を確認
  • Detached Headになっている場合は最新のリモートレポジトリをチェックアウトしなおす
  • 終了

リモートレポジトリをローカルにクローン

$ git clone {url to source repo}

Current Branch を確認

$ git branch

該当リモートブランチがあるかを確認

$ git branch -a

該当リモートブランチをローカルにも一応そのブランチを切ってからそこにcheckout

例えば以下ような状態の場合

$ git branch -a
* master 
  remotes/origin/HEAD
  remotes/origin/master
  remotes/origin/develop

以下のようになる

$ git checkout -b develop origin/develop

ローカルブランチにリモートブランチの特定のcommitをcheckout

$ git checkout <sha1>

以下のようなワーニングがでるが一旦無視

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

確認作業やdotfile(ex .DS_STORE)によってローカルレポジトリに意図しない変更が追加されていないかを確認

上記の作業後続けて別commitをチェックアウトしようとすると以下のようにエラーとなる

error: Your local changes to the following files would be overwritten by checkout:
    app246/.DS_Store
Please, commit your changes or stash them before you can switch branches.
Aborting

その場合は変更をstash(一時退避)

$ git stash

確認

$ git status

以下のようなメッセージならOK

nothing to commit, working directory clean

確認作業後、特にリモートブランチに加える変更がなければstashしていた変更を削除

stashしている変更を確認

$ git stash list
 stash@{0}: WIP on sub: 195f25f add blah
 stash@{1}: WIP on sub: d34ee25 remove blah

削除していいstashは削除

$ git stash drop stash@{0} 

すべて削除する場合は

$ git stash clear

Detached Headになっている場合は最新のリモートレポジトリをチェックアウトしなおす

git statusすると以下のようなワーニングが出てきがち

HEAD detached at 4ee25f6
nothing to commit, working directory clean

その場合は最新のリモートリポジトリをチェックアウトしなおす

$ git checkout develop

以上

60
58
1

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
60
58