git merge

git merge

For a better understanding of how something work, you should find out it in practice, by reading you can understand some idea, but will not make a clear vision in your head.

Let's start to find out how git merge works. I've created some folder by using mkdir command, then created index.js file, then have made :

git add .
git commit -m First-commit

After executing these commands, you will get your first commit and master branch created. From this point history of your project started. Let's move on:

commit 5137027981456931dcb30bd3d50df30956632e11
Author: Andrey Radkevich <email@gmail.com>
Date:   Wed Nov 18 21:36:29 2020 +0200

    First-commit

Above showed what you will see when you will execute git log command. For merging we need some other branch, let's create stage branch by using git checkout -b stage command.

git branch

* master
  stage

Above you see list of branches you already have. Let's continue create new commits for extending our project history :

nano index.js

index.js

const firstVariable = 'first';

above we have created some text in index.js file , the we will create new commit for this :

git add .
git commit -m second-commit-done-in-stage

then extend our index.js file and create one more commit for stage branch nano index.js

index.js

const firstVariable = 'first';
const secondVariable = 'second';
git add .
git commit -m third-commit-stage-branch

What tree do we have at this moment , let's visualize it little bit :

stage   -> second-commit-done-in-stage -> third-commit-stage-branch
master -> First-commit

Then we want to immitate then when you was working in stage branch someone have created new commit to master branch

git checkout master
touch styles.css
nano styles css

styles.css

body {fontSize: 12px;}
git add .
git commit -m second-commit-master-branch

then we can merge 2 branches to see what will happens with our history, how commits will be shown in our git history

git merge stage - merging stage to master

git log

commit 09b141d9a391964ac49a740da508c40f34fb8bb8 (HEAD -> master)
Merge: 4821eb2 7e9cd7f
Author: Andrey Radkevich <email@gmail.com>
Date:   Wed Nov 18 21:48:07 2020 +0200

    Merge branch 'stage'

commit 4821eb27912cd300b5229c480c705fc5d2741310
Author: Andrey Radkevich <email@gmail.com>
Date:   Wed Nov 18 21:45:37 2020 +0200

    second-commit-master-branch

commit 7e9cd7f62a14f4401a74545fa75e1f4df3cf1851 (stage)
Author: Andrey Radkevich <email@gmail.com>
Date:   Wed Nov 18 21:43:02 2020 +0200

    third-commit-stage-branch

commit 2448201e79dd15635ab95279be8f6c4d6acae3cb
Author: Andrey Radkevich <email@gmail.com>
Date:   Wed Nov 18 21:40:03 2020 +0200

    second-commit-done-in-stage

commit 5137027981456931dcb30bd3d50df30956632e11
Author: Andrey Radkevich <email@gmail.com>
Date:   Wed Nov 18 21:36:29 2020 +0200

    First-commit

master -> first-commit- second-commit-done-in-stage - third-commit-stage-branch - second-commit-master-branch - Merge branch 'stage'

As you can see changes which was done in stage was moved before commit made in master and also there was created additional item Merge branch 'stage', not really clear history , and a lot of no needed information as for me , next step will be find out how git rebase works , and compare 2 of them , and find out pros and const of both. Hope I will make it , by using the same examples of code , step-by-step

We will find out together , see you in next article !