#16 Pushing and Deleting branches on remote

It is very easy to push your local branches on Github. It is similar to pushing your code on remote, like we have seen it in here.

Create a new repository on github as we have done it earlier. After creating a repository you will get this page.

So, now I have a project which I want to push on github. To push it on github I will use second option which is shown in above image.

git remote add origin url
git branch -M main
git push -u origin main

After executing the last command you will get this prompt. If you are executing this command then you will get a pop-up to enter your username and password of your Github account. At that point you need to insert a username and password and your code will be pushed on Github. Take reference to this.

Here I am getting this prompt because I have gone through that procedure and that’s why username and password are already saved in the system. Now here it is asking for authorization. It is a security system from git.

When I click on the button Authorize GitCredentialManager git shows me this page i.e. Authentication Succeeded. Now if I go and see on GitHub repository I will be able to see my pushed code there.

Now my master branch is on github. You can click on branch button to see which branches are there.

As shown in above image click on that button to see the branches.

Now I’ll create a new branch with the following command:

git checkout -b branch_name

Now in this branch I will do some changes and I will try to push this branch on GitHub.

Pushing the branch to remote

Now, as you can see our branch is ready to be pushed on GitHub. Make sure that your working tree is clean before pushing your branch on GitHub. Use the git status command to see the status. If it is clean then use following command to push the branch on GitHub.

git push origin branch_name

After executing this command your branch will be pushed to GitHub. You can see the branches by clicking on branch button.

So there is our feature branch and there is our default master branch.

You need to be in the same branch which you want to push on remote.

You can rename the branches while pushing on remote. You need to use following command to rename the branch while pushing it on remote.

git push origin branch_name:new_branch_name

here after colon(:) you have to provide the new branch name.

It is recommended to keep the branch name on remote and local as same.

Now if I try to change the feature branch name then there will be two branches one will be with the previous name and another will be with the new name.

Deleting the remote branches

Deletion of remote branches is also simple. It is just like we used to delete the branches on local. We can delete the branches using the following command.

git push -d origin branch_name

After execution of above command feature branch will be deleted as you can see in above image.

So this was the basics of the git we hope you have learnt the basics of git form this series of posts. Now here we are attaching the cheat sheet of basic git commands so that you can I have quick reference to them.

Leave a Comment

Your email address will not be published. Required fields are marked *