- All right, let's talk about branches.
Branches are used to develop new features
in isolation from the main branch
so that you can quietly test.
And at the moment that you're ready,
you are going to merge.
So the main branch is the default branch
and other branches can be manually added.
Any git repository can have multiple branches
to allow developers to work independently.
And after completion, you can merge the branches
back to the main branch.
In other versions of Git,
"master" was the name of the main branch.
We already talked about that.
That's nowadays deprecated terminology.
And if it's needed, you can use git branch -M main
to set main as the default branch name.
I'd recommend to do that to avoid confusion.
And when you see master, well you know it's the old way
and you should replace main with master.
Now if you work with branches
every now and then it's important to compare your branches.
You can use git diff to do so.
So git diff followed by the different branches
will preview branch differences
and see if there are any potential conflicts.
If you want to merge another branch into the active branch
then you use git merge followed by the name
of the remote branch.
So how do we use branches?
Well, you can use git checkout -b
followed by the branch name to create new branch
and start using it.
Then use git push origin dev-branch to push the new branch
to the remote repository.
Once you have pushed the new branch
to the remote repository,
you can use git checkout main to switch back
to the main branch.
And delete the branch using git branch -d dev-branch.
Or maybe you want to merge before you do so.
So let's talk about merges.
After you make changes to a branch,
the changes can be merged into the main branch.
And that's typically what everybody wants to do, right?
That's what you make your changes for.
Before you do so, it's a good idea to have a look
at the differences.
git diff is going to help you.
So git diff main newfiles,
if newfiles is the name of your new branch, will do that.
So after you have used git checkout main
you can use git merge newfiles to apply changes
from the branch to the main branch.
And if files have been modified in multiple branches,
automatic merges are not possible
where merge conflicts exists.
In such case you need a little bit more of work.
The conflicts must be resolved manually
by editing the affected files.
So before showing you the demo, one more thing.
Instead of git checkout, nowadays
you'll also see git switch.
Git switch, which allows you to switch
between different branches.
You want to use git switch,
that's fine as well.
Where I'm typing, git checkout
just use git switch and it'll work.
Let me show you.
Okay, to start with git branch newfiles
and I'm using git status.
Git status is telling us that we are on the branch main.
Now git checkout newfiles
switches to the branch, newfiles.
I don't even have to do a git status
in order to do so.
I'm in newfiles right now.
So as you can see in newfiles
we still have the current content of the main branch
and that's what we need because we want to develop
on the content of the main branch.
So I'm using echo three > three.txt.
The mission here is that I need a file
and I am going to use my git add
as well as my git commit.
And next my git push
where I need the argument
- -set-upstream origin newfiles.
We need to tell the remote environment
about this branch as well.
And for now the remote environment
doesn't know anything so this is important.
So now the git push has been done
and I'm going to use git checkout main.
So we are in the main branch.
And what is LS showing?
Well, LS is showing a lot but we don't see three.txt.
That is letters three.txt, the file that we just created.
You can use git diff if you want to compare.
Git diff main newfiles
because there we can see the differences.
And in the differences on the top you can see
that we have a +++ three txt.
That's the differences.
So if I'm using git status, then what do we see?
Well, we see nothing relevant.
Only the message about this git ignore.
Now what if I want to merge the changes?
Well that will be git merge newfiles.
That is going to merge the changes
of newfiles into the main branch.
And now if I use my ls command again,
there we can see that three.txt
has been merged into the main branch.
And what is git status showing?
Well, git status is showing on branch main,
your branch is ahead of origin main
by one commit.
Use git push to publish your local commits.
So if you merge, it's getting into the head
and we need to get it out of the head.
And that's what's happening using git push.
And now once more, git status to verify.
And now we can see your branch is up to date
with origin main.
It's always important to use git status,
use git status a lot.
It avoids you from getting confused.