困ったときに使うgitのコマンドまとめ

2021-01-07
岡野 洋平
#
git
#
#

岡野です。

みなさんはgitはコマンドで扱っていますか?

それともSourcetree等のGUIをお使いでしょうか?

私はコマンドを扱っております。理由は特に有りません。

そんなコマンドユーザーの私ではございますが、

gitコマンドで困ったときに使うコマンドを調べて、

メモしてきた内容を開放します!

gitコマンドの実行は自己責任でお願いします!

git コマンドまとめ

困った内容と共に記載していきたいと思います。

他のブランチの変更を取り込みたい

あ、今実装しているこのブランチの対応、あのブランチの修正がないと動かんわ。。

$ git merge このブランチ あのブランチ

※プルリク出すときはあのブランチをターゲットすると、変更点が明確になってよいでしょう。

ローカルブランチ、リモートブランチの作成

よし!次のissue対応するぞ!

$ git checkout -b 新規作成するブランチ ベースとなるリモートブランチ
$ git push -u origin 新規作成するブランチ

// 例)
// git checkout -b new-branch origin/master
// git push -u origin new-branch

リモートブランチをcheckout

依頼されたレビュー対象のリモートブランチ、ローカルに持ってこよ。

$ git checkout -b 新規作成するローカルブランチ名 ローカルに持ってきたいリモートブランチ名

// 例) git checkout -b okano/review origin/okano/review

Push済の過去のコミットをまとめる

あーしょうもないコミットをPushしすぎた。。これは人に見せられるコミットログではない。。

Pushしちゃってるけど、このコミットログまとめよ。

1. まとめるコミットの確認

$ git log --oneline

AAAAAAA コミットA
BBBBBBB コミットB
CCCCCCC コミットC
DDDDDDD コミットD
EEEEEEE コミットE

2. rebaseでコミットをまとめる作業を開始する。

// 特定のコミットから最新までのコミットをまとめる場合
$ git rebase -i EEEEEEE

// 最新から5番目までのコミットをまとめる場合
$ git rebase -i HEAD~5

3. まとめるコミットを指定する。まとめる場合は 以下のように初期表示のpicksを変更する。

変更後にwq!で保存する。

s AAAAAAA コミットA
s BBBBBBB コミットB
s CCCCCCC コミットC
s DDDDDDD コミットD
pick EEEEEEE コミットE

# Rebase AAAAAAA..EEEEEEE onto XXXXXXX
#
# 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

4. 次にコミットコメントを指定する画面が開くので、必要に応じて修正する。

変更後にwq!で保存する。

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

機能追加対応

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# Not currently on any branch.
# Changes to be committed:
#   (use "git reset HEAD ..." to unstage)
#
# modified:   HttpRequest.php
#

5. コミットをまとめ、コミットコメントを修正後に、以下のコマンドでpushして完了

$ git push -f

※強制プッシュは履歴を書き換えるので使用には細心の注意を払ってください!

コミットはせずに変更を退避したい

修正対応中だけど、他のブランチに切り替えたい。

あれ?

error: Your local changes to the following files would be overwritten by checkout:
    対象ファイル
Please, commit your changes or stash them before you can switch branches.
error: The following untracked working tree files would be overwritten by checkout:
    対象ファイル
Please move or remove them before you can switch branches.
Aborting

って言われる。。

1. `git status` で状況確認

$ git status

Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

    modified:   修正中のファイル

2. `git stash` コマンドで修正中のファイルを退避。その後、退避できているか確認。

$ git stash save
Saved working directory and index state WIP on XXXXX: xxxxx

$ git status
On branch master
Your branch is up to date with 'origin/master'.

3. `git checkout`で切り替えたいブランチに移動する

4. 退避した情報を確認する。

$ git stash list
stash@{0}: WIP on XXXXX: xxxxx

5. 退避したコードを戻す。

$ git stash pop stash@{0}
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

    modified:   修正中のファイル

最後に

gitで同じような事で困る度に調べていたので、メモにまとめたいた内容です。

やはりメモは重要ですね。効率が変わります。

暗記してしまえばと思いますが、まずはメモから!

岡野です。

みなさんはgitはコマンドで扱っていますか?

それともSourcetree等のGUIをお使いでしょうか?

私はコマンドを扱っております。理由は特に有りません。

そんなコマンドユーザーの私ではございますが、

gitコマンドで困ったときに使うコマンドを調べて、

メモしてきた内容を開放します!

gitコマンドの実行は自己責任でお願いします!

git コマンドまとめ

困った内容と共に記載していきたいと思います。

他のブランチの変更を取り込みたい

あ、今実装しているこのブランチの対応、あのブランチの修正がないと動かんわ。。

$ git merge このブランチ あのブランチ

※プルリク出すときはあのブランチをターゲットすると、変更点が明確になってよいでしょう。

ローカルブランチ、リモートブランチの作成

よし!次のissue対応するぞ!

$ git checkout -b 新規作成するブランチ ベースとなるリモートブランチ
$ git push -u origin 新規作成するブランチ

// 例)
// git checkout -b new-branch origin/master
// git push -u origin new-branch

リモートブランチをcheckout

依頼されたレビュー対象のリモートブランチ、ローカルに持ってこよ。

$ git checkout -b 新規作成するローカルブランチ名 ローカルに持ってきたいリモートブランチ名

// 例) git checkout -b okano/review origin/okano/review

Push済の過去のコミットをまとめる

あーしょうもないコミットをPushしすぎた。。これは人に見せられるコミットログではない。。

Pushしちゃってるけど、このコミットログまとめよ。

1. まとめるコミットの確認

$ git log --oneline

AAAAAAA コミットA
BBBBBBB コミットB
CCCCCCC コミットC
DDDDDDD コミットD
EEEEEEE コミットE

2. rebaseでコミットをまとめる作業を開始する。

// 特定のコミットから最新までのコミットをまとめる場合
$ git rebase -i EEEEEEE

// 最新から5番目までのコミットをまとめる場合
$ git rebase -i HEAD~5

3. まとめるコミットを指定する。まとめる場合は 以下のように初期表示のpicksを変更する。

変更後にwq!で保存する。

s AAAAAAA コミットA
s BBBBBBB コミットB
s CCCCCCC コミットC
s DDDDDDD コミットD
pick EEEEEEE コミットE

# Rebase AAAAAAA..EEEEEEE onto XXXXXXX
#
# 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

4. 次にコミットコメントを指定する画面が開くので、必要に応じて修正する。

変更後にwq!で保存する。

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

機能追加対応

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# Not currently on any branch.
# Changes to be committed:
#   (use "git reset HEAD ..." to unstage)
#
# modified:   HttpRequest.php
#

5. コミットをまとめ、コミットコメントを修正後に、以下のコマンドでpushして完了

$ git push -f

※強制プッシュは履歴を書き換えるので使用には細心の注意を払ってください!

コミットはせずに変更を退避したい

修正対応中だけど、他のブランチに切り替えたい。

あれ?

error: Your local changes to the following files would be overwritten by checkout:
    対象ファイル
Please, commit your changes or stash them before you can switch branches.
error: The following untracked working tree files would be overwritten by checkout:
    対象ファイル
Please move or remove them before you can switch branches.
Aborting

って言われる。。

1. `git status` で状況確認

$ git status

Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

    modified:   修正中のファイル

2. `git stash` コマンドで修正中のファイルを退避。その後、退避できているか確認。

$ git stash save
Saved working directory and index state WIP on XXXXX: xxxxx

$ git status
On branch master
Your branch is up to date with 'origin/master'.

3. `git checkout`で切り替えたいブランチに移動する

4. 退避した情報を確認する。

$ git stash list
stash@{0}: WIP on XXXXX: xxxxx

5. 退避したコードを戻す。

$ git stash pop stash@{0}
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

    modified:   修正中のファイル

最後に

gitで同じような事で困る度に調べていたので、メモにまとめたいた内容です。

やはりメモは重要ですね。効率が変わります。

暗記してしまえばと思いますが、まずはメモから!

株式会社グランドリームでは、AWSを駆使した開発からUI/UXデザインまで、Webアプリケーションに関するすべての要望に応えます。
まずは一度お気軽にご相談ください。

お問い合わせはこちら