Git rebase

  1. [online][master] clone git project repo:
    git clone https://github.com/bmwieczorek/my-git-playground.git
  2. [offline][master] create, add and commit file1:
    echo "master: initial file create" > file1
    git add file1
    git commit -m "master: file1: initial file create" file1
  3. [offline][master] create my-branch:
    git branch my-branch
  4. [offline][master]: modify file1; create new file2; add and commit all:
    echo "master: updated after my-branch was created: append new line" >> file1
    echo "master: updated after my-branch was created: initial file create" > file2
    git add file1 file2
    git commit -m "master: after my-branch was created: updated to add new line in file1, file2 initial create" file1 file2
  5. [offline][master]: switch to my-branch:
    git checkout my-branch
    
    

    NOTE: as file1 modification will be reverted, file2 will be deleted (as it happened after my-branch was created, so my-branch does not have these changes). git log will contain:
    from 2): master: file1: initial file create

  6. [offline][my-branch]: create file my-branch1.txt; modify to add new line in file1.txt; add and commit all:
    echo "my-branch: initial file create" > my-branch1.txt
    echo "my-branch: modify in my-branch" >> file1.txt
    git add file1.txt my-branch1.txt
    git commit -m "my-branch: my-branch1.txt initial create and modified file1.txt to append new line" file1.txt my-branch1.txt

    git log will contain:
    from 6): my-branch: my-branch1.txt initial create and modified file1.txt to append new line” file1.txt my-branch1.txt
    from 2): master: file1: initial file create

  7. [offline][my-branch]: rebase to master:
    $ git rebase master
    First, rewinding head to replay your work on top of it...
    Applying: my-branch: modify file1.txt and initial create file-my-branch.txt in my-branch
    Using index info to reconstruct a base tree...
    M file1.txt
    Falling back to patching base and 3-way merge...
    Auto-merging file1.txt
    CONFLICT (content): Merge conflict in file1.txt
    Failed to merge in the changes.
    Patch failed at 0001 my-branch: modify file1.txt and initial create file-my-branch.txt in my-branch
    The copy of the patch that failed is found in:
     /cygdrive/c/dev/git/u1/master/my-git-playground/.git/rebase-apply/patch
    
    When you have resolved this problem, run "git rebase --continue".
    If you prefer to skip this patch, run "git rebase --skip" instead.
    To check out the original branch and stop rebasing, run "git rebase --abort".
  8. [offline][my-branch]: edit file1 to resolve conflict; add file1 (do not commit), and continue rebase:
    vim file1 ...
    
    cat file1.txt
    master: initial file create 
    master: updated after my-branch was created: append new line 
    my-branch: modify in my-branch
    
    git add file1
    
    git rebase --continue
    Applying: my-branch: my-branch: my-branch1.txt initial create and modified file1.txt to append new line


    NOTE: file system will contain file1 (with 3 lines), fil2 and branch1.txt

    NOTE: git log will contain (not in chronological order, but master changes first and my-branch changes then on the top of that):
    from 8) my-branch: my-branch1.txt initial create and modified file1.txt to append new line
    from 4) master: after my-branch was created: updated to add new line in file1, file2 initial create
    from 2) master: file1: initial file create


    NOTE: during rebase first my-branch changes were reverted, then master changes were applied and eventually my-branch changes were applied as the last

SECOND EXAMPLE

1. COMMIT ON MASTER
$ git clone https://github.com/bmwieczorek/my-git-rebase2.git
Cloning into 'my-git-rebase2'...
warning: You appear to have cloned an empty repository.
Checking connectivity... done.
$ cd my-git-rebase2/
$ echo "master: init create file1" > file1
$ git add file1
$ git commit -m "master: init create file1" file1
[master (root-commit) a12c8dc] master: init create file1
 1 file changed, 1 insertion(+)
 create mode 100644 file1
2. CREATE BRANCH AND COMMIT ON BRANCH
$ git branch my-branch
$ git checkout my-branch
Switched to branch 'my-branch'
$ echo "my-branch: init create branch1" > branch1
$ vim file1
$ cat file1
master: init create file1
my-branch: append new line
$ git add file1 branch1
$ git commit -m "my-branch: append new line to file1 and init create branch" file1 branch1
[my-branch 3f7e4db] my-branch: append new line to file1 and init create branch
 2 files changed, 2 insertions(+)
 create mode 100644 branch1
$ ls
branch1 file1
3. COMMIT ON MASTER
$ git checkout master
Switched to branch 'master'
$ echo "master: init create file2" > file2
$ ls
file1 file2
$ vim file1
$ cat file1
master: init create file1
master: append new line
$ git add file1 file2
$ git commit -m "master: append new line in file1 and init create file2"
[master aa27358] master: append new line in file1 and init create file2
 2 files changed, 2 insertions(+)
 create mode 100644 file2
$ git log
Date: Thu Jan 28 14:21:10 2016 +0100
 master: append new line in file1 and init create file2
Date: Thu Jan 28 14:18:42 2016 +0100
 master: init create file1
$ git checkout my-branch
Switched to branch 'my-branch'
$ git log
Date: Thu Jan 28 14:19:55 2016 +0100
 my-branch: append new line to file1 and init create branch
Date: Thu Jan 28 14:18:42 2016 +0100
 master: init create file1
4. ON BRANCH: REBASE WITH MASTER
$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: my-branch: append new line to file1 and init create branch
Using index info to reconstruct a base tree...
M file1
Falling back to patching base and 3-way merge...
Auto-merging file1
CONFLICT (content): Merge conflict in file1
Failed to merge in the changes.
Patch failed at 0001 my-branch: append new line to file1 and init create branch
The copy of the patch that failed is found in:
 /cygdrive/c/dev/git3/my-git-rebase2/.git/rebase-apply/patch
When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".
$ cat file1
master: init create file1
<<<<<<< aa273580658e4308ad01d4f72f7e3fef4b791757
master: append new line
=======
my-branch: append new line
>>>>>>> my-branch: append new line to file1 and init create branch
$ ls
branch1 file1 file2
5. RESOLVE CONFLICT ON BRANCH
$ vim file1
$ git status
rebase in progress; onto aa27358
You are currently rebasing branch 'my-branch' on 'aa27358'.
 (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 reset HEAD <file>..." to unstage)
 new file: branch1
Unmerged paths:
 (use "git reset HEAD <file>..." to unstage)
 (use "git add <file>..." to mark resolution)
 both modified: file1
$ git add file1
$ git status
rebase in progress; onto aa27358
You are currently rebasing branch 'my-branch' on 'aa27358'.
 (all conflicts fixed: run "git rebase --continue")
Changes to be committed:
 (use "git reset HEAD <file>..." to unstage)
 new file: branch1
 modified: file1
$ cat file1
master: init create file1
master: append new line
my-branch: append new line
6. ON BRANCH CONRINUE REBASE
$ git rebase --continue
Applying: my-branch: append new line to file1 and init create branch
7. ON BRANCH GIT LOG (not necessary chronological - rewind branch changes, apply first master changes, then reapply branch changes on top of master changes)
$ git log
Date: Thu Jan 28 14:19:55 2016 +0100
 my-branch: append new line to file1 and init create branch
Date: Thu Jan 28 14:21:10 2016 +0100
 master: append new line in file1 and init create file2
Date: Thu Jan 28 14:18:42 2016 +0100
 master: init create file1
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s