Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Create a new folder called branching and git init it.

Copy the text file six_honest_serving_men.txt from the commits folder.

Git add and commit the changes in the folder.

Create a branch called feature/rev_will_not_be_televised using the command git checkout -b feature/rev_will_not_be_relevised

Add the following lines of text (or something that appeals to you) between the two verses of the original poem

Code Block
From the Indians who welcomed the pilgrims
  And to the buffalos who once ruled the plain
Like the vultures circling beneath the dark clouds
  Looking for the rain
  Looking for the rain
Just like the cities staggered on the coastline
  In a nation that just can't stand much more
Like the forest buried beneath the highway
  Never had a chance to grow
  Never had a chance to grow

So the poem should look like this

Code Block
I keep six honest serving-men
  (They taught me all I knew);
Their names are What and Why and When
  And How and Where and Who.
I send them over land and sea,
  I send them east and west;
But after they have worked for me,
  I give them all a rest.

From the Indians who welcomed the pilgrims
  And to the buffalos who once ruled the plain
Like the vultures circling beneath the dark clouds
  Looking for the rain
  Looking for the rain
Just like the cities staggered on the coastline
  In a nation that just can't stand much more
Like the forest buried beneath the highway
  Never had a chance to grow
  Never had a chance to grow
  
I let them rest from nine till five,
  For I am busy then,
As well as breakfast, lunch, and tea,
  For they are hungry men.
But different folk have different views;
  I know a person small
She keeps ten million serving-men,
  Who get no rest at all!
She sends em abroad on her own affairs,
  From the second she opens her eyes
One million Hows, Two million Wheres,
  And seven million Whys!
  
A poem by Rudyard Kipling

Stage and commit the changes - git add . followed by git commit -m "Added Winter in America Lyrics"

If you are using git bash, it should be visible which branch you are on. if you are not using git bash, type git branch to see which branch you are one.

Switch back to the main branch git checkout main

Notice that the text file six_honest_serving_men.txt reverts back to its original state. In actual fact, it hasn’t reverted, it’s simply showing a different truth on a different branch.

It’s good practice to develop off branches and merge those changes into the main/develop branch (typically called known as trunk - main/develop).

Merging changes into main

Because we are not working with a remote git repo, we can merge changes from one branch into the current using git merge

From the command line check that you are on the main branch using the command git branch

...

If you are not on the main branch type git checkout main

Now merge the feature branch feature/rev_will_not_be_televised using the command

Code Block
git merge feature/rev_will_not_be_televised

You should see something like this

...

Because we made changes that git could detect, the merge was completed with no issues.

It’s good practice to delete the branch once the feature is complete. Let’s go ahead and delete the branch feature/rev_will_not_be_televised

Merge conflicts

If whilst you were making changes to your feature branch, someone changed the main branch, the main is considered to be ahead of the feature branch, this will cause a merge conflict.

Switch back to the feature/rev_will_not_be_televised branch

Delete the lines of text from the file (around lines 15-19) and save the file

Code Block
Just like the cities staggered on the coastline
  In a nation that just can't stand much more
Like the forest buried beneath the highway
  Never had a chance to grow
  Never had a chance to grow

Stage and commit the changes

Switch back to main, the lines will reappear because on this branch (the current state of your application) nothing else has been merged into it.

Remove lines 18-19 and save the file

Code Block
  Never had a chance to grow
  Never had a chance to grow

Perform a git merge of feature/rev_will_not_be_televised into main.

You get an error

...

Git is very good at telling why an operation cannot be completed. It’s worth reading the messages carefully and following the instructions it gives you.

In this case, we haven’t staged and committed the changes we’ve made in main.

Stage and commit your changes.

Perform a git merge of feature/rev_will_not_be_televised into main.

...

Notice now that you have a merge conflict. This is because you made changes in branches and now you’re trying to merge them together.

Info

Always keep main and develop branches clean of any development. Merge into them, and keep them as the single version of the truth.

...

The 7 less than symbols followed by HEAD indicates the beginning of where the current branch differs from the branch you are merging in.

The 7 equal symbols indicate where the current branch change ends.

The 7 greater than symbols followed by the branch name indicates where the merge branch is being inserted into the current branch.

We must decide which branch holds the truth or is it a combination of the two branches?

We’ve decided that the main branch is the truth.

Edit the file and remove the git indicators so that the text looks like this, save the file

Code Block
From the Indians who welcomed the pilgrims
  And to the buffalos who once ruled the plain
Like the vultures circling beneath the dark clouds
  Looking for the rain
  Looking for the rain
Just like the cities staggered on the coastline
  In a nation that just can't stand much more
Like the forest buried beneath the highway

Stage and commit the changes on the main branch. You should notice that git no longer appends MERGING to the branch name.

Here is a visual view of the branches

...