Wednesday, May 25, 2022

Git branching - local and remote

Hello all, in this post specially dedicated to all DevOps/DevSecOps Engineers.  Where their daily routine will have this commands if we know these we can play with them easily if you don't then it will be horrifies you! 

Git Branching explained in detailed

Prerequisites to this you should be aware git basics and to experiment with these command examples should have git installed on your Mac/Linux or git bash on Windows system.

What is the Branch in Git?

A Git branch is a separate line of work, where your work will not disturb other DevOps team members work.
Branch is a simply a movable  pointer to commit, this we can observe using git branch command output wherever you see that '*' prefixed there the pointer pointing. Default branch is master or main (new trend) but it is optional we can name anything when we initiate the git repository.

Main branch is the FIRST branch of your project that means once you run the 'git init' then 'git branch' you can observe this.
On the branch we can have many changes until find the solution and accordingly you need to do multiple commits.

Each branch created for different purpose for example new feature development we name them as 'feature'. similarly we can also have planned releases where we name them as 'release1.0' or 'dev-release'. Again it is flexible to the SCM manager decision.

Why we need to create branch in Git?

The Major reasons for Git branching are:

  • Any new features development could BREAK the code, to save this.
  • No disturbance between two developer codes that means this git branching simplify the collaboration work which gives more agility to the project

Gitting Start Branching

Here I'm with an interesting story about SCM Git branching that helps to work in collaborative environments where DevOps is a culture.

Let me share with you guys about the steps that I've executed in this are realtime requirements.

Top 10 Examples on git branch sub-command 


1. How to create a branch and check using git sub-commands?

To create a new branch from the existing branch. Here you should be on one of the branch. Generally we start from main or master branch.
git branch newbranch
git branch

2. How to jump onto the new branch directly when it is created?

Create a branch and switch to it in single command
git checkout -b feature
git branch

3. How to switch between the git branches?

Switch to branch We have two command options for switching between branches. 
a. Regular subcommand for switching to a branch
git checkout newbranch
git branch
b. New way to switch between the branch
git switch master
git branch

5. How can I view all local repo branches? 

To view all branches which are created in the local repository no option will do or else we can use status with -b to see in single-line output
git branch 
git status -b -s #alternative option

6. How can I see remote branches on my local laptop/local repo?

To view all the remote branches only you need to use -r or --remote as options

This is command will help you to get more info about all the branches are available in the remote git server such as GitHub or BitBucket etc.
 
git branch -r 
git branch --remote 
git branch remote to view your github or bitbucket branches



7. How can I see all local and remote repositories together?

To view all branches including remote branches use -a or --all options 
git branch -a 
git branch --all 
Observe the different colors for each repos.
  1. The green color indicates current working tree(branch)
  2. The red indicates remote repo branches
  3. The white color indicate local branches
git branch all command example



8. Is there any way to delete a git branch in local repo? 

Delete a branch can be allowed when we use the -d or --delete options on a target branch.

Note : You cannot delete the branch on which you are working. Switch to anyother branch then run this.
git branch -d newbranch
git branch --delete newbranch
Removing a branch


[This is best option to delete because it preserves the recent changes made on the local branch] 9. Deleting the remote branch This is delicated operation, double check before you run this command.
git branch -D myremote-branch 
Compare -d and -D example for git branch removal


9. How to delete a remote branch in git command line ?

Better option is to use delete option with push command
git push origin --delete myremote-branch

10. How to rename a branch on git?

Move a branch/Rename 
a. Linux move command used to rename same way here a branch can be renamed and its reflog
git branch -m newbranch dev
git branch --move newbranch dev 
Git branch move or renaming


b. Rename forcefully 
Even though your target branch exist the git move will happen forcefully when we use -M option.
git branch -M newbranch dev


Hope you enjoy this story. Connect with our earlier technical articles might give some new tricks or tips for your project growth and more productive.

Cheers!!

No comments:

Categories

Kubernetes (24) Docker (20) git (13) Jenkins (12) AWS (7) Jenkins CI (5) Vagrant (5) K8s (4) VirtualBox (4) CentOS7 (3) docker registry (3) docker-ee (3) ucp (3) Jenkins Automation (2) Jenkins Master Slave (2) Jenkins Project (2) containers (2) docker EE (2) docker private registry (2) dockers (2) dtr (2) kubeadm (2) kubectl (2) kubelet (2) openssl (2) Alert Manager CLI (1) AlertManager (1) Apache Maven (1) Best DevOps interview questions (1) CentOS (1) Container as a Service (1) DevOps Interview Questions (1) Docker 19 CE on Ubuntu 19.04 (1) Docker Tutorial (1) Docker UCP (1) Docker installation on Ubunutu (1) Docker interview questions (1) Docker on PowerShell (1) Docker on Windows (1) Docker version (1) Docker-ee installation on CentOS (1) DockerHub (1) Features of DTR (1) Fedora (1) Freestyle Project (1) Git Install on CentOS (1) Git Install on Oracle Linux (1) Git Install on RHEL (1) Git Source based installation (1) Git line ending setup (1) Git migration (1) Grafana on Windows (1) Install DTR (1) Install Docker on Windows Server (1) Install Maven on CentOS (1) Issues (1) Jenkins CI server on AWS instance (1) Jenkins First Job (1) Jenkins Installation on CentOS7 (1) Jenkins Master (1) Jenkins automatic build (1) Jenkins installation on Ubuntu 18.04 (1) Jenkins integration with GitHub server (1) Jenkins on AWS Ubuntu (1) Kubernetes Cluster provisioning (1) Kubernetes interview questions (1) Kuberntes Installation (1) Maven (1) Maven installation on Unix (1) Operations interview Questions (1) Oracle Linux (1) Personal access tokens on GitHub (1) Problem in Docker (1) Prometheus (1) Prometheus CLI (1) RHEL (1) SCM (1) SCM Poll (1) SRE interview questions (1) Troubleshooting (1) Uninstall Git (1) Uninstall Git on CentOS7 (1) Universal Control Plane (1) Vagrantfile (1) amtool (1) aws IAM Role (1) aws policy (1) caas (1) chef installation (1) create deployment (1) create organization on UCP (1) create team on UCP (1) docker CE (1) docker UCP console (1) docker command line (1) docker commands (1) docker community edition (1) docker container (1) docker editions (1) docker enterprise edition (1) docker enterprise edition deep dive (1) docker for windows (1) docker hub (1) docker installation (1) docker node (1) docker releases (1) docker secure registry (1) docker service (1) docker swarm init (1) docker swarm join (1) docker trusted registry (1) elasticBeanStalk (1) global configurations (1) helm installation issue (1) mvn (1) namespaces (1) promtool (1) service creation (1) slack (1)