0
0

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 1 year has passed since last update.

[Git] 動作を試す 実行例38:rebaseでブランチの派生元を変更する

Posted at

Gitの動作を理解するために、Gitのコマンドを実際に試して、結果を見てみました。

1つの記事内で一連のGitコマンドが完結しているので、これら一連のコマンドを順に実行させて結果を見ることで、一連のGitの動作を実際に体感でき、一通り独習することが可能です。

※前回記事のリポジトリ状態からの続きになっています。

前回記事へ 目次へ:Git関連記事のまとめページ 次回記事へ

実行例

(※ひとまず適当に新規ブランチで作業する変更の操作を模擬、branch2の位置から新規のbranch3を作成することにする)
git checkout branch2
dir /b
↓
結果: 
test1.txt
test10.txt
test2.txt
test4.txt

※新規ブランチを作成する
git checkout -b branch3
↓
結果: 
Switched to a new branch 'branch3'

echo Sample-Added-114 >> test1.txt
echo Sample-Added-******21 > test2.txt
git commit -a -m "message14"
↓
結果: 
[branch3 2f0863f] message14
 2 files changed, 2 insertions(+), 1 deletion(-)

echo Sample-Added-115 >> test1.txt
echo Sample-Added-115 > test15.txt
git add .
git commit -m "message15"
↓
結果: 
[branch3 6f87a33] message15
 2 files changed, 2 insertions(+)
 create mode 100644 test15.txt

type test1.txt
↓
結果: 
Sample-Added-11
Sample-Added-12
Sample-Added-13
Sample-Added-14
Sample-Added-18
Sample-Added-19
Sample-Added-110
Sample-Added-114
Sample-Added-115

-----

git log --oneline --graph --all
↓
結果: 
* 6f87a33 (HEAD -> branch3) message15
* 2f0863f message14
| *   2b19eaa (branch1) Merge branch 'master' into branch1
| |\
| | * a4d1247 (master) message13 (fixed test10.txt)
| | *   4791df6 Merge branch 'branch2'
| | |\
| |_|/
|/| |
* | | f3ef64d (branch2) message12
* | | 7fdeb04 message10
* | | b838160 message9
* | | c68ef01 message8
| * | 135f847 Merge branch 'master' into branch1
| |\|
| | * 60b0341 message12
| | * 045a734 message11
| | * d10f934 message10
| |/
|/|
| * 2495cae message7
|/
* 94996a0 message4
* 20bbab7 message3
* 00cad71 message2
* 4ace194 message1

※現在のブランチ構造の模式図(+Mはマージ、10C,12Cはチェリーピック)
message1-2-3-4-10-11-12-+M-13 ←master
             |       ↓ ↑ ↓
             +-7-----+M-↑-+M ←branch1
             |          ↑
             +-8-9-10C--12C ←branch2
                         |
                         +-14-15 ←branch3

-----

(※branch3をmasterの所へリベース master ← branch3)

※ブランチの派生元を変更する=リベース
git rebase master
↓
結果: 
Auto-merging test1.txt
CONFLICT (content): Merge conflict in test1.txt
error: could not apply 2f0863f... message14
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply 2f0863f... message14

git status
↓
結果: 
interactive rebase in progress; onto a4d1247
Last command done (1 command done):
   pick 2f0863f message14
Next command to do (1 remaining command):
   pick 6f87a33 message15
  (use "git rebase --edit-todo" to view and edit)
You are currently rebasing branch 'branch3' on 'a4d1247'.
  (fix conflicts and then run "git rebase --continue")
  (use "git rebase --skip" to skip this patch)
  (use "git rebase --abort" to check out the original branch)

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   test2.txt

Unmerged paths:
  (use "git restore --staged <file>..." to unstage)
  (use "git add <file>..." to mark resolution)
        both modified:   test1.txt

git diff
↓
結果: 
diff --cc test1.txt
index 1c2a726,774edb7..0000000
--- a/test1.txt
+++ b/test1.txt
@@@ -5,4 -5,4 +5,8 @@@ Sample-Added-1
  Sample-Added-18
  Sample-Added-19
  Sample-Added-110
++<<<<<<< HEAD
 +Sample-Added-111
++=======
+ Sample-Added-114
++>>>>>>> 2f0863f (message14)

(※コンフリクトを手動で解消)
echo Sample-Added-11 > test1.txt
echo Sample-Added-12 >> test1.txt
echo Sample-Added-13 >> test1.txt
echo Sample-Added-14 >> test1.txt
echo Sample-Added-18 >> test1.txt
echo Sample-Added-19 >> test1.txt
echo Sample-Added-110 >> test1.txt
echo Sample-Added-111 >> test1.txt
echo Sample-Added-114 >> test1.txt
type test1.txt
↓
結果: 
Sample-Added-11
Sample-Added-12
Sample-Added-13
Sample-Added-14
Sample-Added-18
Sample-Added-19
Sample-Added-110
Sample-Added-111
Sample-Added-114

git add .
git status -s
↓
結果: 
M  test1.txt
M  test2.txt

git rebase --continue
↓
結果: 
hint: Waiting for your editor to close the file...       0 [main] vim (16140) D:\...\PortableGit\usr\bin\vim.exe: *** fatal error - cygheap base mismatch detected - 0x1371408/0x1331408.

※git-cmd.exeでは、vim.exeエディタの起動でエラーが発生する模様。

git status
↓
結果: 
interactive rebase in progress; onto a4d1247
Last command done (1 command done):
   pick 2f0863f message14
Next command to do (1 remaining command):
   pick 6f87a33 message15
  (use "git rebase --edit-todo" to view and edit)
You are currently rebasing branch 'branch3' on 'a4d1247'.
  (all conflicts fixed: run "git rebase --continue")

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   test1.txt
        modified:   test2.txt

※2つのコマンドを編集する為に、「git rebase --continue」呼出しでエディタが起動する模様

※既定のエディタをWindowsのNotepadに変更
git config --global core.editor notepad

git rebase --continue
↓
結果: 
hint: Waiting for your editor to close the file...       0 [main] sh (19828) D:\...\PortableGit\usr\bin\sh.exe: *** fatal error - cygheap base mismatch detected - 0x12C1408/0x1281408.

※まだ、エラーが発生する。繰り返し「git rebase --continue」を呼出し。

git status
↓
結果: 
interactive rebase in progress; onto a4d1247
Last command done (1 command done):
   pick 2f0863f message14
Next command to do (1 remaining command):
   pick 6f87a33 message15
  (use "git rebase --edit-todo" to view and edit)
You are currently editing a commit while rebasing branch 'branch3' on 'a4d1247'.
  (use "git commit --amend" to amend the current commit)
  (use "git rebase --continue" once you are satisfied with your changes)

nothing to commit, working tree clean

git rebase --continue
↓
結果: 
Successfully rebased and updated refs/heads/branch3.

※繰り返し「git rebase --continue」呼出しで、成功と表示される

git status
↓
結果: 
On branch branch3
nothing to commit, working tree clean

type test1.txt
↓
結果: 
Sample-Added-11 
Sample-Added-12 
Sample-Added-13 
Sample-Added-14 
Sample-Added-18 
Sample-Added-19 
Sample-Added-110 
Sample-Added-111 
Sample-Added-114 
Sample-Added-115

git log --oneline --graph --all
↓
結果: 
* 8133746 (HEAD -> branch3) message15
* 51006a6 message14
| *   2b19eaa (branch1) Merge branch 'master' into branch1
| |\
| |/
|/|
* | a4d1247 (master) message13 (fixed test10.txt)
* |   4791df6 Merge branch 'branch2'
|\ \
| * | f3ef64d (branch2) message12
| * | 7fdeb04 message10
| * | b838160 message9
| * | c68ef01 message8
| | *   135f847 Merge branch 'master' into branch1
| | |\
| |_|/
|/| |
* | | 60b0341 message12
* | | 045a734 message11
* | | d10f934 message10
|/ /
| * 2495cae message7
|/
* 94996a0 message4
* 20bbab7 message3
* 00cad71 message2
* 4ace194 message1

※現在のブランチ構造の模式図(+Mはマージ、10C,12Cはチェリーピック)
                           master
                           ↓
message1-2-3-4-10-11-12-+M-13-14-15 ←branch3
             |       ↓ ↑ ↓
             +-7-----+M-↑-+M ←branch1
             |          ↑
             +-8-9-10C--12C ←branch2

-----

(※masterブランチをbranch3と同じ位置まで進めておく=Fast-forward)
git checkout master
git merge branch3
↓
結果: 
Updating a4d1247..8133746
Fast-forward
 test1.txt  | 2 ++
 test15.txt | 1 +
 test2.txt  | 2 +-
 3 files changed, 4 insertions(+), 1 deletion(-)
 create mode 100644 test15.txt

git log --oneline -1
↓
結果: 
8133746 (HEAD -> master, branch3) message15

-----

(※既にマージ済みの状態にあるbranch2をmasterにリベースする場合の動作を見る)

git checkout branch2
git log --oneline -1
↓
結果: 
f3ef64d (HEAD -> branch2) message12

※ブランチの派生元を変更する=リベース
git rebase master
↓
結果: 
Successfully rebased and updated refs/heads/branch2.

git log --oneline -1
↓
結果: 
8133746 (HEAD -> branch2, master, branch3) message15

※既にマージ済みのbranch2からは、何もリベースされるコミットなし、ブランチの指し位置が移るだけ

(※当リベース前の状態に戻す)
git reset --hard f3ef64d

-----

(※一度マージ済みのbranch2をmasterにリベースする場合の動作を見る)

dir /b
↓
結果: 
test1.txt
test10.txt
test2.txt
test4.txt

echo Sample-Added-116 > test16.txt
git add .
git commit -m "message16"

※ブランチの派生元を変更する=リベース
git rebase master
↓
結果: 
Successfully rebased and updated refs/heads/branch2.

dir /b
↓
結果: 
test1.txt
test10.txt
test15.txt
test16.txt
test2.txt
test4.txt

(※当リベース前の状態に戻す)
git reset --hard f3ef64d

環境

Windows 10、PortableGit-2.40.0-64-bitを使用、全てローカルPC上で実施、GitHub等は不使用。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?