This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 8: git branch

git merge

face Josiah Wang

Now, let’s switch back to the killer branch.

user@MACHINE:~/branchtest$ git checkout killer
user@MACHINE:~/branchtest$ git log --oneline
9d34c0a (HEAD -> killer) Added a new file idea.txt
6ca445c Added b to python.txt
620496e Initial commit
user@MACHINE:~/branchtest$ ls
idea.txt  python.txt

You might notice that idea.txt is back.

If you look at python.txt, you will see that the latest change has not been included (c had not been added). Obviously, the file has not been changed in killer branch version, since it is now independent of the main branch since breaking out.

Now let’s say you are happy with your killer idea, and want to include this in your main branch. We will now try to merge all your changes back into the main branch.

First, switch back to the main branch. This is something you need to remember to do before merging. The run git merge killer -m "Your commit message".

user@MACHINE:~/branchtest$ git checkout main
user@MACHINE:~/branchtest$ git merge killer -m "My killer idea works! Merging killer to main branch"
Merge made by the 'recursive' strategy.
 idea.txt | 3 +++
 1 file changed, 3 insertions(+)
 create mode 100644 idea.txt
user@MACHINE:~/branchtest$ git log --oneline --all --graph
*   23e5475 (HEAD -> main) My killer idea works! Merging killer to main branch
|\
| * 9d34c0a (killer) Added a new file idea.txt
* | 76917d8 Added c to python.txt
|/
* 6ca445c Added b to python.txt
* 620496e Initial commit

As you can see from the graph, you have merged both branches, and the main branch points to the latest commit.

And if you look at your branchtest directory, you will find idea.txt in there, as well as the latest python.txt!