Git Tutorial 6: Branches (Create, Merge, Delete a branch)

codebasics · Beginner ·☁️ DevOps & Cloud ·10y ago

Key Takeaways

This video tutorial covers the basics of Git branches, including creating, merging, and deleting branches, to manage alternate versions of code.

Full Transcript

dear friends in this tutorial we are going to look at branches in gate let's first see why branches are needed if you have code like this where I have this file hungry dot py and let's say you are making some crazy changes on this file ok and these changes are experimental in nature so that you might throw them away hence you want to preserve your original code so in order to do that what you'll typically do is you will copy/paste this file ok and then you will make your changes here I will just call it original file so this one is my original file original file and then you will make your changes here bunch of changes here and then you run it and you kind of do your experiment with it that way your code is still saved in this original file ok but this way of working is not very efficient so get by default supports this branch concept which essentially allows you to do the same thing it allows you to create two branches of your code where you can do parallel code development on both the branches you can make experiments you can test stuff and once things look ok you can merge them back into one file ok so that's essentially what primes will do for you now if you noticed or whenever you have good directory you will see master here so master is a default branch that it will give you now I want to make experimental changes on this core so I need to create a new branch so first let's run git branch come on when you do that it will list all the branches that you have I only have master right now I am going to create a new branch called thirsty and when I do get branch I now see master and thirsty okay now first you need to make thirsty branch active you see there is a star here and this is in green color which means master is an active branch so to make thirsty branch active you will run git checkout spaced branch named thirsty now it is saying it switched to branch thirsty so now you are on a thirsty branch again when you run git branch you'll see star in front of thirsty okay I will now make my changes so if you are not hungry I want to ask are you thirsty so I will just say input odd I you thirsty and based on the user feedback are the asked user to drink water okay so let's say this is your code you ran it it works fine so now you want to commit it so you say git commit before doing it commit you always need to do you get ad and then you get commit minus M water okay so you committed your change into thirsty branch okay now let's so let's check out our master branch back and let's see what our master branch has so I just checked out my master branch and when I move my cursor here you notice that all those changes went away because master branch doesn't have those changes those changes are experimental in nature and they exist only in thirsty branch okay so masters Magda brands now if I again go back to thirsty I will be able to see those changes so you notice there is no thirsty change when I when I switch the branch I will now see this changes right so it's quite powerful all right now let me make one more change so I added water now I am going to ask my kid to drink soda as well because it's so thirsty right so I added that and I will now commit this change as well cool so now if you do get long you see soda water per groupies are all the previous changes and if you switch the branch to master you will obviously lose those changes and when you do when you do get long okay I mistakenly tired let me type it here good long I see only border Pisa you see I don't see any water and soda changes because I am on master branch and that branch doesn't have any or thirsty related for changes okay so I will keep on making my core changes in my thirsty branch like this execute the code run it and the moment when I feel that these chord changes are ready to merge I will do the mush but before I show you the marsh let me now make a chord change into master branch so if you want to make a chord change in master branch you need to check out master branch so now I'm in master branch Here I am going to add so I love to eat junk food so I'm not going to eat only samosa but pizza burger and fries as well okay and as usual you do get a git commit to add your fries we need to get log you have fries burger peas etcetera but this change eat fries doesn't exist in you're thirsty branch you can always verify by checking it out so you see there is no fries here okay so just to give you kind of pictorial view of what we just did you can take a look at this picture so we we're on master branch this this line here is our master branch and we all the circles are individual commits so you added Pisa you added burger and at this point you created a new branch called thirsty so all green colored dots are commits into thirsty branch okay so you added water juice here you are the fries here so fries is not present in thirsty branch and water in juice is not present in master branch okay so now what we are going to do is these changes look okay and I want to merge a thirsty chord changes into master in order to do that you first need to go to your base branch where you want to merge into so I want to merge thirsty chord changes into master branch hence I am going into master branch master branch first okay so master is my active branch and now I will do git merge thirsty this is that moist thirsty branch into master branch when you do that it will ask you to type in the commit message I will do : WQ that will save my commit message and my Maj is done so when I cat get log here here after fries I have one more change which says Kate branch Marge branch thirsty alright now when I look at the code here you will notice now it is a merger of these two branches I have my thirsty code as well and I have fries as well so this is explained by this picture here so until this point you were two different branches and now you merged so at this point you have fries and water and juice as well okay alright now when I look at my github repository I only have master here because I have not pushed my changes so this is the place where you see all the branches on github okay I'm going to now push the thirsty branch okay so it says good push alright so it is not alright so you will just do git push and this is failed to push reference up to the DJ because it is behind integrator remote changes okay let me do get poked is something which is not okay git push now when you do get push it's gonna post the master branch alright so right now what it did is when you do get push at any time it is just pushing the current active branch so master changes are pushed if I look at here I see all the all the changes say fries and thirsty and everything okay but now I want to help thirsty branch present here as well so for that you need to check out that branch first so now your interests around and now you do git push okay what this is saying is that upstream meaning github doesn't help thirsty branch so you want to push a new branch use this command yes I want to do that so just copy paste this command and what this is saying is set upstream origin thirsty meaning pushed thirsty to my upstream which is github so now when i refresh and take a look here I have two branches master and thirsty so when I switch the branch here thirsty look at my core this is just my thirsty branch code it doesn't have fries here and if you look at master it has everything okay so branches essentially allows you to maintain two separate copies of your code okay and those copies might have alternate versions of your code your algorithm or whatever you're trying to do all right okay now let's see how you can delete the branch okay so I'm going to show you how to delete the branch so for deleting the branch first let me create some dummy branch now last time when we created a branch we shoot two commands get branch and then get checkout you can do both the things in one come on by doing it check out - be dummy so mine what this is saying is create a new branch called dummy for me - B means branch and also check out that branch alright so now it created the me branch and it made that active as well so if you do get branch see you see a third branch called dummy and this is active ok now what I'm going to do is delete this branch so get Ranchos three branches I want to delete the me the way you do it is by running git branch - dee dee means delete dummy when you do that it says it deleted the brunt so now when I do get branch I don't have that planche anymore alright so that was all about get branches and great merging thank you for

Original Description

In this tutorial, I'll cover a very powerful feature of git called branch. I will show you how you can manage alternate versions of your code by creating separate branches, how to merge branches and delete them. Here is the list of topics covered in this git tutorial, 1) how to create a new branch in git 2) merging a branch 3) deleting a branch from local as well as remote Git Tutorial: https://www.youtube.com/watch?v=xAAmje1H9YM&list=PLeo1K3hjS3usJuxZZUBdjAcilgfQHkRzW Do you want to learn technology from me? Check https://codebasics.io/ for my affordable video courses. Next Video: Git Tutorial 7: What is HEAD?: https://www.youtube.com/watch?v=ZaI1co-rt9I&list=PLeo1K3hjS3usJuxZZUBdjAcilgfQHkRzW&index=7 Machine Learning Tutorial With Python: https://www.youtube.com/watch?v=gmvvaobm7eQ&list=PLeo1K3hjS3uvCeTYTeyfe0-rN5r8zn9rw Website: https://codebasics.io/ Facebook: https://www.facebook.com/codebasicshub Twitter: https://twitter.com/codebasicshub
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from codebasics · codebasics · 48 of 60

1 Python Tutorial - 1. Install python on windows
Python Tutorial - 1. Install python on windows
codebasics
2 Python Tutorial - 2. Variables
Python Tutorial - 2. Variables
codebasics
3 Python Tutorial - 3. Numbers
Python Tutorial - 3. Numbers
codebasics
4 Python Tutorial - 4. Strings
Python Tutorial - 4. Strings
codebasics
5 Python Tutorial - 5. Lists
Python Tutorial - 5. Lists
codebasics
6 Python Tutorial - 6. Install PyCharm on Windows
Python Tutorial - 6. Install PyCharm on Windows
codebasics
7 PyCharm Tutorial - 7. Debug python code using PyCharm
PyCharm Tutorial - 7. Debug python code using PyCharm
codebasics
8 Python Tutorial -  8. If Statement
Python Tutorial - 8. If Statement
codebasics
9 Python Tutorial - 9. For loop
Python Tutorial - 9. For loop
codebasics
10 Python Tutorial -  10. Functions
Python Tutorial - 10. Functions
codebasics
11 Python Tutorial - 11. Dictionaries and Tuples
Python Tutorial - 11. Dictionaries and Tuples
codebasics
12 Python Tutorial - 12. Modules
Python Tutorial - 12. Modules
codebasics
13 Python Tutorial - 13. Reading/Writing Files
Python Tutorial - 13. Reading/Writing Files
codebasics
14 How to install Julia on Windows
How to install Julia on Windows
codebasics
15 Python Tutorial - 14. Working With JSON
Python Tutorial - 14. Working With JSON
codebasics
16 Julia Tutorial - 1. Variables
Julia Tutorial - 1. Variables
codebasics
17 Julia Tutorial - 2. Numbers
Julia Tutorial - 2. Numbers
codebasics
18 Python Tutorial - 15. if __name__ == "__main__"
Python Tutorial - 15. if __name__ == "__main__"
codebasics
19 Julia Tutorial - Why Should I Learn Julia Programming Language
Julia Tutorial - Why Should I Learn Julia Programming Language
codebasics
20 Python Tutorial  - 16. Exception Handling
Python Tutorial - 16. Exception Handling
codebasics
21 Julia Tutorial - 3. Complex and Rational Numbers
Julia Tutorial - 3. Complex and Rational Numbers
codebasics
22 Julia Tutorial - 4. Strings
Julia Tutorial - 4. Strings
codebasics
23 Python Tutorial -  17. Class and Objects
Python Tutorial - 17. Class and Objects
codebasics
24 Julia Tutorial - 5. Functions
Julia Tutorial - 5. Functions
codebasics
25 Julia Tutorial - 6. If Statement and Ternary Operator
Julia Tutorial - 6. If Statement and Ternary Operator
codebasics
26 Julia Tutorial - 7. For While Loop
Julia Tutorial - 7. For While Loop
codebasics
27 Python Tutorial  - 18. Inheritance
Python Tutorial - 18. Inheritance
codebasics
28 Julia Tutorial - 8. begin and (;) Compound Expressions
Julia Tutorial - 8. begin and (;) Compound Expressions
codebasics
29 Python Tutorial - 12.1 - Install Python Module (using pip)
Python Tutorial - 12.1 - Install Python Module (using pip)
codebasics
30 Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
codebasics
31 Julia Tutorial - 10. Exception Handling
Julia Tutorial - 10. Exception Handling
codebasics
32 Python Tutorial  - 19. Multiple Inheritance
Python Tutorial - 19. Multiple Inheritance
codebasics
33 Python Tutorial - 20. Raise Exception And Finally
Python Tutorial - 20. Raise Exception And Finally
codebasics
34 Python Tutorial - 21. Iterators
Python Tutorial - 21. Iterators
codebasics
35 Python Tutorial - 22. Generators
Python Tutorial - 22. Generators
codebasics
36 Python Tutorial - 23. List Set Dict Comprehensions
Python Tutorial - 23. List Set Dict Comprehensions
codebasics
37 Python Tutorial - 24. Sets and Frozen Sets
Python Tutorial - 24. Sets and Frozen Sets
codebasics
38 Python Tutorial - 25. Command line argument processing using argparse
Python Tutorial - 25. Command line argument processing using argparse
codebasics
39 Debugging Tips - What is bug and debugging?
Debugging Tips - What is bug and debugging?
codebasics
40 Debugging Tips - Conditional Breakpoint
Debugging Tips - Conditional Breakpoint
codebasics
41 Debugging Tips - Watches and Call Stack
Debugging Tips - Watches and Call Stack
codebasics
42 Python Tutorial - 26. Multithreading - Introduction
Python Tutorial - 26. Multithreading - Introduction
codebasics
43 Git Tutorial 3:  How To Install Git
Git Tutorial 3: How To Install Git
codebasics
44 Git Tutorial 1: What is git / What is version control system?
Git Tutorial 1: What is git / What is version control system?
codebasics
45 Git Tutorial 2 : What is Github? | github tutorial
Git Tutorial 2 : What is Github? | github tutorial
codebasics
46 Git Tutorial 4: Basic Commands: add, commit, push
Git Tutorial 4: Basic Commands: add, commit, push
codebasics
47 Git Tutorial 5: Undoing/Reverting/Resetting code changes
Git Tutorial 5: Undoing/Reverting/Resetting code changes
codebasics
Git Tutorial 6: Branches (Create, Merge, Delete a branch)
Git Tutorial 6: Branches (Create, Merge, Delete a branch)
codebasics
49 Git Github Tutorial 10: What is Pull Request?
Git Github Tutorial 10: What is Pull Request?
codebasics
50 Git Tutorial 7: What is HEAD?
Git Tutorial 7: What is HEAD?
codebasics
51 Git Tutorial 9: Diff and Merge using meld
Git Tutorial 9: Diff and Merge using meld
codebasics
52 Difference between Multiprocessing and Multithreading
Difference between Multiprocessing and Multithreading
codebasics
53 Python Tutorial - 27. Multiprocessing Introduction
Python Tutorial - 27. Multiprocessing Introduction
codebasics
54 Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
codebasics
55 Git Tutorial 8 - .gitignore file
Git Tutorial 8 - .gitignore file
codebasics
56 Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
codebasics
57 Python Tutorial - 30. Multiprocessing Lock
Python Tutorial - 30. Multiprocessing Lock
codebasics
58 Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
codebasics
59 What is code?
What is code?
codebasics
60 Python unit testing - pytest introduction
Python unit testing - pytest introduction
codebasics

This tutorial teaches how to use Git branches to manage different versions of code, including creating, merging, and deleting branches. It's a fundamental skill for any developer to manage their codebase efficiently.

Key Takeaways
  1. Create a new branch using Git
  2. Make changes to the code in the new branch
  3. Merge the new branch into the main branch
  4. Delete the new branch after merging
💡 Using branches in Git allows developers to work on different features or versions of their codebase simultaneously without affecting the main codebase.

Related Reads

📰
Slack Built Its Own EC2 Platform. Buying One Was the Harder Question.
Learn how Slack built its own EC2 platform and the considerations behind the build vs buy decision
Medium · DevOps
📰
Engineering Culture Starts with Version Control — Building Trust, Ownership, and Collaboration at…
Learn how version control systems can foster a culture of trust, ownership, and collaboration in engineering teams
Medium · DevOps
📰
Not Every Workload Belongs on a Free Server: Red Flags and Exit Criteria
Learn when to avoid using free servers for workloads and how to identify red flags and exit criteria
Dev.to · Casey Sun
📰
Reviewing a PR You Can't Run Locally: A Reproducible Free-Tier Loop
Learn to review a PR without running it locally using a reproducible free-tier loop, ensuring efficient open-source maintenance
Dev.to · Blake Yang
Up next
Containers vs Virtual Machines
KodeKloud
Watch →