Loading lesson path
Concept visual
Start at both ends
In Git, a branch is like a separate workspace where you can make changes and try new ideas without affecting the main project. Think of it as a "parallel universe" for your code.
Branches let you work on different parts of a project, like new features or bug fixes, without interfering with the main branch.Let's say you have a large project, and you need to update the design on it. How would that work without and with Git:
Make copies of all the relevant files to avoid impacting the live version Start working with the design and find that code depend on code in other files, that also need to be changed! Make copies of the dependant files as well. Making sure that every file dependency references the correct file name
EMERGENCY! There is an unrelated error somewhere else in the project that needs to be fixed ASAP!Save all your files, making a note of the names of the copies you were working on Work on the unrelated error and update the code to fix it Go back to the design, and finish the work there Copy the code or rename the files, so the updated design is on the live version (2 weeks later, you realize that the unrelated error was not fixed in the new design version because you copied the files before the fix)
Formula
With a new branch called new - design, edit the code directly without impacting the main branchEMERGENCY! There is an unrelated error somewhere else in the project that needs to be fixed ASAP!Formula
Create a new branch from the main project called small - error - fix
Fix the unrelated error and merge the small - error - fix branch with the main branch
You go back to the new - design branch, and finish the work there
Merge the new - design branch with main (getting alerted to the small error fix that you were missing)Branches allow you to work on different parts of a project without impacting the main branch. When the work is complete, a branch can be merged with the main project. You can even switch between branches and work on different projects without them interfering with each other. Branching in Git is very lightweight and fast!
Let's say you want to add a new feature. You can create a new branch for it. Let add some new features to our index.html page. We are working in our local repository, and we do not want to disturb or possibly wreck the main project.
Now we created a new branch called "
Formula
hello - world - images"
Let's confirm that we have created a new branch. To see all branches in your repository, use:
Formula
We can see the new branch with the name "hello - world - images", but theNow you can work in your new branch without affecting the main branch.
Now we have moved our current workspace from the master branch, to the new branch Open your favourite editor and make some changes. For this example, we added an image (img_hello_world.jpg) to the working folder and a line of code in the index.html file:
<!DOCTYPE html> <html> <head>
Formula
< title > Hello World!</title ><link rel="stylesheet" href="bluestyle.css"> </head> <body>
Formula
< h1 > Hello world!</h1 ><div><img src="img_hello_world.jpg" alt="Hello World from Space"
style="width:100%;max-width:960px"></div>Formula
< p > This is the first file in my new Git Repo.</p >
< p > A new line in our file!</p ></body> </html> We have made changes to a file and added a new file in the working directory (same directory as the main branch ). Now check the status of the current branch