Git Merge Guide - part 1
(these posts are part of the material I cover in my devops course)
What is git merge
- Let's say you work in your local git repository, and you were doing some work in the work branch:
1$>
2$> git branch
3* work
4$> ls
5work1.txt
6$>
- You want to add something, but you are not sure it is going to be good, so you switch to a new branch (called new-work). The -b option instructs git to create that branch for you.
- Initially, the new branch is exactly like the old one.
1$>
2$> git checkout -b new-work
3Switched to a new branch 'new-work'
4$> git branch
5* new-work
6 work
7$> git status
8On branch new-work
9nothing to commit, working tree clean
10$> ls
11work1.txt
12$>
- But then you add another file (work2.txt), add and commit:
1$> ls
2work1.txt
3$> echo world > work2.txt
4$> git add work2.txt
5$> git commit -m "adding work2.txt"
6[new-work f612a62] adding work2.txt
7 1 file changed, 1 insertion(+)
8 create mode 100644 work2.txt
9$>
10$> ls
11work1.txt work2.txt
12$>
Merging my work
- It is now the time to add my changes (created ub new-work branch) to work branch, that is my primary development environment in this example.
- The git merge command is used for that:
1$>
2$> git status
3On branch new-work
4nothing to commit, working tree clean
5$>
6$> git checkout work
7Switched to branch 'work'
8$> git merge new-work
9Updating e75d5b3..f612a62
10Fast-forward
11 work2.txt | 1 +
12 1 file changed, 1 insertion(+)
13 create mode 100644 work2.txt
14$>
- Here's what happened:
- git search for a mutual commit, a point in time where both branches were the same.
- It then tried to apply only the changes (in our case adding a file) to the merging branch (work in our case.)
- This was a simple case:
There were no changes in work after switching to new-work, so work had to just look like new-work. - Git moved the branch (which is just a "pointer" to point to the last commit in new-work)
- This type of very simple merge is called fast forware
- There is an image from the scm-book showing exactly this:
- This image shows a simmilar situation, just before merge has taken place. Here, master has not changed anything, so merging iss53 into master means that master should point to the same place where iss53 points to.