0% found this document useful (0 votes)
11 views29 pages

Ci CD-02

The document discusses branching in version control systems. It covers the basics of branching, creating new branches, navigating between branches, merging branches, and deleting branches. Key commands for working with branches in Git are also provided.

Uploaded by

kevf7585
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views29 pages

Ci CD-02

The document discusses branching in version control systems. It covers the basics of branching, creating new branches, navigating between branches, merging branches, and deleting branches. Key commands for working with branches in Git are also provided.

Uploaded by

kevf7585
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Branching

Goals of the ● Branching Basics: Learn what branching is and


its role in software development.

Module ● Creating New Branches: Discover how to start a


new branch for your projects.

● Navigating Between Branches: Get skilled at


switching and managing multiple branches.

● Merging Branches: Understand how to combine


branches, including solving merge conflicts.

● Deleting Branches: Learn when and how to


delete branches to maintain an organized
repository.
Intro to Branching
Intro to Branching
Branching

VCS systems use branching - parallel versions of the codebase

- Often one Main branch


- Branches start from other branches
- Branches merge with other branches
Intro to Branching

- Often there is a branching strategy for a project, for example


- One main branch
- One branch per feature/bugfix
- One branches for every published version

- Often one person works on one issue


- Otherwise; one branch per person
Intro to Branching

- When you create a branch, only you have it…

- …until you push it to another repository


Intro to Branching

Whenever we initialize a folder as a repository, there will be


one default branch (typically called main).

We can create a branch that is a copy of (usually) the main


branch. Then you can add your changes to this branch
without affecting the main branch.
Intro to Branching

With Git, we can create many of these alternative


environments (i.e. we can create different branches) so
other versions of our project code can exist and be tracked
in parallel.

That allows us to add new (experimental, unfinished, and


potentially buggy) features in separate branches, without
touching the 'official' stable version of our project code
(which is usually kept on the main branch).
Creating a New Branch
Creating a New Branch

You can create a new branch using the following command:

The new branch that gets created will be the reference to the current state of your
repository.
Creating a New Branch

Main
Commit1 Commit2 Commit3 Commit4
23ad9ad 16b36c6 ad8621a 0e25143

branch1

Create new branch called branch1 start from commit2


git checkout -b <branch name> [start point]

$ git checkout -b branch1 16b36c6


Changing Branches
Changing Branches

To switch to a different branch, you use the git checkout command:

With that, you switch to a different isolated timeline of your project


by changing branches.
Changing Branches

To create a new branch and change to it at the same time, you can use the -b flag:
Changing Branches

To go back to the main branch, use this command:


Merging Branches
Merging Branches

Once we create a branch from master, they start living separate


lives so to say. We don't want this to continue for too long,
because it will become harder and harder to bring the two
together.

For example, after you fully implemented and tested a new


feature in your code, you would want to merge those changes
to the stable branch of your project (which is usually the default
master branch).
Merging Branches

To merge the changes from a different branch into your current branch, you can use
this command:

You would replace <branch-name> with the branch that you want to integrate into
your current branch.
Merging Branches

Master
Commit1 Commit2 Commit3 Commit4
23ad9ad 16b36c6 ad8621a 0e25143

branch1
Commit3
ab63faa1

$ git merge branch1


Deleting Branches
Deleting branches

To delete a branch, you can run the git branch command with the -d flag:

Read more about branching and merging on this link.


Git Workflow
Git workflow

Recap:

❏ Creating a branch $ git checkout -b <new-branch-name>


❏ Renaming current branch $ git branch -m <branch-name>
❏ Listing branches $ git branch [-a]
❏ Switching branches $ git checkout <branch-name>
❏ Deleting a branch $ git branch -d <branch-name>
❏ Merging changes $ git merge <branch-name>
❏ Publishing a branch $ git push -u origin <branch-name>

Note: there are alternatives to these.


Git Workflow
Git workflow

Branches are fundamental to Git and there's lots you can do with them!

create delete checkout

rename compare
Key ● Understanding Branching: You learned what
branching is and its significance in development.
Takeaways ● Branch Creation & Management: You're able to
create, switch, and effectively manage branches.

● Merging Know-How: You learned some


techniques for merging branches.

● Maintaining Repository Health: You understand


the need for and how to delete old branches.
Exercises
Exercise
Exercise
Instructions

● Create a new repository locally.


● Add a readme file to the main branch. Add a heading to file. Make sure to add,
commit your changes.
● Create a branch named content and switch to the new branch.
● Add text to the readme file on the content branch. Don't forget to add and commit
these changes.
● Go back to master and create another branch named hotfix from main. Note that
the changes in your previous branch, content, are not there.
● View all branches on terminal.
Exercise
Exercise
Instructions

● Create a folder.
● Initialize a git repo within that folder.
● Create a readme file in the folder.
● Make changes to the readme file.
● Add the changes to the read me. Check the status of the repository. Then,
leave a commit message and check the status once more.
● What command should you use to check the status of the repository? Write
your answer in this readme.
● Make more changes to the readme file. Use the shorthand command to add
and commit changes.
● Check the history of the repository.
● What command should you use to check the repository's history?
Resources
Git Resources

● Git Handbook
● Git Flow
● Git Glossary
● Git Documentation
● Git Developer Beginner
● Git Config Cheatsheet
● Git Cheatsheet

You might also like