Let's imagine that I've added some important content to my file.
One staged it with Git at this command and performed a new commit with git commit the same command.
But on this point I want to experiment with this file and perform some tests.
But on the other hand, I don't want to fail anything because it's in production.
That's where exactly we need.
Branching.
Creating a new branch in a project basically means creating a new copy of that project.
You can experiment with this copy without affecting the original.
So if the experiment fails, you can just abandon it and return to the original the master branch.
But if your tests are successful, Git makes it easy to import these experimental elements into the
master and even if you change your mind later on, you can easily revert back to the state of the project
before this merger.
After creating a new branch and git, you can create new commits in a branch while not affecting other
branches.
It is one of the best features of git.
So by default you are in the master branch and it is commonly used that people usually expect to find
the latest up to date code in a particular project in the master branch.
So with that in mind, let's start working with branches and good to see the list of branches and the
current branch you are working on.
Run the following command git branch.
If you have cloned your repository or set a remote, you can see the remote branches to just add dash
a to the command and it will look like git branch dash.
The branches that colored in red signify that they are on a remote.
In my case, I've got just one remote branch and one local branch which is master.
See that asterisk to the left of master?
It indicates that I'm working just now on this branch.
Now let's create a branch and get to create a new branch and stay in your current branch.
Just run the following git branch intel, name it test so I'll type git branch test and press.
Enter however on run and git branch command again.
It seems that the active branch is still the master branch.
To change the active branch.
We can run the check out command so I'll tap check out git check out test.
You can also combine the two commands above and thereby create and check out to a new branch in a single
command by adding dash B to the check out command so you could type git check out dash b test to.
Now if you run git branch command again, you'll see that your active branch is test to the branches
we've just created are based on the latest commit of the current active branch, which in our case is
master.
If you want to create a branch based on a certain commit, let's say we want to use a commit a 9590
you can run the following command git check out dash B test old.
That will be a name for this branch followed by hash.
In my case it will be a 9590.
You can rename a branch by running git branch dash.
Hm.
Temp renamed.
It will rename the current branch to temp renamed name to delete a branch.
Run the following command git branch dash capital dx test two but try not to delete branches unless
you have two as there is not really any downside to keeping branches and you have to delete them only
if the number of branches in the repository becomes too large to be manageable.
Please note that that dash capital D option used in above example deletes a branch even if it is not
synchronized with a remote branch.
This means that if you have commits in your current branch that have not been pushed yet, this capital
D will still delete your branch without providing any warning.
To ensure you don't lose data you can use Dash rd option.
This RD option only deletes a branch.
If it has been synchronized with a remote branch.
So let's try running this now.
I'll tap, give it branch D test one and get.
Should give you a warning and abort the operation as the data hasn't been merged with a branch yet.