
No Comments Yet
Be the first to share your thoughts and start the conversation.
GSAP Course launch discount ends soon!🎉
Claim DiscountBe 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.