
No Comments Yet
Be the first to share your thoughts and start the conversation.
Be the first to share your thoughts and start the conversation.
How did you manage to remove the blur property and reach here?
Upgrading gives you access to quizzes so you can test your knowledge, track progress, and improve your skills.
By logging in, you'll unlock full access to this and other free tutorials on JSM Pro.
Why? Logging in lets us personalize your learning experience, track your progress, and keep you in the loop with new workshops, coding tips, and platform updates.
You'll also be the first to know about upcoming launches, events, and exclusive discounts.
No spam—just helpful content to level up your skills.
If that sounds fair, go ahead and log in to continue →
Enter your name and email to get instant access
In this lesson, we dive into the concepts of branching and merging in Git, highlighting how these features enhance project management by allowing multiple versions of a project to exist simultaneously. Branching enables developers to experiment and create new features without affecting the original project, facilitating smoother collaborative workflows.
git branch <branch_name>
. To switch branches, use git checkout <branch_name>
.git checkout -b <branch_name>
.git add .
is used to stage changes for a commit, along with a commit message in imperative mood.git push --set-upstream origin <branch_name>
to publish your local branch to the remote repository.git pull
.00:00:00 Let's take things up a notch and dive into branching and merging.
00:00:04 This is where Git truly shines.
00:00:07 Branches in Git allow you to create different versions of your project, like making a copy of a project at a specific moment in time.
00:00:16 Whatever changes you make in this copied version won't affect the original.
00:00:22 The main project or branch stays untouched while you experiment, modify, or add new features in the copied branch.
00:00:30 If everything works out, you can later merge your changes back into the original project.
00:00:35 If not, no worries, the original remains safe and unchanged.
00:00:40 When working in a team, using separate branches for different features or bug fixes is essential.
00:00:47 It allows you and your team to work independently on different parts of the code without causing conflicts or errors, ensuring everyone can focus on their
00:00:58 own tasks.
00:00:58 At the start, you'll have one default branch called main.
00:01:02 To create a new branch, run git branch, and then type a branch name.
00:01:08 This will create a new branch.
00:01:11 And if you want to switch to this newly created branch, then run git checkout, and then enter the branch name you want to check out too.
00:01:19 And there we go.
00:01:20 Switch to branch branch name.
00:01:23 Now, if you want to go back to main, just run git checkout main.
00:01:28 There we go.
00:01:29 And here's a little pro tip.
00:01:30 There is a shortcut to create a new branch and immediately move to it.
00:01:35 To do that, run git checkout with a dash B flag, and then enter a branch name, such as feature branch.
00:01:45 Of course, this branch name and feature branch are just dummy names.
00:01:49 Make sure your branch name is short and explains which changes will you be making on that branch.
00:01:54 For more tips on how to properly name your branches, you can download the Git reference guide.
00:01:59 So let's create and move to this feature branch in one command.
00:02:04 There we go.
00:02:05 And what I'm about to say next is very important.
00:02:08 So keep it in mind.
00:02:09 When you create a new branch, it'll be based on the branch you're currently on.
00:02:15 So if you're on the main branch and run the command, the new branch will contain the code from the main branch at that point in time.
00:02:24 However, if you're on a different branch with different code, the new branch will inherit that code instead.
00:02:32 So to ensure you're creating the new branch from the correct starting point, you should either first switch to the branch you want to base the new one
00:02:40 on or run this command, git branch.
00:02:44 Then you can enter a new branch name.
00:02:48 And then the next thing can be the source branch.
00:02:51 So if you do it this way and replace the new branch name and the source branch with the names of actual branches, then it'll create a new branch from another
00:03:01 specific branch.
00:03:02 So if you run this command, you can directly create and switch to a branch based on any other branch without needing to check out to it first.
00:03:11 For now, I'll remove that.
00:03:12 And let's say that we want to go into our code and implement this feature we're working on.
00:03:17 Let's say that in our case, the only feature we want to do is to modify the readme.
00:03:22 So below hello-git, I'll say, I'm adding this from feature-branch.
00:03:30 Here we go.
00:03:31 Feature implemented.
00:03:32 If only it was this easy.
00:03:34 And you can see that our IDE immediately highlighted this readme file in blue, indicating that it has some changes.
00:03:41 Now we need to add it, commit it, and push it.
00:03:44 This time, instead of saying git add readme-md, let's just use the git add dot, which is a command that you'll use much more often.
00:03:52 Next, we need to commit the changes with git commit-m, and then we have to add a commit message.
00:03:59 So, this is the perfect time to learn how to write a proper commit message.
00:04:05 A quality commit message is written in the imperative mood, a grammatical mood that sounds like you're giving a command.
00:04:14 Like, improve mobile responsiveness or add A-B testing.
00:04:19 When writing your commit message, make it answer this question.
00:04:24 If applied to the code base, this commit will, and then fill in the blank.
00:04:30 Like this commit will upgrade the packages or this commit will fix thread allocation.
00:04:37 And why do we do this?
00:04:39 Well, because it answers the question, what will happen when I merge the branch containing this commit?
00:04:46 It will add A-B testing, for example.
00:04:49 Be direct and eliminate filler words.
00:04:52 For example, let's use modify readme in this case.
00:04:57 It's short, sweet, and in an imperative mood.
00:05:00 And press enter.
00:05:01 There we go, we've just made Git aware of our commit.
00:05:05 Now that you know how to write better commits, let's take a moment and check out our remote repository.
00:05:10 What do you think?
00:05:12 Will it have the latest commit we made?
00:05:14 Let's reload it, and it's the same.
00:05:17 It doesn't contain our newly created feature branch.
00:05:20 Do you know why?
00:05:22 It's because the changes we made are in the local repository, which has not yet been synced with the remote repo.
00:05:29 To see those changes, first, you'll have to publish your local branch.
00:05:33 And you can do that using the git push –set-upstream origin, and then the name of the branch, in this case, feature-branch.
00:05:46 And press Enter.
00:05:48 There we go.
00:05:49 An upstream branch is a remote branch that your local branch tracks.
00:05:54 When you set an upstream branch using set upstream, you're essentially linking your local branch to a branch on a remote repo.
00:06:02 Through this command, you push a local feature branch to the origin remote repository, and then you set the upstream branch for your local feature branch
00:06:13 to track origin feature branch.
00:06:16 Alternatively, you can also use git push dash u origin feature dash branch.
00:06:24 Or the name of your branch, of course.
00:06:26 Both setupstream and dash u establish a tracking relationship between your local branch and the remote branch.
00:06:35 This way, in the future, if you want to push something from your local branch to your remote branch, you simply have to run git push.
00:06:43 That's it.
00:06:44 At this moment, for us, everything is up to date.
00:06:46 But as you make future changes, you don't have to rerun setupstream or dash you.
00:06:51 You only have to add it, commit it, and push it.
00:06:55 That's it.
00:06:55 And if somebody else made changes to your remote branch, either directly or by merging some other changes into it, you have to make your local branch up
00:07:05 to date with the remote branch.
00:07:07 And you do that by using the git pull command.
00:07:11 There we go.
00:07:12 It's already up to date in this case.
00:07:14 This command fetches changes from the remote repo and merges them into your local repo for that branch.
00:07:20 There are also two more advanced and highly practical commands that help you move changes between your remote and local repos.
00:07:28 And I'll explain those in detail later on.
00:07:31 So keep watching.
00:07:32 Now, if you go back to GitHub, check this out.
00:07:36 Feature branch had recent pushes a few minutes ago.
00:07:39 And if you go to branches, You can see our feature branch right here.
00:07:44 So if you click on it, you can see the state of your code base on that branch.
00:07:49 And you can see the modify readme commit that we made.
00:07:52 But back on the main branch, it hasn't been modified.
00:07:56 It simply says hello git still.
00:07:59 So once you have made all of the changes for this branch and implemented the feature you wanted to add and tested it, you'll want to merge it back into
00:08:07 the main branch.
00:08:08 To do that, you must open up a pull request.