LoginSignup
0
0

More than 1 year has passed since last update.

【Git】rebase

Posted at

コミットメッセージの変更

現状確認

$ git log --oneline
e0136fa (HEAD -> master) add3
05674c6 add2
9d19a21 add1

編集

$ git rebase -i HEAD~3

▼変更前

../monaka/git_test/.git/rebase-merge/git-rebase-todo
pick 9d19a21 add1
pick 05674c6 add2
pick e0136fa add3

▼変更後(3番目のコミットをeditに変更)

../monaka/git_test/.git/rebase-merge/git-rebase-todo
pick 9d19a21 add1
pick 05674c6 add2
edit e0136fa add3

保存して終了すると、次にやるべきことを表示してくれている

$ git rebase -i HEAD~3
Stopped at e0136fa...  add3
You can amend the commit now, with

  git commit --amend 

Once you are satisfied with your changes, run

  git rebase --continue

コミットメッセージの修正

→コミットメッセージ編集画面が立ち上がるので、変更を加える

(あと、この時謎のブランチにいた)

$ git commit --amend 

▼修正完了後

$ git commit --amend 
[detached HEAD 6b9ea42] change add3 commit message
 Date: Sun Aug 14 12:07:04 2022 +0900
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 third.html

rebaseを続行

(以下を実行すると元のブランチに戻ってきてた)

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

確認

$ git log --oneline
e0136fa (HEAD -> master) change add3 commit message
05674c6 add2
9d19a21 add1

コミットの順番を入れ替える

現状確認

$ git log --oneline
e0136fa (HEAD -> master) change add3 commit message
05674c6 add2
9d19a21 add1

編集

$ git rebase -i HEAD~3

▼変更前

...it_test/.git/rebase-merge/git-rebase-todo Modified
pick 9d19a21 add1
pick 05674c6 add2
pick 6b9ea42 change add3 commit message

▼変更後(1番目のコミットと2番目のコミットの入れ替え)

...it_test/.git/rebase-merge/git-rebase-todo Modified
pick 05674c6 add2
pick 9d19a21 add1
pick 6b9ea42 change add3 commit message

編集完了後

$ git rebase -i HEAD~3
Successfully rebased and updated refs/heads/master.

確認

$ git log --oneline -n 3
f4abf55 (HEAD -> master) change add3 commit message
5c93e54 add1
7777adb add2

コミットをまとめる

現状確認

$ git log --oneline -n 3
f4abf55 (HEAD -> master) change add3 commit message
5c93e54 add1
7777adb add2

編集

$ git rebase -i HEAD~3

▼変更前

.../monaka/git_test/.git/rebase-merge/git-rebase-todo
pick 7777adb add2
pick 5c93e54 add1
pick f4abf55 change add3 commit message

▼変更後

...it_test/.git/rebase-merge/git-rebase-todo Modified
pick 7777adb add2
squash 5c93e54 add1
squash f4abf55 change add3 commit message

コミットメッセージ編集画面(変更してもしなくてもOK)

      /home/monaka/git_test/.git/COMMIT_EDITMSG      
# This is a combination of 3 commits.
# This is the 1st commit message:

add2

# This is the commit message #2:

add1

# This is the commit message #3:

change add3 commit message

確認

$ git log --oneline
3ecbd1e (HEAD -> master) add2
96283d2 (origin/master) add feature 

コミットを分割する

現状確認

$ git log --oneline
3ecbd1e (HEAD -> master) add2

編集

$ git rebase -i HEAD~1

▼変更前

...it_test/.git/rebase-merge/git-rebase-todo Modified
pick 3ecbd1e add2

▼変更後

...it_test/.git/rebase-merge/git-rebase-todo Modified
edit 3ecbd1e add2

editと記載されているコミットのコミットを取り消して、ステージングしていない状態に戻す

  • git reset コマンド:コミットを取り消してステージングしていない状況まで戻す
  • HEAD^(キャレット):editと記載したコミットを指す
$ git reset HEAD^

確認

$ git status
interactive rebase in progress; onto 96283d2
Last command done (1 command done):
   edit 3ecbd1e add2
No commands remaining.
You are currently editing a commit while rebasing branch 'master' on '96283d2'.
  (use "git commit --amend" to amend the current commit)
  (use "git rebase --continue" once you are satisfied with your changes)

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        first.html
        second.html
        third.html

nothing added to commit but untracked files present (use "git add" to track)

first.htmlとsecond.htmlをコミット

$ git add first.html second.html 
$ git commit -m 'add1&2'
[detached HEAD c8c96fd] add1&2
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 first.html
 create mode 100644 second.html

third.htmlをコミット

$ git add third.html 
$ git commit -m 'add3'
[detached HEAD eb18196] add3
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 third.html

リベース

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

確認

$ git log --oneline
eb18196 (HEAD -> master) add3
c8c96fd add1&2

コミットの削除

確認

$ git log --oneline -n 3
eb18196 (HEAD -> master, origin/master) add3
c8c96fd add1&2
96283d2 add feature

編集

$ git rebase -i HEAD~3

▼変更前

.../monaka/git_test/.git/rebase-merge/git-rebase-todo
pick 96283d2 add feature
pick c8c96fd add1&2
pick eb18196 add3

▼変更後

...it_test/.git/rebase-merge/git-rebase-todo Modified
pick 96283d2 add feature

確認

$ git log --oneline -n 3
96283d2 (HEAD -> master) add feature

参考

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